wangliang 4 years ago
parent
commit
1117b9353c

+ 10 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/domain/SysDomain.java

@@ -32,6 +32,16 @@ public class SysDomain implements Serializable {
 
 
     String adminLogoUrl;
     String adminLogoUrl;
 
 
+    String htmlToPdfUrl;
+
+    public String getHtmlToPdfUrl() {
+        return htmlToPdfUrl;
+    }
+
+    public void setHtmlToPdfUrl(String htmlToPdfUrl) {
+        this.htmlToPdfUrl = htmlToPdfUrl;
+    }
+
     public String getAdminLogoUrl() {
     public String getAdminLogoUrl() {
         return adminLogoUrl;
         return adminLogoUrl;
     }
     }

+ 13 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/enums/PageSizeEnum.java

@@ -0,0 +1,13 @@
+package com.qmth.distributed.print.business.enums;
+
+/**
+ * @Description: 页面大小enum
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/7
+ */
+public enum PageSizeEnum {
+    A3,
+    A4;
+}

+ 20 - 6
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/htmltopdf/HtmlToPdfInterceptor.java

@@ -1,27 +1,41 @@
 package com.qmth.distributed.print.business.util.htmltopdf;
 package com.qmth.distributed.print.business.util.htmltopdf;
 
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 import java.io.BufferedReader;
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
 
 
+/**
+ * @Description: htmltopdf线程
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/7
+ */
 public class HtmlToPdfInterceptor extends Thread {
 public class HtmlToPdfInterceptor extends Thread {
+    private final static Logger log = LoggerFactory.getLogger(HtmlToPdfInterceptor.class);
+
     private InputStream is;
     private InputStream is;
 
 
-    public HtmlToPdfInterceptor(InputStream is){
+    public HtmlToPdfInterceptor(InputStream is) {
         this.is = is;
         this.is = is;
     }
     }
 
 
     @Override
     @Override
-    public void run(){
-        try{
-            InputStreamReader isr = new InputStreamReader(is, "utf-8");
+    public void run() {
+        try {
+            InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
             BufferedReader br = new BufferedReader(isr);
             BufferedReader br = new BufferedReader(isr);
             String line = null;
             String line = null;
             while ((line = br.readLine()) != null) {
             while ((line = br.readLine()) != null) {
-                System.out.println(line.toString()); //输出内容
+                log.info(line.toString()); //输出内容
             }
             }
-        }catch (IOException e){
+        } catch (IOException e) {
+            log.error("请求出错", e);
             e.printStackTrace();
             e.printStackTrace();
         }
         }
     }
     }

+ 26 - 21
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/htmltopdf/wkhtmltopdf.java → distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/htmltopdf/HtmltopdfUtil.java

@@ -1,20 +1,32 @@
 package com.qmth.distributed.print.business.util.htmltopdf;
 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
      * 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 file = new File(destPath);
         File parent = file.getParentFile();
         File parent = file.getParentFile();
         // 如果pdf保存路径不存在,则创建路径
         // 如果pdf保存路径不存在,则创建路径
@@ -26,10 +38,11 @@ public class wkhtmltopdf {
         // 非windows 系统
         // 非windows 系统
 //            toPdfTool = "/usr/local/bin/wkhtmltopdf";
 //            toPdfTool = "/usr/local/bin/wkhtmltopdf";
 //        }
 //        }
-        cmd.append(toPdfTool);
+        DictionaryConfig dictionaryConfig = SpringContextHolder.getBean(DictionaryConfig.class);
+        cmd.append(dictionaryConfig.sysDomain().getHtmlToPdfUrl());
         cmd.append(" ");
         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("  --page-size A4 ");// 打A4
 //        cmd.append("  --header-line");// 页眉下面的线
 //        cmd.append("  --header-line");// 页眉下面的线
 //        cmd.append(" --header-center 这里是页眉这里是页眉这里是页眉这里是页眉 ");//页眉中间内容
 //        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-html  file:///" + "https://blog.csdn.net/x6582026/article/details/53835835");// (添加一个HTML页脚,后面是网址)
 //        cmd.append(" --footer-line");// * 显示一条线在页脚内容上)
 //        cmd.append(" --footer-line");// * 显示一条线在页脚内容上)
 //        cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
 //        cmd.append(" --footer-spacing 5 ");// (设置页脚和内容的距离)
-
         cmd.append(srcPath);
         cmd.append(srcPath);
         cmd.append(" ");
         cmd.append(" ");
         cmd.append(destPath);
         cmd.append(destPath);
 
 
-        System.out.println(cmd.toString());
+        log.info("proc:{}", cmd.toString());
         boolean result = true;
         boolean result = true;
         try {
         try {
             Process proc = Runtime.getRuntime().exec(cmd.toString());
             Process proc = Runtime.getRuntime().exec(cmd.toString());
@@ -55,17 +67,10 @@ public class wkhtmltopdf {
             output.start();
             output.start();
             proc.waitFor();
             proc.waitFor();
         } catch (Exception e) {
         } catch (Exception e) {
+            log.error("请求出错", e);
             result = false;
             result = false;
             e.printStackTrace();
             e.printStackTrace();
         }
         }
         return result;
         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");
-    }
 }
 }

+ 3 - 2
distributed-print/src/main/java/com/qmth/distributed/print/api/SysController.java

@@ -15,13 +15,14 @@ import com.qmth.distributed.print.business.bean.result.LoginResult;
 import com.qmth.distributed.print.business.config.DictionaryConfig;
 import com.qmth.distributed.print.business.config.DictionaryConfig;
 import com.qmth.distributed.print.business.entity.*;
 import com.qmth.distributed.print.business.entity.*;
 import com.qmth.distributed.print.business.enums.DownloadFileEnum;
 import com.qmth.distributed.print.business.enums.DownloadFileEnum;
+import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.RoleTypeEnum;
 import com.qmth.distributed.print.business.enums.RoleTypeEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.service.*;
 import com.qmth.distributed.print.business.util.AuthUtil;
 import com.qmth.distributed.print.business.util.AuthUtil;
 import com.qmth.distributed.print.business.util.RedisUtil;
 import com.qmth.distributed.print.business.util.RedisUtil;
 import com.qmth.distributed.print.business.util.ServletUtil;
 import com.qmth.distributed.print.business.util.ServletUtil;
-import com.qmth.distributed.print.business.util.htmltopdf.wkhtmltopdf;
+import com.qmth.distributed.print.business.util.htmltopdf.HtmltopdfUtil;
 import com.qmth.distributed.print.business.util.security.Md5Utils;
 import com.qmth.distributed.print.business.util.security.Md5Utils;
 import com.qmth.distributed.print.common.SignatureEntityTest;
 import com.qmth.distributed.print.common.SignatureEntityTest;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.contant.SystemConstant;
@@ -253,7 +254,7 @@ public class SysController {
     @ApiResponses({@ApiResponse(code = 200, message = "学校信息", response = EditResult.class)})
     @ApiResponses({@ApiResponse(code = 200, message = "学校信息", response = EditResult.class)})
     @Aac(auth = BOOL.FALSE)
     @Aac(auth = BOOL.FALSE)
     public Result queryBySchoolCode(@ApiParam(value = "机构code", required = true) @RequestParam String code) {
     public Result queryBySchoolCode(@ApiParam(value = "机构code", required = true) @RequestParam String code) {
-//        wkhtmltopdf.convert("/ONLINE_EXAM/distributed/3120ee49668e4bf48fd8c161a2abb73b的副本.html", "/ONLINE_EXAM/distributed/3120ee49668e4bf48fd8c161a2abb73b的副本.pdf");
+//        HtmltopdfUtil.convert("/Users/king/Downloads/测试附件/3120ee49668e4bf48fd8c161a2abb73b的副本.html", "/Users/king/Downloads/pdf/3120ee49668e4bf48fd8c161a2abb73b的副本A3_test1.pdf", PageSizeEnum.A3);
         if (!Objects.equals(code.toUpperCase(), RoleTypeEnum.ADMIN.name())) {
         if (!Objects.equals(code.toUpperCase(), RoleTypeEnum.ADMIN.name())) {
             BasicSchool basicSchool = cacheService.schoolCache(code);
             BasicSchool basicSchool = cacheService.schoolCache(code);
             if (Objects.isNull(basicSchool)) {
             if (Objects.isNull(basicSchool)) {

+ 2 - 0
distributed-print/src/main/resources/application.properties

@@ -18,6 +18,7 @@ db.password=123456789
 
 
 #redis\u6570\u636E\u6E90\u914D\u7F6E
 #redis\u6570\u636E\u6E90\u914D\u7F6E
 com.qmth.redis.host=${db.host}
 com.qmth.redis.host=${db.host}
+com.qmth.redis.port=6379
 com.qmth.redis.db=0
 com.qmth.redis.db=0
 #com.qmth.redis.password
 #com.qmth.redis.password
 
 
@@ -52,6 +53,7 @@ sys.config.serverHost=localhost:7001
 sys.config.accessKey=0bce69d94a7b4aef8bc0badf150351a9
 sys.config.accessKey=0bce69d94a7b4aef8bc0badf150351a9
 sys.config.accessSecret=LdUwb5X4etmjW7fDn0KAdoXG0Yt7AkDu
 sys.config.accessSecret=LdUwb5X4etmjW7fDn0KAdoXG0Yt7AkDu
 sys.config.adminLogoUrl=http://qmth-test.oss-cn-shenzhen.aliyuncs.com/frontend/wx_logo.png
 sys.config.adminLogoUrl=http://qmth-test.oss-cn-shenzhen.aliyuncs.com/frontend/wx_logo.png
+sys.config.htmlToPdfUrl=/usr/local/bin/wkhtmltopdf
 spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 
 
 org.center.url=https://solar.qmth.com.cn
 org.center.url=https://solar.qmth.com.cn