PHP Realization of HTML Generating PDF File Method

  • 2021-07-26 07:17:10
  • OfStack

This article describes the use of linux HTML2FPDF and wkhtmltoimage web page html directly generated pdf format file method, shared for your reference. The specific implementation method is as follows:

Find a set of open source programs developed on the basis of FPDF and HTML2FPDF source code, and the author is very powerful. It basically solves the problem of garbled Chinese (as well as Japanese, Korean and Southeast Asian and global languages). It can be tested and available in Windows/Linux development environment without installing other components, which is a boon for website developers without VPS and independent servers.

Needless to say, the source code name is MPDF, and the official address is: http://www.mpdf1.com/has been updated to version 5.6.
Download to official website, unzip to website directory, and use.

<?php
include('mpdf.php');
$mpdf=new mPDF('UTF-8','A4','','',15,15,44,15);
$mpdf->useAdobeCJK = true;
$mpdf->SetAutoFont(AUTOFONT_ALL);
$mpdf->SetDisplayMode('fullpage');
//$mpdf->watermark_font = 'GB';
//$mpdf->SetWatermarkText(' Chinese watermarking ',0.1);
$url = 'http://www. Your domain name .com/';
$strContent = file_get_contents($url);
//print_r($strContent);die;
$mpdf->showWatermarkText = true;
$mpdf->SetAutoFont();
//$mpdf->SetHTMLHeader( ' Head ' );
//$mpdf->SetHTMLFooter( ' Bottom ' );
$mpdf->WriteHTML($strContent);
$mpdf->Output('ss.pdf');
//$mpdf->Output('tmp.pdf',true);
//$mpdf->Output('tmp.pdf','d');
//$mpdf->Output();
exit;
?>

PASS: Note that the PHP file is UTF-8. Don't use the mood sky posted code, when writing their own grid garbled, can be in config. php file $this- > useAdobeCJK = false; Modified to true or must be marked: $mpdf- > useAdobeCJK = true; There are official documents, so you can explore them by yourself.

In this imperfect way, MPDF can only resolve the relatively simple css (it cannot resolve tags such as ul and li in js and css). Now that you are using the wkhtmltoimage extension, you don't care about the use of wkhtmltopdf.
Similarly, an error occurs with wkhtmltoimage 0.11 and you need to download version 0.10.0_rc2.

32-bit:

wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2
tar jxf wkhtmltopdf-0.10.0_rc2-static-i386.tar.bz2
cp wkhtmltoimage-i386 /usr/local/bin/wkhtmltopdf


64-bit
wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.10.0_rc2-static-amd64.tar.bz2
mv wkhtmltoimage-0.10.0_rc2-static-amd64.tar.bz2 wkhtmltoimage-0.10.0_rc2-static-amd64.tar
tar -xvf wkhtmltopdf-0.10.0_rc2-static-amd64.tar
mv wkhtmltoimage-amd64 /usr/bin/wkhtmltopdf Test wkhtmltopdf http://www. Your domain name .com/ Your domain name .pdfphp shell_exec() Execute shell_exec('/usr/local/bin/wkhtmltopdf http://www. Your domain name .com/ /usr/local/wwwroot/ Your domain name .com/ Your domain name .pdf');

I hope this article is helpful to everyone's php programming.


Related articles: