TCPDFxajaxと同じようにpluginで組み込む
その他の方法は、wikiか検索すればでてくる

目次

1.ダウンロード

TCPDFをここからダウンロード。PHP5用の他にPHP4用もある
結構更新頻度が高いようでその時の最新のものでいいかな

解凍するとtcpdfフォルダが1個でてくる。

2.ファイルをコピー

xajaxと同じように解凍してでてきた tcpdf/配下を system/plugins/ へコピー。
中にある「example」と「doc」は、コピー先にはいらないので削除。

3.プラグインファイルの作成

system/plugins/に「tcpdf_pi.php」を作成。

ソース

4.langファイルの作成

上のプラグインファイルでrequireしている「tcpdf/config/lang/jpn.php」というファイルは、
ダウンロードしたものにはないんで、tcpdf/config/lang/eng.php をリネームして保存。

ソース

// JAPANESE

global $l;
$l = Array();

// PAGE META DESCRIPTORS --------------------------------------

$l['a_meta_charset'] = 'UTF-8';
$l['a_meta_dir'] = 'jpn';
$l['a_meta_language'] = 'ja';

// TRANSLATIONS --------------------------------------
$l['w_page'] = 'page';

5.サンプル

tcpdf/example/にあるサンプルコードをコントローラーへ移植。

load->view('pdf_index');
    }


    function example_001()
    {
        $this->load->plugin( 'tcpdf' );
        $pdf = tcpdf();

        // set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor("Nicola Asuni");
        $pdf->SetTitle("TCPDF Example 002");
        $pdf->SetSubject("TCPDF Tutorial");
        $pdf->SetKeywords("TCPDF, PDF, example, test, guide");
        // remove default header/footer
        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);
        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);

        //set auto page breaks
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

        //initialize document
        $pdf->AliasNbPages();

        // add a page
        $pdf->AddPage();

        // ---------------------------------------------------------

        // set font
        $pdf->SetFont("kozgopromedium", "", 12);

        $msg = "Example 001 あいうえお 名前";

        // print a line using Cell()
        $pdf->Cell(0,10,$msg,1,1,'C');

        // ---------------------------------------------------------

        //Close and output PDF document
        $pdf->Output("example_001.pdf", "I");
    }

    function example_002()
    {

        $site_url = $this->config->site_url();
        $this->load->plugin( 'tcpdf' );
        $pdf = tcpdf();


        // set document information この辺は無くてもOK
        //$pdf->SetCreator(PDF_CREATOR);
        //$pdf->SetAuthor('Nicola Asuni');
        //$pdf->SetTitle('TCPDF Example 006');
        //$pdf->SetSubject('TCPDF Tutorial');
        //$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

        // set default header data
//        $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
        $pdf->SetHeaderData('', '', '日本語でもOKかな??', 'ヘッダーストリングって?');

        // set header and footer fonts ヘッダーとフッターのフォントは別に指定
        $pdf->setHeaderFont(Array("kozgopromedium", '', PDF_FONT_SIZE_MAIN));
        $pdf->setFooterFont(Array("kozgopromedium", '', PDF_FONT_SIZE_DATA));

        //set margins
        $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
        $pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
        $pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

        //set auto page breaks 改ページ:これをtrueにすると セルの途中で改ページされず自動調節される
        $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
//        $pdf->SetAutoPageBreak(FALSE, PDF_MARGIN_BOTTOM);

        //set image scale factor
        //$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); 

        //set some language-dependent strings
//        $pdf->setLanguageArray($l); 

        // ---------------------------------------------------------

        // set font これはBODYのフォント
        $pdf->SetFont("kozgopromedium", "", 10);


        // - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
        // Print a table

        // add a page
        $pdf->AddPage();

        // create some HTML content
        //$subtable = "
ab
cd
"; $subtable = "
ab
cd
"; //$htmltable = "

HTML TABLE:

#RIGHT alignLEFT align4A
1A1 example link column span. One two tree four five six seven eight nine ten.
line after br
small text normal subscript normal superscript normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  1. first
    1. sublist
    2. sublist
  2. second
small small small small small small small small small small small small small small small small small small small small
4B
".$subtable."A2 ? € € & e è
A2 ? € € & e è
Red Yellow BG4C
1A2AA
2AB
2AC
4D
1B4E
1C2C3C4F
"; $htmltable = "

HTML TABLE:

#RIGHT alignLEFT align4A
1A1 example link column span. One two tree four five six seven eight nine ten.
line after br
small text normal subscript normal superscript normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla
  1. first
    1. sublist
    2. sublist
  2. second
small small small small small small small small small small small small small small small small small small small small
4B
".$subtable."A2 ? € € & e è
A2 ? € € & e è
Red Yellow BG4C
1A2AA
2AB
2AC
4D
1B4E
1C2C3C4F
"; // output the HTML content $pdf->writeHTML($htmltable, true, 0, true, 0); // Print some HTML Cells $cellcontent = "red green blue
red green blue"; $pdf->SetFillColor(255,255,0); $pdf->writeHTMLCell(0, 0, '', '', $cellcontent, 'LRTB', 1, 0, true, 'L'); $pdf->writeHTMLCell(0, 0, '', '', $cellcontent, 'LRTB', 1, 1, true, 'C'); $pdf->writeHTMLCell(0, 0, '', '', $cellcontent, 'LRTB', 1, 0, true, 'R'); //$pdf->AddPage(); $stehtml = "
" . "" . "" . "" . "" . ""; for ($i=5;$i<=55;$i++) { if ($i == 21) { $stehtml .= ""; } else { $stehtml .= ""; } } $stehtml .= "
abcdefg@a1234567890qwertyuio.comashshshfvjdbvjdhfbjbvcdjhbvjhbvjdhbf名前(1行目)草彅ここは2行連結
ここは3行連結あいうえおかきくけこフォントサイズをスタイルで指定している2行目ここはCELL
3行目
4行目
行".$i."長い文章をしれてみて改行がどうなるかを確認してみることにした。どうだろうか????長い文章をしれてみて改行がどうなるかを確認してみることにした。どうだろうか????
行".$i."
"; $pdf->writeHTML($stehtml, true, 0, true, 0); // reset pointer to the last page $pdf->lastPage(); // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // Print a table //Close and output PDF document $pdf->Output("example_002.pdf", "I"); }

ビュー



PDFサンプル



PDFサンプル

サンプル1

サンプル2

example_002は、もとのサンプルソースに2ページ目を独自に追加。

FONTやけどいろいろあるけど、デフォ以外のFONTを埋め込むと当然ファイルサイズがかなりふくれあがった。
要件次第かなぁ

TCPDFについてFONTなどを配布しているサイトがあります。
MONZEN.ORG

カテゴリー: CodeIgniter