TP5 實現HTML/富文本生成PDF文件

1、準備工作

  • 安裝依賴包 MPDF
composer require mpdf/mpdf

2、代碼實現

$content = '123456';
$pdf = new Mpdf();
//參考網站:http://www.thinkphp.cn/code/2127.html
// 設置打印模式
// $pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('作者');
$pdf->SetTitle($title);
$pdf->SetSubject('項目');
$pdf->SetKeywords('關鍵詞,關鍵詞');

// 設置左、上、右的間距
$pdf->SetMargins('10', '10', '10');
// 設置是否自動分頁  距離底部多少距離時分頁
$pdf->SetAutoPageBreak(TRUE, '15');

//開啓字段文字和樣式
$pdf->autoScriptToLang = true;
$pdf->autoLangToFont = true;
$pdf->AddPage();
// 設置字體
$pdf->SetFont('stsongstdlight', '', 14, '', true);

//設置背景
$pdf->SetWatermarkImage('背景圖路徑', 0.1, [230, 180]);
$pdf->showWatermarkImage = true;

//標題
$title = '<h1 style="text-align: center;">' . $title . '</h1><p style="color: grey;font-size: 10px;">版本:1.0</p>
<span style="color: grey;font-size: 10px;">發佈/生效日期:2020 年 8 月 10 日</span><hr style="color: grey;">';

$pdf->WriteHTML($title . $content);
$pdf->Output();

$pdf->Output();返回的內容是pdf格式,裏面的參數可以自行設置
其中要關閉TP5的調試模式,否則會出錯。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章