TCPDFもxajaxと同じようにpluginで組み込む
その他の方法は、wikiか検索すればでてくる
1.ダウンロード
TCPDFをここからダウンロード。PHP5用の他にPHP4用もある
結構更新頻度が高いようでその時の最新のものでいいかな
解凍するとtcpdfフォルダが1個でてくる。
2.ファイルをコピー
xajaxと同じように解凍してでてきた tcpdf/配下を system/plugins/ へコピー。
中にある「example」と「doc」は、コピー先にはいらないので削除。
3.プラグインファイルの作成
system/plugins/に「tcpdf_pi.php」を作成。
ソース
1 2 3 4 5 6 7 8 |
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once('tcpdf/config/lang/jpn.php'); require_once('tcpdf/tcpdf.php'); function tcpdf($orientation='P', $unit='mm', $format='A4', $unicode=true, $encoding='UTF-8', $diskcache=false){ return new TCPDF($orientation, $unit, $format, $unicode, $encoding, $diskcache); } ?> |
4.langファイルの作成
上のプラグインファイルでrequireしている「tcpdf/config/lang/jpn.php」というファイルは、
ダウンロードしたものにはないんで、tcpdf/config/lang/eng.php をリネームして保存。
ソース
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// 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/にあるサンプルコードをコントローラーへ移植。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
<?php class TestTcpdf extends Controller { public function __construct() { parent::controller(); } function index() { $this->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 = "<table border="1" cellspacing="1" cellpadding="1"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>"; $subtable = "<table style="border-collapse: collapse" border="1"><tr><td>a</td><td>b</td></tr><tr><td>c</td><td>d</td></tr></table>"; //$htmltable = "<h2>HTML TABLE:</h2><table border="1" cellspacing="2" cellpadding="2"><tr><th>#</th><th align="right">RIGHT align</th><th align="left">LEFT align</th><th>4A</th></tr><tr><td>1</td><td bgcolor="#cccccc" align="center" colspan="2">A1 ex<i>amp</i>le <a href="http://www.tcpdf.org">link</a> column span. One two tree four five six seven eight nine ten.<br />line after br<br /><small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla<ol><li>first<ol><li>sublist</li><li>sublist</li></ol></li><li>second</li></ol><small color="#FF0000" bgcolor="#FFFF00">small small small small small small small small small small small small small small small small small small small small</small></td><td>4B</td></tr><tr><td>".$subtable."</td><td bgcolor="#0000FF" color="yellow" align="center">A2 ? € € & e è<br/>A2 ? € € & e è</td><td bgcolor="#FFFF00" align="left"><font color="#FF0000">Red</font> Yellow BG</td><td>4C</td></tr><tr><td>1A</td><td rowspan="2" colspan="2" bgcolor="#FFFFCC">2AA<br />2AB<br />2AC</td><td bgcolor="#FF0000">4D</td></tr><tr><td>1B</td><td>4E</td></tr><tr><td>1C</td><td>2C</td><td>3C</td><td>4F</td></tr></table>"; $htmltable = "<h2>HTML TABLE:</h2><table style="border-collapse:collapse" border="1"><tr><th>#</th><th align="right">RIGHT align</th><th align="left">LEFT align</th><th>4A</th></tr><tr><td>1</td><td bgcolor="#cccccc" align="center" colspan="2">A1 ex<i>amp</i>le <a href="http://www.tcpdf.org">link</a> column span. One two tree four five six seven eight nine ten.<br />line after br<br /><small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla<ol><li>first<ol><li>sublist</li><li>sublist</li></ol></li><li>second</li></ol><small color="#FF0000" bgcolor="#FFFF00">small small small small small small small small small small small small small small small small small small small small</small></td><td>4B</td></tr><tr><td>".$subtable."</td><td bgcolor="#0000FF" color="yellow" align="center">A2 ? € € & e è<br/>A2 ? € € & e è</td><td bgcolor="#FFFF00" align="left"><font color="#FF0000">Red</font> Yellow BG</td><td>4C</td></tr><tr><td>1A</td><td rowspan="2" colspan="2" bgcolor="#FFFFCC">2AA<br />2AB<br />2AC</td><td bgcolor="#FF0000">4D</td></tr><tr><td>1B</td><td>4E</td></tr><tr><td>1C</td><td>2C</td><td>3C</td><td>4F</td></tr></table>"; // output the HTML content $pdf->writeHTML($htmltable, true, 0, true, 0); // Print some HTML Cells $cellcontent = "<span color="red">red</span> <span color="green">green</span> <span color="blue">blue</span><br /><span color="red">red</span> <span color="green">green</span> <span color="blue">blue</span>"; $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 = "<br><div>" . "<table style="border-collapse:separate" border="1">" . "<tr><td>abcdefg@a1234567890qwertyuio.comashshshfvjdbvjdhfbjbvcdjhbvjhbvjdhbf</td><td>名前(1行目)</td><td>草彅</td><td rowspan="2">ここは2行連結</td></tr>" . "<tr><td rowspan="3"><span style="font-size:8px">ここは3行連結あいうえおかきくけこフォントサイズをスタイルで指定している</span></td><td>2行目</td><td>ここはCELL</td></tr>" . "<tr><td colspan="3">3行目</td></tr>" . "<tr><td colspan="3">4行目</td></tr>"; for ($i=5;$i<=55;$i++) { if ($i == 21) { $stehtml .= "<tr><td colspan="4">行".$i."長い文章をしれてみて改行がどうなるかを確認してみることにした。どうだろうか????長い文章をしれてみて改行がどうなるかを確認してみることにした。どうだろうか????</td></tr>"; } else { $stehtml .= "<tr><td colspan="4">行".$i."</td></tr>"; } } $stehtml .= "</table></div>"; $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"); } |
ビュー
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<html> <head> <title>PDFサンプル</title> </head> <body> <h1>PDFサンプル</h1> <p><a href="testtcpdf/example_001" target="_blank">サンプル1</a></p> <p><a href="testtcpdf/example_002" target="_blank">サンプル2</a></p> </body> </html> |
example_002は、もとのサンプルソースに2ページ目を独自に追加。
FONTやけどいろいろあるけど、デフォ以外のFONTを埋め込むと当然ファイルサイズがかなりふくれあがった。
要件次第かなぁ
TCPDFについてFONTなどを配布しているサイトがあります。
MONZEN.ORG