PHP implementation of HTML generated PDF file method _php Tips

Source: Internet
Author: User
Tags bz2 wkhtmltopdf

The example of this article describes the use of Html2fpdf and Wkhtmltoimage in Linux to directly generate the HTML Web page PDF format File method, share for everyone to reference. The implementation methods are as follows:

Find a fpdf and Html2fpdf source based on the development of a set of open source programs, the author is very strong. The basic solution of the Chinese (as well as Japanese, Korean and Southeast Asia and the global language) garbled problem, in the Windows/linux development environment test available, do not need to install other component support, is not a VPS and independent server Web site developers of the Gospel.

Not much said, the source name is Mpdf, the official address is: http://www.mpdf1.com/has been updated to version 5.6.
To the official website to download, extract to the site directory, use.

Copy Code code as follows:
<?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 (' China Watermark ', 0.1);
$url = ' http://www. your domain name. com/';
$strContent = file_get_contents ($url);
Print_r ($strContent);d ie;
$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 should be UTF-8. Do not have the mood the sky pastes out the code, oneself writes when the square garbled, may in the config.php file $THIS-&GT;USEADOBECJK = false; modified to true or must be annotated: $mpdf-&GT;USEADOBECJK = true; The official has the document, may grope for oneself.

This is not the perfect method, Mpdf can only parse relatively simple CSS (can not parse JS and CSS in the UL Li and other tags). Now that you have used the wkhtmltoimage extension, you don't care about the use of wkhtmltopdf.
Similarly, Wkhtmltoimage 0.11 will have an error and need to download the 0.10.0_RC2 version.

32-bit:

Copy Code code 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 code 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. 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. pdf ');

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.