How to Generate PDF files using HTML in PHP

Source: Internet
Author: User
Tags wkhtmltopdf
This article mainly introduces how to use PHP to generate pdf files in HTML. in linux, HTML2FPDF and wkhtmltoimage can be used to generate pdf files directly from html pages, for more information about how to use HTML2FPDF and wkhtmltoimage in linux to directly generate a pdf file on the webpage html. The specific implementation method is as follows:

I found an open-source program developed on the basis of FPDF and HTML2FPDF source code. It basically solves the problem of garbled characters in Chinese (as well as Japanese, Korean, and Southeast Asia and global languages). It is available in Windows/Linux development environment and does not need to be installed with other components, it is a good news for website developers without VPS and independent servers.

Not much said, the source code name is MPDF, the official address is: http://www.mpdf1.com/has been updated to version 5.6.
Download it from the official website, decompress it to the website directory, and use it.

The code is as follows:

<? Php
Include ('mpdf. php ');
$ Mpdf = new mPDF ('utf-8', 'A4 );
$ Mpdf-> useAdobeCJK = true;
$ Mpdf-> SetAutoFont (AUTOFONT_ALL );
$ Mpdf-> SetDisplayMode ('fullpage ');
// $ Mpdf-> watermark_font = 'GB ';
// $ Mpdf-> SetWatermarkText ('Chinese watermark', 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 ');
// $ Mpdf-> output('tmpupload', true );
// $ Mpdf-> output('tmpupload', 'D ');
// $ Mpdf-> Output ();
Exit;
?>


PASS: it should be noted that the php file is a UTF-8. You don't have to worry about the code posted by the sky. when you write a box with garbled text, you can go to config. change $ this-> useAdobeCJK = false in the PHP file to true or Mark $ mpdf-> useAdobeCJK = true. if you have any official documents, you can find them on your own.

This is not a perfect method. MPDF can only parse relatively simple css (it cannot parse ul li and other labels in js and css ). Now that the wkhtmltoimage extension is used, you do not care about the use of wkhtmltopdf.
Similarly, wkhtmltoimage 0.11 may cause an error. you need to download Version 0.10.0 _ rc2.

32-bit:

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

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
Music wkhtmltoimage-amd64/usr/bin/wkhtmltopdf test wkhtmltopdf http: // www. your domain name. com/your domain name php 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 ');

I hope this article will help you with php programming.

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.