瀏覽代碼

优化html转pdf,改回去有问题

wangliang 4 年之前
父節點
當前提交
aed0bc8bc9

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

@@ -92,13 +92,6 @@
             <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>

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

@@ -34,16 +34,6 @@ 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;
     }

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

@@ -3,7 +3,6 @@ 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;
@@ -18,7 +17,10 @@ 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.*;
+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.common.contant.SystemConstant;
 import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
 import com.qmth.distributed.print.common.util.HexUtils;
@@ -61,9 +63,6 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
     @Autowired
     private DictionaryConfig dictionaryConfig;
 
-    @Resource
-    AsposePdfUtil asposePdfUtil;
-
     /**
      * 保存附件
      *
@@ -143,13 +142,13 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
                 String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
 //                destUrl = destUrl.replaceAll("\\\\","/");
 
-//                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();
-//                }
+                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));
@@ -167,8 +166,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);
-                File pdfFile = asposePdfUtil.documentToPdf(finalFile.getPath(), destUrl, PaperSize.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[]{
@@ -298,13 +297,13 @@ 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 = asposePdfUtil.documentToPdf(htmlFile.getPath(), destUrl, PaperSize.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;

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

@@ -1,110 +0,0 @@
-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;
-    }
-}

+ 2 - 5
distributed-print-business/src/main/java/com/qmth/distributed/print/business/util/PdfUtil.java

@@ -1,10 +1,7 @@
 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 com.itextpdf.text.pdf.PdfStamper;
+import com.itextpdf.text.pdf.*;
 import com.qmth.distributed.print.business.bean.dto.PdfDto;
 import com.qmth.distributed.print.business.enums.PageSizeEnum;
 import com.qmth.distributed.print.business.enums.UploadFileEnum;
@@ -61,7 +58,7 @@ public class PdfUtil {
                         .add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
                 outputPath = stringJoiner.toString() + dirName.toString();
             }
-            copy = new PdfCopy(document, new FileOutputStream(outputPath));
+            copy = new PdfSmartCopy(document, new FileOutputStream(outputPath));
             document.open();
             for (int i = 0; i < files.length; i++) {
                 reader = new PdfReader(files[i]);

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

@@ -1,14 +0,0 @@
-<?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>

二進制
distributed-print-business/src/main/resources/lib/aspose-words-15.8.0-jdk16.jar


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

@@ -57,8 +57,6 @@ 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