Previously, a customer needed to generate PDF files for some html pages, and then I found some classes that used php to enclose html pages into PDF files. The method is to find a lot of things, such as html2pdf, pdflib, and FPDF, but none of them meet my requirements.
Pdflib and FPDF are two methods that need to be programmed to generate pdf. In other words, html pages cannot be directly converted to pdf; html2pdf can convert html pages into pdf files, but it can only convert simple html code, if you want html content to be typeset through the background News Editor, it will certainly not work.
After a long time of struggle, Baidu and Google search are all used. After a long time of searching, I am very hard to find a very useful method. The following is a grand introduction.
It is: wkhtmltopdf. wkhtmltopdf can directly convert any web page that can be viewed in a browser into a pdf. First, it indicates that it is not a php class, it is a software that converts html pages into pdf files, but it is not a simple desktop software, and it is directly processed by cmd batch. In addition, php has a shell_exec () function. The following describes how to use php to generate a PDF file.
1. Download and install pdf
: Http://code.google.com/p/wkhtmltopdf/downloads/list
There are installation packages installed on various platforms. If the English language is not good, Google Translate Them. The following uses windows as an example. My download is wkhtmltopdf9.9-installer.exe. I have no problem installing and testing windows 2003 and windows 32-bit 64-bit windows. After downloading the package, you can install it directly. Pay attention to the installation path, which will be used below.
After installation, you must add:; C: Program Files (x86) wkhtmltopdf after the system environment variable named "Path", which is your installation directory. Restart your computer after installation.
Ii. test the effect
Enter wkhtmltopdf http://www.shwzzz.cn/F: website1.pdf directly in cmd
The first is: the name of the running software (which remains unchanged), the second is the URL, and the third is the generated path and file name. After you press enter, do you want to see the prompt for generating a progress bar? Congratulations, you have succeeded. Go to your generated directory and check if there is a generated PDF file.
3. Calls in php
Php calls are simple. You can use the shell_exec function. If the shell_exec function cannot be used, check whether php. ini has been disabled.
Example: <? Php shell_exec ("wkhtmltopdf http://www.shwzzz.cn/1.20.")?>
3. Solve paging Problems
Wkhtmltopdf is easy to use, but it is somewhat unsatisfactory. That is, when an html page is long and I need to pagination in the specified place, what should I do? Wkhtmltopdf developers did not consider this point during 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"> However, wkhtmltopdf has a good method, that is, to add a: page-break-inside: avoid after the div style.
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Source: http://www.shwzzz.cn/news/xuetang/159.html