Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The method of exporting pdf from html by wkhtmltopdf

Shulou Source: shulou.com Published: 2022-06-02 08:05:59 10月24日 Update

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!

Tags: Content method header learning environment path function size tool more system process help utility for this next location company variable font Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Apple MariaDB Shulou Tech Info Microsoft