1. 安裝

使用 composer 安裝即可,在 packagist 的地址是 :https://packagist.org/packages/tecnickcom/tcpdf

作者 tecnickcom 各種和 tcpdf 相關的包都有:https://packagist.org/packages/tecnickcom/

 

composer require tecnickcom/tcpdf

2. 使用方法

tcpdf 官網很多例子,可以直接用,比如 writeHtml() 就有專門的例子說明:

https://tcpdf.org/examples/example_006/

3. 範例代碼

        $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// set document information
        $pdf->SetCreator(PDF_CREATOR);
        $pdf->SetAuthor('Benny');
        $pdf->SetTitle('Benny Title');

        $pdf->setPrintHeader(false);
        $pdf->setPrintFooter(false);

// set default monospaced font
        $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
        $pdf->SetMargins(10, 10, 10);
        $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);

// Add a page
// This method has several options, check the source code documentation for more information.
        $pdf->AddPage();

// set default font subsetting mode
        $pdf->setFontSubsetting(true);
// Set font
// dejavusans is a UTF-8 Unicode font, if you only need to
// print standard ASCII chars, you can use core fonts like
// helvetica or times to reduce file size.
//        $pdf->SetFont('msyh', '', 11, '', true);

// set text shadow effect
        $pdf->setTextShadow(array('enabled' => false));
// Set some content to print
        $html = '
<style>
input{
border:0;
border-bottom: 1px solid #cccccc;
padding:5px;
}
</style>
<div >
<h1>Test PDF</h1>
<input type="text" name="name" value="" size="20" maxlength="30" style="border:0;border-bottom: 1px;" />
<table cellspacing="0" border="1px"> 
  <tbody>
    <tr>
        <td>td1</td>
        <td>td2</td>
        <td>td3</td>
    </tr> 
</tbody>
</table>
</div> 
<div style=" margin-top: 50px">
<figure data-media-type="photo">
<div class="photo-container">
<img class="photo" src="https://images.chinatimes.com/newsphoto/2022-06-08/1024/20220608001529.jpg" alt="三上悠亞日前宣布單飛建立新公司。(圖/翻攝自yua_mikami IG" width="1024" height="670">
</div>
<br/>
<figcaption>Image/taken from yua_mikami IG</figcaption>
</figure>
</div>
';
// Print text using writeHTMLCell()
// $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
// output the HTML content
        $pdf->writeHTML($html, true, false, true, false, '');
// reset pointer to the last page
        $pdf->lastPage();
// ---------------------------------------------------------
// Close and output PDF document
// This method has several options, check the source code documentation for more information.
        $pdf->Output('example_001.pdf', 'D');
//============================================================+
// END OF FILE
//============================================================+

 

4. 生成結果

5. 問題

[遇到亂碼]

可使用

     $pdf->SetFont('stsongstdlight', '', 12);

解決

 

文章標籤
全站熱搜
創作者介紹
創作者 benny 的頭像
benny

Benny的網站開發筆記

benny 發表在 痞客邦 留言(0) 人氣(0)