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

How to use wkhtmltopdf to convert HTML to PDF in java

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

Share

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

This article shows you how to use java wkhtmltopdf HTML to PDF, concise and easy to understand, absolutely can make you shine, through the detailed introduction of this article I hope you can gain something.

Step 1 Download and install

Choose the installation package according to your system.

Step 2 Configure environment variables

After installation, find the installation path and configure it into environment variables for easy use.

After configuring the environment variables, you can test them.

Look at the conversion effect.

You can see that the conversion effect is very good.

The third step code calls wkhtmltopdf for conversion

auxiliary code

package fangrong.com.cn.common.utils;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;public class HtmlToPdfInterceptor extends Thread { private InputStream is; public HtmlToPdfInterceptor(InputStream is) { this.is = is; } public void run() { try { InputStreamReader isr = new InputStreamReader(is, "utf-8"); BufferedReader br = new BufferedReader(isr); String line = null; while ((line = br.readLine()) != null) { System.out.println(line.toString()); //Output conversion progress, etc. } } catch (IOException e) { e.printStackTrace(); } }}

translation code

package fangrong.com.cn.common.utils;import java.io.File;/** * Input form or JavaScript script support: --enable-forms, The following are netizen finishing parameter description * wkhtmltopdf [OPTIONS]... [More input files] * General options * --allow Allows loading of files or files from specified folders (repeatable)* --book* When setting up a book to print, the option * --collate is usually set Organize when printing multiple copies * --cookie Set an extra cookie (repeatable)* --cookie-jar Read and write cookies and provide cookies in jar file * --copies Number of PDF files copied and printed (default is 1)* --cover* Use HTML files as cover pages. It will insert * --custom-header before TOC with header and footer Set an additional HTTP header (repeatable)* --debug-javascript javascript debug output displayed * --default-header* Add a default header, with the name of the left side of the page and the page count to the right, e.g.: --header-left '[webpage]' --header-right '[page]/[toPage]' --header-line * --disable-external-links* Disable-internal-links* Prohibited Local Links * --disable-javascript Disables JavaScript * --disable-pdf-compression* Disable lossless compression on PDF objects * --disable-smart-shrinking* Disable intelligent strategy shrinkage using WebKit, leaving pixel/ DPI ratio unchanged * --disallow-local-file-access Prohibits local files that allow conversion from reading other local files unless explicitly allows--allow * --dpi Explicitly change DPI (this has no effect on X11-based systems)* --enable-plugins Enable installed plug-ins (e.g. Flash * --encoding Set default text encoding * --extended-help Displays more extensive help detailing unusual command switches * --forms* Open HTML Form Field to PDF Form Field * --grayscale PDF format will be generated in grayscale * --help Display help * --htmldoc Output Program HTML Help * --ignore-load-errors Ignoring claims An error page has been encountered during loading * --lowquality Produce low quality PDF/ PS. Helpful Narrow the space in the result document * --manpage Output program manual page * --margin-bottom Set page margin (default 10mm) * --margin-left Left margin (default 10mm) * --margin-right Set page right margin (default 10mm) * --margin-top Set page margin (default 10mm) * --minimum-font-size

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