|
@@ -1,20 +1,32 @@
|
|
|
package com.qmth.distributed.print.business.util.htmltopdf;
|
|
|
|
|
|
-import java.io.File;
|
|
|
+import com.qmth.distributed.print.business.config.DictionaryConfig;
|
|
|
+import com.qmth.distributed.print.business.enums.PageSizeEnum;
|
|
|
+import com.qmth.distributed.print.common.contant.SpringContextHolder;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
|
-public class wkhtmltopdf {
|
|
|
+import java.io.File;
|
|
|
|
|
|
- // wkhtmltopdf在系统中的路径
|
|
|
- private static String toPdfTool = "/usr/local/bin/wkhtmltopdf";
|
|
|
+/**
|
|
|
+ * @Description: htmltopdf
|
|
|
+ * @Param:
|
|
|
+ * @return:
|
|
|
+ * @Author: wangliang
|
|
|
+ * @Date: 2021/4/7
|
|
|
+ */
|
|
|
+public class HtmltopdfUtil {
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(HtmltopdfUtil.class);
|
|
|
|
|
|
/**
|
|
|
* html转pdf
|
|
|
*
|
|
|
- * @param srcPath html路径,可以是硬盘上的路径,也可以是网络路径
|
|
|
- * @param destPath pdf保存路径
|
|
|
- * @return 转换成功返回true
|
|
|
+ * @param srcPath
|
|
|
+ * @param destPath
|
|
|
+ * @param pageSizeEnum
|
|
|
+ * @return
|
|
|
*/
|
|
|
- public static boolean convert(String srcPath, String destPath) {
|
|
|
+ public static boolean convert(String srcPath, String destPath, PageSizeEnum pageSizeEnum) {
|
|
|
File file = new File(destPath);
|
|
|
File parent = file.getParentFile();
|
|
|
// 如果pdf保存路径不存在,则创建路径
|
|
@@ -26,10 +38,11 @@ public class wkhtmltopdf {
|
|
|
// 非windows 系统
|
|
|
// toPdfTool = "/usr/local/bin/wkhtmltopdf";
|
|
|
// }
|
|
|
- cmd.append(toPdfTool);
|
|
|
+ DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
|
|
|
+ cmd.append(dictionaryConfig.sysDomain().getHtmlToPdfUrl());
|
|
|
cmd.append(" ");
|
|
|
-
|
|
|
- cmd.append(" --page-size A3 ");// 打A3
|
|
|
+ cmd.append(" --page-size ").append(pageSizeEnum.name()).append(" ");// 打A3
|
|
|
+// cmd.append(" --margin-top 3cm ");
|
|
|
// cmd.append(" --page-size A4 ");// 打A4
|
|
|
// cmd.append(" --header-line");// 页眉下面的线
|
|
|
// cmd.append(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
|
|
@@ -40,12 +53,11 @@ public class wkhtmltopdf {
|
|
|
// //cmd.append(" --footer-html file:///" + "https://blog.csdn.net/x6582026/article/details/53835835");// (添加一个HTML页脚,后面是网址)
|
|
|
// cmd.append(" --footer-line");// * 显示一条线在页脚内容上)
|
|
|
// cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
|
|
|
-
|
|
|
cmd.append(srcPath);
|
|
|
cmd.append(" ");
|
|
|
cmd.append(destPath);
|
|
|
|
|
|
- System.out.println(cmd.toString());
|
|
|
+ log.info("proc:{}", cmd.toString());
|
|
|
boolean result = true;
|
|
|
try {
|
|
|
Process proc = Runtime.getRuntime().exec(cmd.toString());
|
|
@@ -55,17 +67,10 @@ public class wkhtmltopdf {
|
|
|
output.start();
|
|
|
proc.waitFor();
|
|
|
} catch (Exception e) {
|
|
|
+ log.error("请求出错", e);
|
|
|
result = false;
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
-// HtmlToPdf.convert("http://www.baidu.com", "F:/pdf/baidu.pdf");
|
|
|
- String filename = "3120ee49668e4bf48fd8c161a2abb73b的副本";
|
|
|
- wkhtmltopdf.convert("/Users/king/Downloads/测试附件/" + filename + ".html", "/Users/king/Downloads/pdf/" + filename + "A4.pdf");
|
|
|
-// HtmlToPdf.convert("http://api.gyingyuan.com/", "F:/pdf/"+ UUID.randomUUID().toString()+".pdf");
|
|
|
-// HtmlToPdf.convert("https://www.aliyun.com/jiaocheng/285649.html", "F:/pdf/baidu.pdf");
|
|
|
- }
|
|
|
}
|