Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of converting HTMl to PDF

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "the method of converting HTMl to PDF". In the daily operation, I believe many people have doubts about the method of converting HTMl to PDF. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "the method of HTMl to PDF"! Next, please follow the editor to study!

Background

The company needs to package all the articles on the official website into pdf, because the articles are generated by post-rich text with HTML tags in the content. So you can only put the content in html and then download it.

HTML to PDF investigation 1. IText

IText is a very famous Java class library that quickly generates PDF files.

Advantages:

1. Very mature Java class library, a lot of related help documents, get started quickly.

two。 Full-featured, easy to use, you can generate POF documents directly through the code.

Disadvantages:

1. Although html to pdf is supported, there are some HTML tags and css attributes are not supported, and the conversion result may not be consistent with the original document.

two。 External css files are not supported

3. The cooperation with the Asian language support package itext-asian.jar is not very good, and it is easy to solve the problem of Chinese garbled code.

2.Flying Saucer

Flying Saucer is written in pure Java, and you can apply CSS2.1 style to render XML/XHTML into PDF, pictures. This library basically implements the integrity of CSS 2.1and fully conforms to the W3C specification.

Advantages:

1. Support for external css files

two。 PDF can be generated directly by entering the page address

Disadvantages:

1. Many css support is not good, which styles do not support you have to try slowly, quite painful process.

two。 The uppercase tag is not recognized! For example, DIV TD and so on, all do not recognize! In the final analysis, it is case-sensitive.

3. Anyway, all kinds of small details need to be controlled, if you are not very familiar with it, it is very easy to bug.

3.wkhtmltox

Wkhtmltox is a tool, similar to a phantomjs tool

Advantages:

1. PDF can be generated directly by entering the page address

two。 Very good support for HTML and CSS, support for external css

3. The utility model is easy to use and easy to operate.

Disadvantages: no obvious hard wounds are found, as if when the downloaded page is very large, it will cause the build to fail.

Wkhtmltox

Why introduce all of the above?

Download wkhtmltox

Download address on the official website: https://wkhtmltopdf.org/downloads.html

Install and use on linux

Upload wkhtmltox to the server

Rz

Extract, install (tar-Jxf)

Extract the xz file: xz-d wkhtmltox-0.12.4_linux-generic-amd64.tar.xz

Extract the installation tar file: tar-xvf wkhtmltox-0.12.4_linux-generic-amd64.tar

Copy the program to the / usr/bin/ directory so that it can be executed in any directory.

Cp wkhtmltox/bin/wkhtmltopdf / usr/bin/

Use Wkhtmltopdf

Wkhtmltopdf http://www.baidu.com / data/tmp/demo.pdf

5. If the generation of pdf fails, the related package may be missing. Go to usr/lib and install the corresponding package according to the error message.

Cd / usr/lib [daxiang [@ localhost] (https://my.oschina.net/u/570656) lib] $sudo yum install libXrender* [daxiang [@ localhost] (https://my.oschina.net/u/570656) lib] $sudo yum install libXextJAVA call Wkhtmltopdfpublic class HtmlToPdfUtil {public static String getCommand (String htmlUrl, String filePath) {StringBuilder command = new StringBuilder (); String system = System.getProperty ("os.name") If (system.contains ("Windows")) {command.append ("D:\ Program Files\\ wkhtmltopdf\\ bin\\ wkhtmltopdf.exe");} else if (system.contains ("Linux")) {command.append ("wkhtmltopdf") } / / command.append ("--encoding utf-8"); / / set the text code to utf-8 / / command.append ("--header-center Analysys"); / / display Analysys / / command.append ("--header-line") in the center of the header / / display a line under the header / / command.append ("--margin-left 5cm"); / / move the page left to 5cm / / command.append ("--footer-right [page] / [topage]"); / / display the number of pages command.append (htmlUrl) .append (") .append (filePath); return command.toString () } public static void main (String [] args) throws Exception {String command = getCommand ("https://www.baidu.com/"," e:/demo3.pdf "); Process process = Runtime.getRuntime () .exec (command); process.waitFor () / / this call is critical, that is, wait for the current command to be executed before executing System.out.println ("execution complete");}} wkhtmltopdf Chinese parameters

Chinese parameter description: http://blog.csdn.net/zhangkezhi_471885889/article/details/52184744

Mode of use:

Linux:wkhtmltopdf [OPTIONS]... [More input files] windows:wkhtmltopdf.exe [OPTIONS]... [More input files]

Case study:

Wkhtmltopdf-- footer-right [page] / [topage] http://www.baidu.com / data/baidu.pdf wkhtmltopdf-- header-center report-- outline-- header-line-- margin-top 2cm http://www.baidu.com / data/baidu.pdf background 1 solution

1. Create a template page: http://localhost:8080/templateView

two。 Using wkhtmltopdf to generate PDF

Matters needing attention

1. Only web pages rendered by template engines (velocity, Thymeleaf) can generate PDF

two。 Because the command is identified by a space number, if the generated PDF file name contains spaces, it will cause the generation to fail.

Knowledge extension (Thymeleaf)

Springboot recommends using Thymeleaf as the template engine. Template engine should be used in springboot projects in the future. It is recommended that everyone can use Thymeleaf.

Springboot configuration Thymeleaf: http://blog.didispace.com/springbootweb/Thymeleaf syntax: http://fanlychie.github.io/post/thymeleaf.html#9-3-2-text at this point, on the "HTMl to PDF method" learning is over, I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report