Home  >  Article  >  Backend Development  >  How to convert html to pdf in php

How to convert html to pdf in php

王林
王林Original
2021-09-28 15:57:485957browse

php method to convert html to pdf: [function genBillPdfContent($htmlContent) {$mpdf = new \Mpdf\Mpdf();$mpdf->SetDisplayMode('fullpa...].

How to convert html to pdf in php

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn