本文主要需求是将html页面转换为pdf文档
1、安装mpdf工具类 (composer方式)
composer require mpdf/mpdf:8.0.3
2、控制器里调用
<?phppublic 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、效果如下图
相关教程推荐:《php教程》
以上就是php如何使用mpdf将html页面转换pdf文件?的详细内容。