Home >Backend Development >PHP Tutorial >How to convert html page to pdf file using mpdf in PHP?
The main requirement of this article is to convert html pages into pdf documents
1. Install the mpdf tool class (composer mode)
composer require mpdf/mpdf:8.0.3
2. Call
<?php public function test_pdf(){ //$mpdf = new \Mpdf\Mpdf(); $mpdf = new \Mpdf\Mpdf([ 'mode' => 'UTF-8', 'format' => 'A4', 'default_font_size' => 40, 'default_font' => '', 'margin_left' => 20, 'margin_right' => 20 ]); $mpdf->autoScriptToLang = true;//支持中文设置 $mpdf->autoLangToFont = true;//支持中文设置 $mpdf->WriteHTML('<h1>你好啊,世界!hello word</h1><p style="color: blue;fontsize:14px;">http://www.baidu.com</p>'); $path = FILE_UPLOAD.date('YmdHis').'_'.mt_rand(1,5).'.pdf'; $mpdf->Output();//直接在页面显示pdf页面内容 //$mpdf->Output($path,'f');//保存pdf文件到指定目录 } ?>
3 in the controller. The effect is as shown below
Recommended related tutorials : "PHP Tutorial"
The above is the detailed content of How to convert html page to pdf file using mpdf in PHP?. For more information, please follow other related articles on the PHP Chinese website!