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 exporting pdf from html by wkhtmltopdf

2025-01-17 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 wkhtmltopdf to achieve html export pdf". In the daily operation, I believe many people have doubts about the method of wkhtmltopdf to achieve html export pdf. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "wkhtmltopdf to achieve html export pdf". Next, please follow the editor to study!

In the process of developing the risk control system, I used wkhtmltopdf to complete the function of generating pdf export at the back end. Among them, for this function, I have also written a tool class for wkhtmltopdf, which can be used repeatedly.

HtmlToPdfInterceptor class

The purpose of this class is to open a thread to implement the wkhtmltopdf process:

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}} catch (IOException e) {e.printStackTrace ();} II. HtmlToPdf utility class

Of course, the first thing is to install the wkhtmltopdf software on the server and configure the environment variables and the Chinese character set.

Import java.io.File;public class HtmlToPdf {/ / wkhtmltopdf path in the system / / windows environment / / private static final String toPdfTool = "C:\\ Program Files\\ wkhtmltopdf\\ bin\\ wkhtmltopdf.exe"; / / linux environment / / private static final String toPdfTool = "/ / usr//local//bin//wkhtmltopdf"; public static boolean convert (String toPdfTool, String srcPath, String destPath) {File file = new File (destPath) File parent = file.getParentFile (); / / if the pdf save path does not exist, create the path if (! parent.exists ()) {parent.mkdirs ();} StringBuilder cmd = new StringBuilder (); / / introduce wkhtmltopdf cmd.append (toPdfTool); cmd.append (") / / set page size cmd.append ("--page-size A4"); / / left spacing cmd.append ("--margin-left 4"); / / Line cmd.append under the header ("--header-line") / / cmd.append ("--header-center Zhongzhibao Financial Technology (Chengdu) Co., Ltd."); / / (add a HTML header followed by a URL) cmd.append ("--margin-top 2cm"); / / (set the distance between the header and the content. Default is 0) cmd.append ("--header-spacing 5") / / set footer content cmd.append ("--footer-center page [page] / total [topage] page"); / / footer font size cmd.append ("--footer-font-size 7"); / / * display a line on footer content) cmd.append ("--footer-line") / / (set the distance between footer and content) cmd.append ("- footer-spacing 0"); cmd.append (srcPath); cmd.append (""); cmd.append (destPath); boolean result = true; try {Process proc = Runtime.getRuntime (). Exec (cmd.toString ()); HtmlToPdfInterceptor error = new HtmlToPdfInterceptor (proc.getErrorStream ()) HtmlToPdfInterceptor output = new HtmlToPdfInterceptor (proc.getInputStream ()); error.start (); output.start (); proc.waitFor ();} catch (Exception e) {result = false; e.printStackTrace ();} return result }} at this point, the study on "the method of wkhtmltopdf implementing html to export pdf" 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