


[Reprint] The most powerful php in history to generate pdf files, how to convert html to pdf files_PHP tutorial
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>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid
