Home > Article > Backend Development > How to convert html to pdf in php
php method to convert html to pdf: [function genBillPdfContent($htmlContent) {$mpdf = new \Mpdf\Mpdf();$mpdf->SetDisplayMode('fullpa...].
The operating environment of this article: windows10 system, php 7, thinkpad t480 computer.
There are many ways to convert html to pdf, such as ours You can use methods such as html2pdf, mpdf, dompdf and tcpdf. We can use composer to install these four open source libraries, but you may encounter slow network transmission, installation timeout, etc. during the installation process. So it is best to use Code Cloud Download and install from the source on.
Although the above four methods can convert html to pdf, mpdf is the best way from the perspective of conversion effect. Of course, this method is not perfect. , mpdf does not support automatic paging of complex tables. During use, we may need to manually disassemble the content in the html, generate multiple html templates, and finally generate multiple single-page PDF files.
mpdf usage example:
/** * @param $htmlContent html文件内容 * @return string 返回生成的PDF文件内容 */ function genBillPdfContent($htmlContent) { $mpdf = new \Mpdf\Mpdf(); $mpdf->SetDisplayMode('fullpage'); $mpdf->autoScriptToLang = true; $mpdf->autoLangToFont = true; $mpdf->WriteHTML($htmlContent); return $mpdf->Output('filename.pdf', \Mpdf\Output\Destination::STRING_RETURN); }
Recommended learning: php training
The above is the detailed content of How to convert html to pdf in php. For more information, please follow other related articles on the PHP Chinese website!