One, the use of wkhtmltopdf
wkhtmltopdf can directly convert any web page that can be browsed in the browser into a pdf
Description: Liu Hongdian's Architectural Forest is a blog dedicated to architecture, address: https://www.cnblogs.com/architectforest
The corresponding source code can be obtained here: https://github.com/liuhongdi/
Description: Author: Liu association E-mail: [email protected]
Second, download and install wkhtmltopdf
1. Official website address:
https://wkhtmltopdf.org/downloads.html
2, download:
[root@blog source]# wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox-0.12.5-1.centos8.x86_64.rpm
3. Installation
[root@blog source]# rpm -ivh wkhtmltox-0.12.5-1.centos8.x86_64.rpm error: Failed dependencies: xorg-x11-fonts-75dpi is needed by wkhtmltox-1:0.12.5-1.centos8.x86_64 xorg-x11-fonts-Type1 is needed by wkhtmltox-1:0.12.5-1.centos8.x86_64
Report an error, first install the two packages that have dependencies:
[root@blog source]# dnf install xorg-x11-fonts-75dpi [root@blog source]# dnf install xorg-x11-fonts-Type1
Just install it again
[root@blog source]# rpm -ivh wkhtmltox-0.12.5-1.centos8.x86_64.rpm Verifying... ################################# [100%] Preparing... ################################# [100%] Updating / installing... 1:wkhtmltox-1:0.12.5-1.centos8 ################################# [100%]
Third, view the version and help
1, view version
[root@blog source]# /usr/local/bin/wkhtmltopdf --version wkhtmltopdf 0.12.5 (with patched qt)
2. View help
[root@blog source]# /usr/local/bin/wkhtmltopdf --help
3. View the manual
[root@blog source]# man wkhtmltopdf
Fourth, the usage example of wkhtmltopdf:
1, turn to the local page
[root@blog ~]# /usr/local/bin/wkhtmltopdf /home/sysop/work/jianli.html /home/webop/work/jianli.pdf Loading pages (1/6) Counting pages (2/6) Resolving links (4/6) Loading headers and footers (5/6) Printing pages (6/6) Done
2. Go to the online page:
[root@blog ~]# /usr/local/bin/wkhtmltopdf http://www.baidu.com /home/webop/work/baidu.pdf
Five, the problems encountered in the use of wkhtmltopdf
1. Question: Some text cannot be displayed normally?
solve:
Save fonts under windows to / usr / share / fonts / directory
[root@blog source]# cp /home/webop/work/simsun.ttc /usr/share/fonts/
2, the problem: the font of the text is not displayed normally, some are bold, some are Song type
solve:
Specify the path of the font in the head,
@font-face{font-family: "宋体";src:url("/usr/share/fonts/simsun/simsun.ttc");}
Specify the corresponding font in the elements on the page, for example:
<body style='font-family:"宋体";'>
Then you can convert
Six, a complete example:
html code:
<html> <head> <meta charset="utf-8" /> <meta name="renderer" content="webkit"> <title>测试pdf页面生成</title> <style> @font-face{font-family: "宋体";src:url("/usr/share/fonts/simsun/simsun.ttc");} @font-face{font-family: "黑体";src:url("/usr/share/fonts/simhei/simhei.ttf");} </style> </head> <body style='font-family:"宋体";'> <table style="width:1024px;margin: auto;background: #eeeeee;"> <tr><td>姓名:</td><td>老刘</td></tr> <tr><td>照片:</td><td><img src= "https://imgs-qn.qiniuyun.com/ware/sowhatimg/ware/orig/2/35/35158.jpg" /> </ td > </ tr > < tr > < td > Description: </ td > < td > < div style = 'width: 800px; word-wrap: break-word; line-height: 40px; font-family: "Bold Body";' > Palm Web OS is an embedded based on Linux kernel operating system. The platform was officially released at the 2009 CES conference, and the first mobile phone with Web OS, Plam Pre, was released in June of the same year.
At that time, the iPhone 3GS '"rival enemy", Palm pre mobile phone. The slider Qwerty full keyboard, multi-touch 3.1-inch TFT screen,
uses 600Mhz ARM Cortex A8 CPU, "trackball" function is similar to the iPhone Home button. </ div > </ td > </ tr > </body> </html>
pdf renderings:
You can see that the font in the pdf is rendered according to the font we specified
Seven, check the version of centos
[root@blog ~]# cat /etc/redhat-release CentOS Linux release 8.0.1905 (Core)