Home  >  Article  >  Backend Development  >  [Reprint] The most powerful php in history to generate pdf files, how to convert html to pdf files_PHP tutorial

[Reprint] The most powerful php in history to generate pdf files, how to convert html to pdf files_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:311160browse

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 and FPDF are two methods that require writing programs to generate pdf, which means they do not support directly converting html pages into pdf; although html2pdf can convert html pages into pdf files, it can only convert general Simple html code, if your html content wants 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 useful 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 software that converts html pages into pdf. 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. If your English is not good, just use Google to translate them. The following is For example, on the windows platform, I downloaded the wkhtmltopdf-0.9.9-installer.exe version. I am running win7 There was no problem in the installation test on 32-bit, 64-bit and Windows 2003. Just install it directly after downloading. Pay attention to the installation path, which will be used below.
After installation, you need to add the following to the system environment variable named "Path": ;C:Program Files (x86)wkhtmltopdf, which is the directory where you installed it. After installation, restart your computer.

Two, test the effect
Directly enter in cmd: wkhtmltopdf http://www.shwzzz.cn/ F:website1.pdf
The first is: the name of the running software (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.

Three, call 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:

3. Solve the paging problem
wkhtmltopdf is very useful, but it has some drawbacks. 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>1362px, but the value becomes wrong as it goes to the end. We still don’t know how many pixels a PDF page has. 
<p>But <span>wkhtmltopdf has a good method, which is to add a page-break-inside:avoid; after the style of that div, and it will be ok. <br>
</span></p>
<pre class="brush:html;toolbar:false;"><!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>http://code.google.com/p/wkhtmltopdf/This is a communication platform for <span>wkhtmltopdf issues, but in English. </span>
<p><span>Article source: http://www.shwzzz.cn/news/xuetang/159.html</span></p><p align="left"></p><div style="display:none;">
<span id="url" itemprop="url">http://www.bkjia.com/PHPjc/781928.html</span><span id="indexUrl" itemprop="indexUrl">www.bkjia.com</span><span id="isOriginal" itemprop="isOriginal">true</span><span id="isBasedOnUrl" itemprop="isBasedOnUrl">http: //www.bkjia.com/PHPjc/781928.html</span><span id="genre" itemprop="genre">TechArticle</span><span id="description" itemprop="description">There was a customer who needed to generate pdf files from some html pages, and then I found some people who used php to surround the html pages. class into a pdf file. It can be said that I have searched a lot for methods, such as html2pdf, p...</span>
</div>
<div class="art_confoot"></div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn