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:
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!

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

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,

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

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

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.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.
