Home >Backend Development >PHP Tutorial >How to convert html page to pdf file using mpdf in PHP?

How to convert html page to pdf file using mpdf in PHP?

青灯夜游
青灯夜游forward
2020-07-18 15:33:383898browse

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([
    &#39;mode&#39; => &#39;UTF-8&#39;, &#39;format&#39; => &#39;A4&#39;, &#39;default_font_size&#39; => 40, &#39;default_font&#39; => &#39;&#39;, &#39;margin_left&#39; => 20, &#39;margin_right&#39; => 20
  ]);
    $mpdf->autoScriptToLang = true;//支持中文设置 
    $mpdf->autoLangToFont = true;//支持中文设置 
    $mpdf->WriteHTML(&#39;<h1>你好啊,世界!hello word</h1><p style="color: blue;fontsize:14px;">http://www.baidu.com</p>&#39;);
    $path = FILE_UPLOAD.date(&#39;YmdHis&#39;).&#39;_&#39;.mt_rand(1,5).&#39;.pdf&#39;;         
    $mpdf->Output();//直接在页面显示pdf页面内容 
    //$mpdf->Output($path,&#39;f&#39;);//保存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!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete