PHP implements the method of generating PDF files from HTML,
The example in this article describes the method of using HTML2FPDF and wkhtmltoimage to directly generate pdf format files from web page html in Linux. It is shared with everyone for your reference. The specific implementation method is as follows:
I found an open source program developed based on FPDF and HTML2FPDF source code. The author is very good. It basically solves the problem of garbled characters in Chinese (as well as Japanese, Korean, Southeast Asian and global languages). It can be tested in Windows/Linux development environment and does not require the installation of other component support. It is good news for website developers who do not have VPS and independent servers.
Without further ado, the source code name is MPDF, and the official address is: http://www.mpdf1.com/ It has been updated to version 5.6.
Download it from the official website, unzip it to the website directory, and use it.
Copy code The code is as follows:
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('China Watermark',0.1);
$url = 'http://www.yourdomain.com/';
$strContent = file_get_contents($url);
//print_r($strContent);die;
$mpdf->showWatermarkText = true;
$mpdf->SetAutoFont();
//$mpdf->SetHTMLHeader( 'Header' );
//$mpdf->SetHTMLFooter( 'Bottom' );
$mpdf->WriteHTML($strContent);
$mpdf->Output('ss.pdf');
//$mpdf->Output('tmp.pdf',true);
//$mpdf->Output('tmp.pdf','d');
//$mpdf->Output();
exit;
?>
PASS: It should be noted that the PHP file must be UTF-8. Don’t use the code posted by Mood Sky. If the squares are garbled when you write it yourself, you can change $this->useAdobeCJK = false; in the config.php file to true or you must mark: $mpdf->useAdobeCJK = true; There are official documents, you can explore it yourself.
With this imperfect method, MPDF can only parse relatively simple css (it cannot parse tags such as ul li in js and css). Now that we have used the wkhtmltoimage extension, we don't care about the use of wkhtmltopdf.
Similarly, wkhtmltoimage 0.11 will cause errors and you need to download version 0.10.0_rc2.
32-bit:
Copy code The code is as follows:
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
Copy code The code is as follows:
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.yourdomain.com/ yourdomain.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 will be helpful to everyone’s PHP programming design.
http://www.bkjia.com/PHPjc/907840.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/907840.htmlTechArticleHow PHP implements HTML to generate PDF files. This article describes the use of HTML2FPDF and wkhtmltoimage to directly convert web page html in Linux. The method of generating files in pdf format is shared with everyone for reference...