wangliang 4 жил өмнө
parent
commit
16fcdbe281

+ 7 - 0
distributed-print-business/pom.xml

@@ -92,6 +92,13 @@
             <groupId>org.freemarker</groupId>
             <artifactId>freemarker</artifactId>
         </dependency>
+        <dependency>
+            <groupId>com.aspose.words</groupId>
+            <artifactId>aspose-words-jdk16</artifactId>
+            <version>15.8.0</version>
+            <scope>system</scope>
+            <systemPath>${project.basedir}/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar</systemPath>
+        </dependency>
     </dependencies>
 
 </project>

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

@@ -34,6 +34,16 @@ public class SysDomain implements Serializable {
 
     String htmlToPdfUrl;
 
+    String documentToPdfLicense;
+
+    public String getDocumentToPdfLicense() {
+        return documentToPdfLicense;
+    }
+
+    public void setDocumentToPdfLicense(String documentToPdfLicense) {
+        this.documentToPdfLicense = documentToPdfLicense;
+    }
+
     public String getHtmlToPdfUrl() {
         return htmlToPdfUrl;
     }

+ 21 - 17
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/BasicAttachmentServiceImpl.java

@@ -3,6 +3,7 @@ package com.qmth.distributed.print.business.service.impl;
 import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
 import com.aliyun.oss.common.utils.BinaryUtil;
+import com.aspose.words.PaperSize;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.itextpdf.text.DocumentException;
 import com.itextpdf.text.pdf.PdfReader;
@@ -17,10 +18,7 @@ import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
 import com.qmth.distributed.print.business.mapper.BasicAttachmentMapper;
 import com.qmth.distributed.print.business.service.BasicAttachmentService;
-import com.qmth.distributed.print.business.util.HtmlToPdfUtil;
-import com.qmth.distributed.print.business.util.OssUtil;
-import com.qmth.distributed.print.business.util.PdfUtil;
-import com.qmth.distributed.print.business.util.ServletUtil;
+import com.qmth.distributed.print.business.util.*;
 import com.qmth.distributed.print.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
 import com.qmth.distributed.print.common.util.HexUtils;
@@ -63,6 +61,9 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
     @Autowired
     private DictionaryConfig dictionaryConfig;
 
+    @Resource
+    AsposePdfUtil asposePdfUtil;
+
     /**
      * 保存附件
      *
@@ -141,13 +142,14 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 String pdfDirName = pdfStringJoiner.toString();
                 String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
 //                destUrl = destUrl.replaceAll("\\\\","/");
-                HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
-                File pdfFile = new File(destUrl);
-                if (!pdfFile.exists()) {
-                    pdfFile.getParentFile().mkdirs();
-                    pdfFile.createNewFile();
-                }
 
+//                HtmlToPdfUtil.convert(localHtmlFile.getPath(), destUrl, PageSizeEnum.A3);
+                File pdfFile = asposePdfUtil.documentToPdf(localHtmlFile.getPath(), destUrl, PaperSize.A3);
+//                File pdfFile = new File(destUrl);
+//                if (!pdfFile.exists()) {
+//                    pdfFile.getParentFile().mkdirs();
+//                    pdfFile.createNewFile();
+//                }
                 pdfDto = PdfUtil.addPdfPage(pdfFile);
                 localFileList.add(new PdfDto(pdfFile.getPath(), PageSizeEnum.A3, pdfDto.getPageCount()));
                 fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
@@ -165,7 +167,8 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 jsonObject.put(SystemConstant.TYPE, SystemConstant.LOCAL);
                 jsonObject.put(SystemConstant.PATH, stringJoiner.toString());
                 String destUrl = finalFile.getPath().replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
-                HtmlToPdfUtil.convert(finalFile.getPath(), destUrl, PageSizeEnum.A3);
+//                HtmlToPdfUtil.convert(finalFile.getPath(), destUrl, PageSizeEnum.A3);
+                File pdfFile = asposePdfUtil.documentToPdf(finalFile.getPath(), destUrl, PaperSize.A3);
                 jsonObject.put(SystemConstant.PDF_PATH, destUrl);
             }
             jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
@@ -294,13 +297,14 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
 
             String pdfDirName = pdfStringJoiner.toString();
             String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
-            File pdfFile = new File(destUrl);
-            if (!pdfFile.exists()) {
-                pdfFile.getParentFile().mkdirs();
-                pdfFile.createNewFile();
-            }
 
-            HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
+//            File pdfFile = new File(destUrl);
+//            if (!pdfFile.exists()) {
+//                pdfFile.getParentFile().mkdirs();
+//                pdfFile.createNewFile();
+//            }
+//            HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
+            File pdfFile = asposePdfUtil.documentToPdf(htmlFile.getPath(), destUrl, PaperSize.A4);
             String htmlFileMd5 = DigestUtils.md5Hex(new FileInputStream(htmlFile));
             String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
             PdfDto pdfDto = null;

+ 110 - 0
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/AsposePdfUtil.java

@@ -0,0 +1,110 @@
+package com.qmth.distributed.print.business.util;
+
+import com.aspose.words.*;
+import com.qmth.boot.api.exception.ApiException;
+import com.qmth.distributed.print.business.config.DictionaryConfig;
+import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
+import com.qmth.distributed.print.common.util.ResultUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Component;
+import org.springframework.util.ResourceUtils;
+
+import javax.annotation.Resource;
+import java.io.*;
+import java.util.Objects;
+
+/**
+ * @Description: aspose pdf util
+ * @Param:
+ * @return:
+ * @Author: wangliang
+ * @Date: 2021/4/7
+ */
+@Component
+public class AsposePdfUtil {
+    private final static Logger log = LoggerFactory.getLogger(AsposePdfUtil.class);
+
+    @Resource
+    DictionaryConfig dictionaryConfig;
+
+    /**
+     * 获取license
+     *
+     * @return
+     */
+    private boolean getLicense() {
+        boolean result = false;
+        try {
+            File license = null;
+            License aposeLic = new License();
+            if (dictionaryConfig.sysDomain().getDocumentToPdfLicense().startsWith(ResourceUtils.CLASSPATH_URL_PREFIX)) {
+                license = ResourceUtils.getFile(dictionaryConfig.sysDomain().getDocumentToPdfLicense());
+                aposeLic.setLicense(new FileInputStream(license));
+            } else {
+                InputStream stream = getClass().getClassLoader().getResourceAsStream(dictionaryConfig.sysDomain().getDocumentToPdfLicense());//classes下的读取方法
+//                license = new File(dictionaryConfig.sysDomain().getWordToPdfLicense());//放服务器上的读取方法
+                aposeLic.setLicense(stream);
+            }
+            result = true;
+        } catch (Exception e) {
+            log.error("请求出错", e);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        }
+        return result;
+    }
+
+    /**
+     * document转pdf
+     *
+     * @param documentPath
+     * @param pdfPath
+     * @return
+     * @throws IOException
+     */
+    public File documentToPdf(String documentPath, String pdfPath, int paperSize) throws IOException {
+        if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
+            throw ExceptionResultEnum.ERROR.exception("hyml转pdf失败,验证过期");
+        }
+        FileOutputStream os = null;
+        File file = null;
+        try {
+            long start = System.currentTimeMillis();
+            file = new File(pdfPath); //新建一个pdf文档
+            if (!file.getParentFile().exists()) {
+                file.getParentFile().mkdirs();
+                file.createNewFile();
+            }
+            os = new FileOutputStream(file);
+            Document doc = new Document(documentPath); //Address是将要被转化的word文档
+            DocumentBuilder builder = new DocumentBuilder(doc);
+            PageSetup pageSetup = builder.getPageSetup();
+            pageSetup.setPaperSize(paperSize);
+            pageSetup.setVerticalAlignment(PageVerticalAlignment.TOP);
+            if (paperSize == PaperSize.A3) {
+                pageSetup.setOrientation(Orientation.PORTRAIT);
+            }
+            doc.save(os, com.aspose.words.SaveFormat.PDF);//全面支持DOC, DOCX, OOXML, RTF HTML, OpenDocument, PDF, EPUB, XPS, SWF 相互转换
+//            ossUtil.ossUpload(pdfPath, new FileInputStream(file), DigestUtils.md5Hex(new FileInputStream(file)));
+            long end = System.currentTimeMillis();
+            log.info("共耗时:" + ((end - start) / 1000.0) + "秒"); //转化用时
+        } catch (Exception e) {
+            log.error("请求出错", e);
+            if (e instanceof ApiException) {
+                ResultUtil.error((ApiException) e, e.getMessage());
+            } else {
+                ResultUtil.error(e.getMessage());
+            }
+        } finally {
+            if (Objects.nonNull(os)) {
+                os.flush();
+                os.close();
+            }
+        }
+        return file;
+    }
+}

+ 14 - 0
distributed-print-business/src/main/resources/License.xml

@@ -0,0 +1,14 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<License>
+    <Data>
+        <Products>
+            <Product>Aspose.Total for Java</Product>
+            <Product>Aspose.Words for Java</Product>
+        </Products>
+        <EditionType>Enterprise</EditionType>
+        <SubscriptionExpiry>20991231</SubscriptionExpiry>
+        <LicenseExpiry>20991231</LicenseExpiry>
+        <SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
+    </Data>
+    <Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature>
+</License>

BIN
distributed-print-business/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar


+ 3 - 1
distributed-print/src/main/resources/application.properties

@@ -12,7 +12,7 @@ spring.application.name=distributed-print
 #\u6570\u636E\u6E90\u914D\u7F6E
 db.host=localhost
 db.port=3306
-db.name=distributed_print
+db.name=distributed-136
 db.username=root
 db.password=123456789
 
@@ -57,6 +57,8 @@ sys.config.accessSecret=y7AO6W0TOdTF8HpWBwGHbp3wfIHsmUKr
 sys.config.adminLogoUrl=http://qmth-test.oss-cn-shenzhen.aliyuncs.com/frontend/wx_logo.png
 #sys.config.htmlToPdfUrl=/usr/local/bin/wkhtmltopdf
 sys.config.htmlToPdfUrl=/usr/local/bin/wkhtmltopdf
+#\u672C\u5730\u5F00\u53D1\u7528classpath
+sys.config.documentToPdfLicense=classpath:License.xml
 spring.resources.static-locations=file:${sys.config.serverUpload},classpath:/META-INF/resources/,classpath:/resources/
 
 org.center.url=https://solar.qmth.com.cn