Home > Article > Backend Development > How to convert html to pdf file using php
How to use php to convert html to pdf files: first download and install pdf; then test the effect; then use the "shell_exec" function to call it in php; and finally solve the paging problem.
#There was a customer who needed to generate pdf files from some html pages, and then I looked for some classes that use php to convert html pages into pdf files. It can be said that I searched a lot for methods. I tried html2pdf, pdflib, and FPDF, but none of them met my requirements.
pdflib, FPDF These two methods require writing programs to generate pdf, which means that they do not support directly converting html pages into pdf; although html2pdf can convert html pages into pdf files, it only It can convert ordinary simple html codes. If your html content needs to be typeset through the background news editor, it will definitely not work.
After struggling for a long time, I searched on Baidu and Google. After searching for a long time, I finally found a very easy-to-use method. I will introduce it here.
It is: wkhtmltopdf, wkhtmltopdf can directly convert any web page that can be viewed in the browser into a pdf. First of all, let me explain that it is not a php class, but a html page that converts html pages into pdf. It is a software, but it is not a simple desktop software, and it directly performs cmd batch processing. And php has a shell_exec() function. The following is a step-by-step introduction on how to use php to generate pdf files.
1. Download and install pdf
Download address: http://code.google.com/p/wkhtmltopdf/downloads/list
There are installation packages for various platforms, in English If it’s not good, just Google translate it. The following is an example of using it on the windows platform. I downloaded the wkhtmltopdf-0.9.9-installer.exe version. I installed and tested it on win7 32-bit 64-bit and windows 2003 without any problems. Just install it directly after downloading. Pay attention to the installation path, which will be used below.
After installation, you need to add after the system environment variable named "Path":;C:Program Files (x86)wkhtmltopdf, which is the directory where you installed it. After installation, restart your computer.
Second, test the usage effect
Input directly in cmd: wkhtmltopdf http://www.shwzzz.cn/ F:website1.pdf
The first one is: running software name (this is unchanged) The second is the URL and the third is the generated path and file name. After pressing Enter, do you see a prompt of a generation progress bar? Congratulations on your success. Go to your generation directory to see if there is a newly generated pdf file.
3. Calling in php
Calling in php is very simple. Just use the shell_exec function. If the shell_exec function cannot be used, check whether it is disabled in php.ini.
Example: b2c7ff02e5174be99b172ec4ea3db754
Three, solve the paging problem
wkhtmltopdf is very useful. But there are also some unsatisfactory aspects. What should I do if an html page is very long and I need to paginate it at a specified place? The developers of wkhtmltopdf did not take this into consideration when developing,
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"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>pdf</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <style type="text/css"> *{ margin:0px; padding:0px;} div{ width:800px; height:1362px;margin:auto;} </style> <body> <div style=" background:#030"></div> <div style=" background:#033"></div> <div style=" background:#369"></div> <div style=" background:#F60"></div> <div style=" background:#F3C"></div> <div style=" background:#F0F"></div> <div style=" background:#0FF"></div> <div style=" background:#FF0"></div> <div style=" background:#00F"></div> <div style=" background:#0F0"></div> <div style=" background:#033"></div> <div style=" background:#369"></div> <div style=" background:#F60"></div> <div style=" background:#030"></div> <div style=" background:#033"></div> <div style=" background:#369"></div> <div style=" background:#F60"></div> <div style=" background:#F3C"></div> <div style=" background:#F0F"></div> <div style=" background:#0FF"></div> <div style=" background:#FF0"></div> <div style=" background:#00F"></div> <div style=" background:#0F0"></div> </body> </html>
When I generate it into pdf, I want each block to be one page, After countless debuggings, one page of PDF is about 1362px, but the value becomes wrong the further it goes. We still don’t know how many pixels one page of PDF is.
But wkhtmltopdf has a good method, which is to add a page-break-inside:avoid; after the p style, and it will be ok.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>pdf</title> <link href="css/style.css" rel="stylesheet" type="text/css" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <style type="text/css"> *{ margin:0px; padding:0px;} div{ width:800px; min-height:1362px;margin:auto;page-break-inside:avoid;} </style> <body> <div style=" background:#030"></div> <div style=" background:#033"></div> <div style=" background:#369"></div> <div style=" background:#F60"></div> <div style=" background:#F3C"></div> <div style=" background:#F0F"></div> <div style=" background:#0FF"></div> <div style=" background:#FF0"></div> <div style=" background:#00F"></div> <div style=" background:#0F0"></div> <div style=" background:#033"></div> <div style=" background:#369"></div> <div style=" background:#F60"></div> <div style=" background:#030"></div> <div style=" background:#033"></div> <div style=" background:#369"></div> <div style=" background:#F60"></div> <div style=" background:#F3C"></div> <div style=" background:#F0F"></div> <div style=" background:#0FF"></div> <div style=" background:#FF0"></div> <div style=" background:#00F"></div> <div style=" background:#0F0"></div> </body> </html>
http://code.google.com/p/wkhtmltopdf/This is a communication platform for wkhtmltopdf issues, but in English.
For a lot of related knowledge, please visit PHP Chinese website!
The above is the detailed content of How to convert html to pdf file using php. For more information, please follow other related articles on the PHP Chinese website!