以PHP而言第一個聯想到的一定是FPDF,但是FPDF不支援UTF8,所以如果PDF中要顯示中文還要下載另一個檔案。TCPDF支援UTF8且支援輸出BARCODE,所以非英語系國家用它來做開發再適合不過。
首先當然要先下載TCPDF並安裝,這個不說了,有問題請再告訴我。
下載完後將檔案放在網頁專案中(我的在C:\www\PDF),然後開啟伺服器,TCPDF中有很多測試檔讓我們參考。本文說明怎麼把網頁參數帶入得到一個PDF檔,所以這種伺服器檔案位置放置的問題不研究太久。以下說明正文,開啟檔案(examples_038.php支援東亞輸出),這個檔案輸出日文"こんにちは",我在本文中皆以該檔為基礎改成我要的,如果您需要改字型的話請參考這裡,而因為該檔中有呼叫檔頭檔尾,如果需要變成自己的檔頭檔位的話請改tcpdf_config.php第42行以下相關設定,如下:
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // set document information $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Shih Yu Chung'); $pdf->SetTitle('Blog TCPDF toturial'); $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); // set header and footer fonts $pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); $pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); // set default monospaced font $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); //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 $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM); //set image scale factor $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); //set some language-dependent strings $pdf->setLanguageArray($l); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0', 'U', 20); // add a page $pdf->AddPage(); // print a line using Cell() $pdf->Cell(0, 10, '測試中文施囿仲', 1, 1, 'C'); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example_038.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

設定完以後開始進入說明。
1. 建構物件
TCPDF __construct(
[string
$orientation = 'P'], [string
$unit = 'mm'], [mixed
$format = 'A4'], [boolean
$unicode = true], [String
$encoding = 'UTF-8'], [boolean
$diskcache = false])
1. $orientation ==>P:橫向輸出 L:直向 2. $unit ==> 單位 3. $format ==> 紙張格式 4. $unicode ==> 多國語言支援 5. $encoding ==> 編碼方式 6. $diskcache ==> 支援快取 所有預設值皆在tcpdf_config.php有定義我的第一個PDF。
STEP1 : SETFONT : 設定字型
STEP2 : ADDPAGE : 增加頁(自動呼叫OPEN())
STEP3 : WRITE : 寫入頁
STEP4 : OUTPUT : 輸出頁(自動呼叫CLOSE())
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0','',20); //取消header $pdf->setPrintHeader(false); //取消footer $pdf->setPrintFooter(false); // add a page $pdf->AddPage(); // print a line using Cell() $pdf->write(10,"我成功啦~~~~~~~~~~~"); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

2. Write 這裡說明文字輸出、輸入
mixed Write( float $h, string $txt, [mixed $link = ''], [int $fill = 0], [string $align = ''], [boolean $ln = false], [int $stretch = 0], [boolean $firstline = false], [boolean $firstblock = false], [float $maxh = 0])
float $h Line 行高string $txt 列印的字串
mixed $link URL或傳入AddLink()返回值
int $fill 說明背景是否填滿顏色
(1) 填滿
(0) 預設,不填滿: 0.
string $align 文字串定位.
Possible values are:
* L: 靠左對齊 (default value)
* C: 置中
* R: 靠右
* J: 排版
boolean $ln 如果為1則新的字串接在前字串後,否則另開新行,如同在輸入字串後打上ENTER.
int $stretch 拉伸字串
mode:
* 0 = 不可
* 1 = 如果需要水平拉伸
* 2 = 強制水平拉伸
* 3 = 如果需要調整字元間距
* 4 = 強制調整字元間距
boolean $firstline if true prints only the first line and return the remaining string.
boolean $firstblock if true the string is the starting of a line.
float $maxh maximum height. The remaining unprinted text will be returned. It should be >= $h and less then remaining space to the bottom of the page, or 0 for disable this feature. 官方文件看此,但是看一下範例及結果可能會較快:
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0','',20); //取消header $pdf->setPrintHeader(false); //取消footer $pdf->setPrintFooter(false); // add a page $pdf->AddPage(); // print a line using Cell() $pdf->write(10,"第一行\n","http://www.google.com.tw","","C","","2"); $pdf->write(10,"第二行","","0","R"); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

3. GetX()、GetY() 輸出當前游標座標值(輸出FLOAT)
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0','',20); //取消header $pdf->setPrintHeader(false); //取消footer $pdf->setPrintFooter(false); // add a page $pdf->AddPage(); // print a line using Cell() $x = $pdf->GetX(); $y = $pdf->GetY(); $str = "[x=".$x.";y=".$y."]\n"; $pdf->Write(10,$str); $x = $pdf->GetX(); $y = $pdf->GetY(); $str = "[x=".$x.";y=".$y."]"; $pdf->Write(10,$str); $x = $pdf->GetX(); $y = $pdf->GetY(); $str = "[x=".$x.";y=".$y."]"; $pdf->Write(10,$str); $pdf->Output(); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

4. SetXY()、SETX()、SETY()
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0','',20); //取消header $pdf->setPrintHeader(false); //取消footer $pdf->setPrintFooter(false); // add a page $pdf->AddPage(); // print a line using Cell() $pdf->SetXY(15.0, 15.0); $pdf->Write(10,'第一列'); $pdf->SetXY(30.0, 30.0); $pdf->Write(10,'第二列'); $pdf->SetXY(45.0, 45.0); $pdf->Write(10,'第三列'); $pdf->Output(); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

5. SetFontSize()、ln() : SetFontSize設定字串大小、ln換行
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0','',20); //取消header $pdf->setPrintHeader(false); //取消footer $pdf->setPrintFooter(false); // add a page $pdf->AddPage(); $pdf->Write(10,'第一列'); $pdf->Ln(); $pdf->SetFontSize(24); $pdf->Write(10,'第二列'); $pdf->Ln(); $pdf->SetFontSize(28); $pdf->Write(10,'第三列'); $pdf->Output(); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

6. SetTextColor() : 設定字元顏色,參數只有一個時為Gray Scale值,參數有三個時為(R,G,B)值
<?php require_once('/tcpdf/config/lang/eng.php'); require_once('/tcpdf/tcpdf.php'); // create new PDF document $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // --------------------------------------------------------- // set font $pdf->SetFont('arialunicid0','',20); //取消header $pdf->setPrintHeader(false); //取消footer $pdf->setPrintFooter(false); // add a page $pdf->AddPage(); //############################################################# $pdf->SetTextColor(220, 20, 60); $pdf->Write(10,'第一列'); $pdf->Ln(); $pdf->SetTextColor(0, 191, 255); $pdf->Write(10,'第二列'); $pdf->Ln(); $pdf->SetTextColor(64); $pdf->Write(10,'第三列'); $pdf->Ln(); $pdf->SetTextColor(128); $pdf->Write(10,'第四列'); $pdf->Ln(); $pdf->SetTextColor(192); $pdf->Write(10,'第五列'); $pdf->Output(); // --------------------------------------------------------- //Close and output PDF document $pdf->Output('example.pdf', 'I'); //============================================================+ // END OF FILE //============================================================+ ?>

待續 update : 2010/3/31
沒有留言:
張貼留言