Laravel 使用TCPDF生成PDF文件
阿新 • • 發佈:2019-02-19
上次,簡單的提了使用PHPWord生成Word文件,這次,就簡單的講下tcpdf生成PDF文件。效果圖如下:
首先,引入tcpdf類庫,使用composer
composer require tecnickcom/tcpdf
程式碼示例 :
// 設定文件資訊
$pdf->SetCreator('Hello world');
$pdf->SetAuthor('dyk');
$pdf->SetTitle('TCPDF示例');
$pdf->SetSubject('TCPDF示例' );
$pdf->SetKeywords('TCPDF, PDF, PHP');
// 設定頁首和頁尾資訊
$pdf->SetHeaderData('tcpdf_logo.jpg', 30, 'www.marchsoft.cn', '三月軟體!', [0, 64, 255], [0, 64, 128]);
$pdf->setFooterData([0, 64, 0], [0, 64, 128]);
// 設定頁首和頁尾字型
$pdf->setHeaderFont(['stsongstdlight' , '', '10']);
$pdf->setFooterFont(['helvetica', '', '8']);
// 設定預設等寬字型
$pdf->SetDefaultMonospacedFont('courier');
// 設定間距
$pdf->SetMargins(15, 15, 15);//頁面間隔
$pdf->SetHeaderMargin(5);//頁首top間隔
$pdf->SetFooterMargin(10);//頁尾bottom間隔
// 設定分頁
$pdf->SetAutoPageBreak(true, 25);
// 設定自動換頁
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 設定影象比例因子
$pdf->setImageScale(1.25);
// 設定預設字型構造子集模式
$pdf->setFontSubsetting(true);
// 設定字型 stsongstdlight支援中文
$pdf->SetFont('stsongstdlight', '', 14);
// 新增一頁
$pdf->AddPage();
$pdf->Ln(5);//換行符
$html = '
<table width="400" border="1">
<tr>
<th align="left">消費專案</th>
<th align="right">一月</th>
<th align="right">二月</th>
</tr>
<tr>
<td align="left">衣服</td>
<td align="right">$241.10</td>
<td align="right">$50.20</td>
</tr>
<tr>
<td align="left">化妝品</td>
<td align="right">$30.00</td>
<td align="right">$44.45</td>
</tr>
<tr>
<td align="left">食物</td>
<td align="right">$730.40</td>
<td align="right">$650.00</td>
</tr>
<tr>
<th align="left">總計</th>
<th align="right">$1001.50</th>
<th align="right">$744.65</th>
</tr>
</table>
';
$pdf->writeHTML($html, true, false, true, false, '');
//輸出PDF
$pdf->Output('t.pdf', 'I');//I輸出、D下載
有興趣的話,可以深入的瞭解一下