wangliang 4 роки тому
батько
коміт
31d447c58e

+ 60 - 56
distributed-print-business/pom.xml

@@ -1,10 +1,10 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <?xml version="1.0" encoding="UTF-8"?>
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-	<modelVersion>4.0.0</modelVersion>
-	<groupId>com.qmth.distributed.print.business</groupId>
-	<artifactId>distributed-print-business</artifactId>
-	<version>2.0.0</version>
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+    <groupId>com.qmth.distributed.print.business</groupId>
+    <artifactId>distributed-print-business</artifactId>
+    <version>2.0.0</version>
     <build>
     <build>
         <plugins>
         <plugins>
             <plugin>
             <plugin>
@@ -19,63 +19,67 @@
     </build>
     </build>
     <packaging>jar</packaging>
     <packaging>jar</packaging>
 
 
-	<parent>
-		<groupId>com.qmth.distributed.print.service</groupId>
-		<artifactId>distributed-print-service</artifactId>
-		<version>2.0.0</version>
-	</parent>
+    <parent>
+        <groupId>com.qmth.distributed.print.service</groupId>
+        <artifactId>distributed-print-service</artifactId>
+        <version>2.0.0</version>
+    </parent>
 
 
-	<dependencies>
-		<dependency>
-			<groupId>org.apache.velocity</groupId>
-			<artifactId>velocity-engine-core</artifactId>
-			<version>2.0</version>
-		</dependency>
-		<dependency>
-			<groupId>com.qmth.distributed.print.common</groupId>
-			<artifactId>distributed-print-common</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>commons-fileupload</groupId>
-			<artifactId>commons-fileupload</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>org.apache.poi</groupId>
-			<artifactId>poi-ooxml</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>org.apache.poi</groupId>
-			<artifactId>poi-ooxml-schemas</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>org.apache.poi</groupId>
-			<artifactId>poi</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>com.aliyun.oss</groupId>
-			<artifactId>aliyun-sdk-oss</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>com.qmth.boot</groupId>
-			<artifactId>data-mysql-mp</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>io.springfox</groupId>
-			<artifactId>springfox-swagger2</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>com.github.xiaoymin</groupId>
-			<artifactId>swagger-bootstrap-ui</artifactId>
-		</dependency>
-		<dependency>
-			<groupId>org.jetbrains</groupId>
-			<artifactId>annotations</artifactId>
-		</dependency>
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.velocity</groupId>
+            <artifactId>velocity-engine-core</artifactId>
+            <version>2.0</version>
+        </dependency>
+        <dependency>
+            <groupId>com.qmth.distributed.print.common</groupId>
+            <artifactId>distributed-print-common</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>commons-fileupload</groupId>
+            <artifactId>commons-fileupload</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml-schemas</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.aliyun.oss</groupId>
+            <artifactId>aliyun-sdk-oss</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>data-mysql-mp</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>io.springfox</groupId>
+            <artifactId>springfox-swagger2</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.github.xiaoymin</groupId>
+            <artifactId>swagger-bootstrap-ui</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.jetbrains</groupId>
+            <artifactId>annotations</artifactId>
+        </dependency>
         <dependency>
         <dependency>
             <groupId>com.aliyun</groupId>
             <groupId>com.aliyun</groupId>
             <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
             <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
             <version>1.1.0</version>
             <version>1.1.0</version>
         </dependency>
         </dependency>
+        <dependency>
+            <groupId>com.itextpdf</groupId>
+            <artifactId>itextpdf</artifactId>
+        </dependency>
     </dependencies>
     </dependencies>
 
 
 </project>
 </project>

+ 22 - 4
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/HttpUtil.java

@@ -1,5 +1,6 @@
 package com.qmth.distributed.print.business.util;
 package com.qmth.distributed.print.business.util;
 
 
+import com.qmth.distributed.print.business.util.htmltopdf.HtmlToPdfInterceptor;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.io.IOUtils;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpEntity;
@@ -9,6 +10,8 @@ import org.apache.http.client.methods.HttpPost;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.client.methods.HttpUriRequest;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.impl.client.HttpClients;
 import org.apache.http.message.BasicNameValuePair;
 import org.apache.http.message.BasicNameValuePair;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.IOException;
@@ -18,6 +21,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 import java.util.Map;
 import java.util.Map;
+import java.util.Objects;
 
 
 /**
 /**
  * @Description: http util
  * @Description: http util
@@ -27,6 +31,7 @@ import java.util.Map;
  * @Date: 2020/12/11
  * @Date: 2020/12/11
  */
  */
 public class HttpUtil {
 public class HttpUtil {
+    private final static Logger log = LoggerFactory.getLogger(HttpUtil.class);
 
 
     /**
     /**
      * post 请求
      * post 请求
@@ -69,10 +74,23 @@ public class HttpUtil {
      */
      */
     public static String getRespString(HttpUriRequest request) throws IOException {
     public static String getRespString(HttpUriRequest request) throws IOException {
         // 获取响应流
         // 获取响应流
-        InputStream in = getRespInputStream(request);
-        ByteArrayOutputStream ou = new ByteArrayOutputStream();
-        IOUtils.copy(in, ou);
-        return new String(ou.toByteArray(), StandardCharsets.UTF_8);
+        InputStream in = null;
+        ByteArrayOutputStream ou = null;
+        try {
+            in = getRespInputStream(request);
+            ou = new ByteArrayOutputStream();
+            IOUtils.copy(in, ou);
+        } catch (Exception e) {
+            log.error("请求出错", e);
+        } finally {
+            if (Objects.nonNull(in)) {
+                in.close();
+            }
+            if (Objects.nonNull(ou)) {
+                ou.close();
+            }
+        }
+        return Objects.nonNull(ou) ? new String(ou.toByteArray(), StandardCharsets.UTF_8) : null;
     }
     }
 
 
     /**
     /**

+ 72 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/MergePdfUtil.java

@@ -0,0 +1,72 @@
+package com.qmth.distributed.print.business.util;
+
+import com.itextpdf.text.Document;
+import com.itextpdf.text.pdf.PdfCopy;
+import com.itextpdf.text.pdf.PdfImportedPage;
+import com.itextpdf.text.pdf.PdfReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileOutputStream;
+import java.util.Objects;
+
+/**
+ * @Description: 合并pdf util
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/7
+ */
+public class MergePdfUtil {
+    private final static Logger log = LoggerFactory.getLogger(MergePdfUtil.class);
+
+    /**
+     * 将多个PDF合并成一个PDF
+     *
+     * @param files
+     * @param outputPath
+     */
+    public static void mergePDF(String[] files, String outputPath) {
+        Document document = null;
+        PdfCopy copy = null;
+        PdfReader reader = null;
+        try {
+            document = new Document(new PdfReader(files[0]).getPageSize(1));
+            copy = new PdfCopy(document, new FileOutputStream(outputPath));
+            document.open();
+            for (int i = 0; i < files.length; i++) {
+                reader = new PdfReader(files[i]);
+                int numberOfPages = reader.getNumberOfPages();
+                for (int j = 1; j <= numberOfPages; j++) {
+                    document.newPage();
+                    PdfImportedPage page = copy.getImportedPage(reader, j);
+                    copy.addPage(page);
+                }
+            }
+        } catch (Exception e) {
+            log.error("请求出错", e);
+        } finally {
+            if (Objects.nonNull(document)) {
+                document.close();
+            }
+            if (Objects.nonNull(reader)) {
+                reader.close();
+            }
+            if (Objects.nonNull(copy)) {
+                copy.flush();
+                copy.close();
+            }
+        }
+    }
+
+//    public static void main(String[] args) {
+//        String[] files = {
+//                "/Users/king/Downloads/测试附件/101_148382537591291904_signAndSticker.pdf",
+//                "/Users/king/Downloads/pdf/3120ee49668e4bf48fd8c161a2abb73b的副本A3.pdf",
+//                "/Users/king/Downloads/测试附件/101_158277997340655616_paperPackage.pdf"
+////                "/Users/king/Downloads/测试附件/101_148382537591291904_signAndSticker.pdf"
+//        };
+//        String outputPath = "/Users/king/Downloads/pdf/merge1.pdf";
+//        MergePdfUtil.mergePDF(files, outputPath);
+//    }
+}

+ 14 - 2
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/htmltopdf/HtmlToPdfInterceptor.java

@@ -8,6 +8,7 @@ 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;
 import java.nio.charset.StandardCharsets;
+import java.util.Objects;
 
 
 /**
 /**
  * @Description: htmltopdf线程
  * @Description: htmltopdf线程
@@ -27,16 +28,27 @@ public class HtmlToPdfInterceptor extends Thread {
 
 
     @Override
     @Override
     public void run() {
     public void run() {
+        BufferedReader br = null;
         try {
         try {
             InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
             InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8);
-            BufferedReader br = new BufferedReader(isr);
+            br = new BufferedReader(isr);
             String line = null;
             String line = null;
             while ((line = br.readLine()) != null) {
             while ((line = br.readLine()) != null) {
                 log.info(line.toString()); //输出内容
                 log.info(line.toString()); //输出内容
             }
             }
         } catch (IOException e) {
         } catch (IOException e) {
             log.error("请求出错", e);
             log.error("请求出错", e);
-            e.printStackTrace();
+        } finally {
+            try {
+                if (Objects.nonNull(is)) {
+                    is.close();
+                }
+                if (Objects.nonNull(br)) {
+                    br.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
         }
         }
     }
     }
 }
 }

+ 6 - 1
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/htmltopdf/HtmltopdfUtil.java

@@ -69,8 +69,13 @@ public class HtmltopdfUtil {
         } catch (Exception e) {
         } catch (Exception e) {
             log.error("请求出错", e);
             log.error("请求出错", e);
             result = false;
             result = false;
-            e.printStackTrace();
         }
         }
         return result;
         return result;
     }
     }
+
+//    public static void main(String[] args) {
+////        HtmltopdfUtil.convert("http://www.baidu.com", "/Users/king/Downloads/baidu.pdf", PageSizeEnum.A4);
+//        String filename = "3120ee49668e4bf48fd8c161a2abb73b的副本";
+//        HtmltopdfUtil.convert("/Users/king/Downloads/测试附件/" + filename + ".html", "/Users/king/Downloads/pdf/" + filename + "A4.pdf", PageSizeEnum.A3);
+//    }
 }
 }

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

@@ -15,14 +15,12 @@ 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.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;
@@ -254,7 +252,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) {
-//        HtmltopdfUtil.convert("/Users/king/Downloads/测试附件/3120ee49668e4bf48fd8c161a2abb73b的副本.html", "/Users/king/Downloads/pdf/3120ee49668e4bf48fd8c161a2abb73b的副本A3_test1.pdf", PageSizeEnum.A3);
+//        HtmltopdfUtil.convert("/Users/king/Downloads/测试附件/3120ee49668e4bf48fd8c161a2abb73b的副本.html", "/Users/king/Downloads/测试附件/new_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)) {

+ 6 - 0
pom.xml

@@ -40,6 +40,7 @@
         <httpclient.version>4.5.12</httpclient.version>
         <httpclient.version>4.5.12</httpclient.version>
         <version-plugin.version>2.8.1</version-plugin.version>
         <version-plugin.version>2.8.1</version-plugin.version>
         <netty-all.version>4.1.49.Final</netty-all.version>
         <netty-all.version>4.1.49.Final</netty-all.version>
+        <itextpdf.version>5.5.13</itextpdf.version>
     </properties>
     </properties>
 
 
     <dependencyManagement>
     <dependencyManagement>
@@ -207,6 +208,11 @@
                 <artifactId>spring-boot-starter-quartz</artifactId>
                 <artifactId>spring-boot-starter-quartz</artifactId>
                 <version>${spring-boot.version}</version>
                 <version>${spring-boot.version}</version>
             </dependency>
             </dependency>
+            <dependency>
+                <groupId>com.itextpdf</groupId>
+                <artifactId>itextpdf</artifactId>
+                <version>${itextpdf.version}</version>
+            </dependency>
         </dependencies>
         </dependencies>
     </dependencyManagement>
     </dependencyManagement>