The most efficient way to generate PDF files in PHP, HTML to PDF file method

Source: Internet
Author: User
Tags pdflib php class wkhtmltopdf

There was a client who needed to generate a PDF of some HTML pages, and then I looked for classes that used PHP to surround HTML pages into PDF files. The method is to find a lot of, what html2pdf,pdflib,fpdf these have tried, but have not reached my request.

Pdflib,fpdf These two methods are required to write a program to generate PDF, it is also not supported directly to convert HTML page to pdf;html2pdf this although the HTML page can be converted to PDF file, but it can only convert the general simple HTML code, If your HTML content is going to be formatted with a background news editor, that's certainly not possible.

Tangled half a day, what Baidu, Google search has been used, search for a half-day, Kung Fu is not a conscientious, finally found a very useful method, the following is a grand introduction.

It is: Wkhtmltopdf,wkhtmltopdf can directly convert any Web page that can be browsed in the browser into a PDF, first of all, it is not a PHP class, but a conversion of HTML page into a PDF of the software, But it is not a simple desktop software, and it is directly cmd batch-processed. and PHP has a shell_exec () function. Here's a step-by-step procedure for how to use PHP to generate PDF files.

One, download and install PDF
: http://code.google.com/p/wkhtmltopdf/downloads/list
There are various platforms installed under the installation package, English is not good direct Google translation. The following example is used on the Windows platform, my download is wkhtmltopdf-0.9.9-installer.exe this version, I win7 the 32-bit 64-bit and Windows 2003 installation test is not a problem. Download good after installation directly, note the installation path to know, the following will be used.
After the installation of the system environment variable variable named "Path" after the addition:; C:Program Files (x86) wkhtmltopdf is the directory you installed. Reboot the computer after installation.

Second, the test use effect
Input directly in cmd: Wkhtmltopdf http://www.shwzzz.cn/F:website1.pdf
The first one is: Run the software name (this is constant) the second is the URL the third is the generated path and file name. After the carriage return is not to see a build progress bar prompt, congratulations you have succeeded, to your build directory to see if there is a newly generated PDF file.

Third, PHP calls
PHP call is very simple, with shell_exec This function can be, if the shell_exec function cannot be used to see if the php.ini is disabled.
Example: <?php shell_exec ("Wkhtmltopdf http://www.shwzzz.cn/1.pdf")?>

Third, solve the paging problem
Wkhtmltopdf is very useful, but it is also somewhat unsatisfactory. is when an HTML page is a long I need to be paged in the specified place. Wkhtmltopdf developers did not take this into account at the time of development,
For example, the following HTML page:

<! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "HTTP// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 

When I create a PDF, I want each block to be a page, after a page of many debugging PDFs is about 1362px, but the more backward the value is wrong, it is not yet known how many pixels of the PDF page.

But Wkhtmltopdf has a good way to add one after that Div's style: page-break-inside:avoid; it's OK.

<! doctype html public  "-//w3c//dtd xhtml 1.0 transitional//en"   "HTTP// Www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> 


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.