Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
The previous article introduced some of the front-end ways to implement PDF export, such as browsers, VUe-print-NB, HTML2Canvas, and JSPDF. This article describes the way to realize PDF export by combining the front and back ends, such as WkHTMLTopdf and iText. The front end is mainly responsible for the completion of export template, and the back end is for data filling and export
wkhtmltopdf
Wkhtmltopdf uses webKit rendering engine development, directly HTML pages into PDF, but needs to be installed on the server.
Official website: wkhtmltopdf.org/
Official Issues: github.com/wkhtmltopdf…
Configuration parameters: wkhtmltopdf.org/usage/wkhtm…
The configuration parameters of Chinese reference: segmentfault.com/a/119000001…
Install: Download the installation package from the official website // 2. Check the version: wkHTMLTopdf 0.12.6 (with patched Qt) $wkHTMLTopdf -v // 3. Testing: Enter the test instruction in CMD, $wkhtmltopdf https://www.baidu.com/ ~/Desktop/baidu. PDF // 网 页 $wkhtmltopdf --javascript-delay 3000 Report-html test. PDF // Local HTML pageCopy the code
Baidu page test instructions, you can view the processing progress:
Baidu page export effect display:
The front end needs to write statichtml
The template page is rendered and exported by the server.
Template page practice stomp:
- using
webkit
Rendering engine development, yeahCSS
Style overall support friendly, but rightCSS3
The new feature is not well supported and some page styles will fail.Wkhtmltopdf uses Qt to render HTML. The current version of Wkhtmltopdf uses Qt 4.8.5 Which uses version 2.2.4 of WebKit, As you can see I tried using the -webkit-prefixes
Wkhtmltopdf uses Qt to render HTML. The current version of WkHTMLTopdf uses Qt 4.8.5, which uses WebKit version 2.2.4. The -webkit- prefix is recommended.) - using
flex
Layout, need to use compatibility style, specific can be viewedissue - using
flex
Layout, exportedpdf
Will be truncated, so insteadtable
layout - The default font is Song Typeface. If it is not available on the machine, Chinese cannot be displayed. To use a different font, you must install the corresponding font, see Issue
vertical-align
Properties don’t work- Static template write
CSS
The style is a little bit troublesome, so I can write it directlySCSS
And then synchronously convert toCSS
, the implementation is as follows:
Sass --watch test.scss:test.cssCopy the code
iText
IText: Used in Java background to convert HTML files to PDF files. Based on Java to do HTML CSS style parsing processing, only for simpler pages and style support.
Usage process: the front end is responsible for completing the static template page, the back end fills the data based on the template page, and then exports the PDF file, and the front end downloads it.
Template page practice stomp:
- right
CSS
Poor style support,CSS3
Properties are not supported - Can’t use
flex
Layout, basically only for usetable
implementation - Where you want to change pages, add
page-break-after: always;
, indicating that the next element will be page-fedpdf
时itext
Will automatically recognize - In positioning, percentage is not supported
css
attributetop
(css property top in percents is not supported
) - Absolute positioning may be wrong (
Occupied area has not been initialized. Absolute positioning might be applied incorrectly
)