JAVA实现Html转Pdf(wkhtmltopdf)
生活随笔
收集整理的這篇文章主要介紹了
JAVA实现Html转Pdf(wkhtmltopdf)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
官網地址: https://wkhtmltopdf.org/downloads.html
直接下載對應版本即可
linux 安裝命令
linux需要額外安裝的插件
yum install xorg-x11-fonts-75dpi.noarch yum install xorg-x11-fonts-Type1.noarch yum install icu.x86_64 yum install libjpeg yum install libpng yum install libXrender yum install libXextlinux 默認會安裝到 /usr/local/bin/wkhtmltopdf
安裝完成后默認還有個 wkhtmltopng的工具
linux 測試命令
windows 測試命令
不得不說 效果相當的驚艷,樣式比某些瀏覽器的打印功能都好
下面直接上硬貨
package com.itender.ms.util;import com.itender.ms.config.WkhtmltopdfConfig; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component;import java.io.File;/*** @author ah* @date 2019/5/14 16:15*/ @Component public class HtmlToPdf {private Logger logger = LoggerFactory.getLogger(HtmlToPdf.class);// wkhtmltopdf在系統中的路徑private String toPdfTool ="C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";@Autowiredprivate WkhtmltopdfConfig config;/*** html轉pdf** @param srcPath* html路徑,可以是硬盤上的路徑,也可以是網絡路徑* @param destPath* pdf保存路徑* @return 轉換成功返回true*/public boolean convert(String srcPath, String destPath) {File file = new File(destPath);File parent = file.getParentFile();// 如果pdf保存路徑不存在,則創建路徑if (!parent.exists()) {parent.mkdirs();}StringBuilder cmd = new StringBuilder();if (System.getProperty("os.name").indexOf("Windows") == -1) {cmd.append(config.getPath());//.exe文件路徑}else {cmd.append(toPdfTool);}cmd.append(" ");//cmd.append(" \"");cmd.append(srcPath);// cmd.append("\" ");cmd.append(" ");cmd.append(destPath);//logger.info(cmd.toString());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;}public static void main(String[] args) {// HtmlToPdf.convert("C:\\Users\\dell\\Desktop\\aaa.html", "D:\\11111111.pdf");} } package com.itender.ms.util;import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader;/*** @author ah* @date 2019/5/14 16:25*/ public class HtmlToPdfInterceptor extends Thread {private InputStream is;public HtmlToPdfInterceptor(InputStream is){this.is = is;}@Overridepublic 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()); //輸出內容}}catch (IOException e){e.printStackTrace();}} }注意:html文件一定要注明編碼,不然會亂碼 如
<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8” ></meta>
表格分頁問題
需要添加樣式 :
能夠在最大程度上處理分頁時表格問題,但如果一個單元格內容整頁都無法放下時同樣會出現分頁后沒有邊框問題
java:
org.jsoup.nodes.Document documentHtml = Jsoup.parse(html);documentHtml.head().append("<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\" ></meta>");//后臺處理亂碼documentHtml.head().after("<style type=\"text/css\">\n" +"\ttable, tr, td, th, tbody, thead {\n" +"\tpage-break-inside: avoid !important;\n" +"\n" +"\t}\n" +"\t</style>");//處理表格分頁下面放一些網上找的各大html轉pdf工具性能對比圖
總結
以上是生活随笔為你收集整理的JAVA实现Html转Pdf(wkhtmltopdf)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 地图投影——高斯-克吕格投影、墨卡托投影
- 下一篇: 使用Adobe Audition生成基本