|
@@ -3,7 +3,9 @@ package com.qmth.distributed.print.business.service.impl;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.alibaba.fastjson.JSONObject;
|
|
import com.aliyun.oss.common.utils.BinaryUtil;
|
|
import com.aliyun.oss.common.utils.BinaryUtil;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
+import com.itextpdf.text.DocumentException;
|
|
import com.itextpdf.text.pdf.PdfReader;
|
|
import com.itextpdf.text.pdf.PdfReader;
|
|
|
|
+import com.itextpdf.text.pdf.PdfStamper;
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
import com.qmth.boot.api.exception.ApiException;
|
|
import com.qmth.distributed.print.business.config.DictionaryConfig;
|
|
import com.qmth.distributed.print.business.config.DictionaryConfig;
|
|
import com.qmth.distributed.print.business.entity.BasicAttachment;
|
|
import com.qmth.distributed.print.business.entity.BasicAttachment;
|
|
@@ -14,6 +16,7 @@ import com.qmth.distributed.print.business.mapper.BasicAttachmentMapper;
|
|
import com.qmth.distributed.print.business.service.BasicAttachmentService;
|
|
import com.qmth.distributed.print.business.service.BasicAttachmentService;
|
|
import com.qmth.distributed.print.business.util.HtmlToPdfUtil;
|
|
import com.qmth.distributed.print.business.util.HtmlToPdfUtil;
|
|
import com.qmth.distributed.print.business.util.OssUtil;
|
|
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.ServletUtil;
|
|
import com.qmth.distributed.print.common.contant.SystemConstant;
|
|
import com.qmth.distributed.print.common.contant.SystemConstant;
|
|
import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
|
|
import com.qmth.distributed.print.common.enums.ExceptionResultEnum;
|
|
@@ -21,6 +24,7 @@ import com.qmth.distributed.print.common.util.HexUtils;
|
|
import com.qmth.distributed.print.common.util.ResultUtil;
|
|
import com.qmth.distributed.print.common.util.ResultUtil;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
@@ -30,9 +34,7 @@ import org.springframework.util.FileCopyUtils;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
-import java.io.File;
|
|
|
|
-import java.io.FileInputStream;
|
|
|
|
-import java.io.IOException;
|
|
|
|
|
|
+import java.io.*;
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.time.LocalDateTime;
|
|
import java.time.LocalDateTime;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
@@ -216,56 +218,85 @@ public class BasicAttachmentServiceImpl extends ServiceImpl<BasicAttachmentMappe
|
|
*/
|
|
*/
|
|
@Override
|
|
@Override
|
|
@Transactional
|
|
@Transactional
|
|
- public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> pdfList, Integer printCount) throws IOException {
|
|
|
|
- boolean oss = dictionaryConfig.sysDomain().isOss();
|
|
|
|
- JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
|
|
|
|
- String type = basicAttachment.getType();
|
|
|
|
- String filePath = null;
|
|
|
|
- String url = null;
|
|
|
|
- File htmlFile = null;
|
|
|
|
- if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.FTL_PREFIX)) {
|
|
|
|
- filePath = (String) jsonObject.get(SystemConstant.HTML_PATH);
|
|
|
|
- url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
|
|
|
|
- htmlFile = new File(url);
|
|
|
|
- } else if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.HTML_PREFIX)) {
|
|
|
|
- filePath = (String) jsonObject.get(SystemConstant.PATH);
|
|
|
|
- url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
|
|
|
|
- htmlFile = ossUtil.ossDownload(filePath, url);
|
|
|
|
- }
|
|
|
|
- String pdfDirName = filePath.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
|
|
|
|
- String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
|
|
|
|
- HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
|
|
|
|
- File pdfFile = new File(destUrl);
|
|
|
|
- if (!pdfFile.getParentFile().exists()) {
|
|
|
|
- pdfFile.getParentFile().mkdirs();
|
|
|
|
- pdfFile.createNewFile();
|
|
|
|
- }
|
|
|
|
- String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
|
|
|
|
- if (oss) {//上传至oss
|
|
|
|
- ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
|
|
|
|
- jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
|
|
|
|
|
|
+ public BasicAttachment saveAttachmentPdf(BasicAttachment basicAttachment, List<String> pdfList, Integer printCount) throws IOException, DocumentException {
|
|
|
|
+ try {
|
|
|
|
+ boolean oss = dictionaryConfig.sysDomain().isOss();
|
|
|
|
+ JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
|
|
|
|
+ String type = basicAttachment.getType();
|
|
|
|
+ String filePath = null;
|
|
|
|
+ String url = null;
|
|
|
|
+ File htmlFile = null;
|
|
if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.FTL_PREFIX)) {
|
|
if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.FTL_PREFIX)) {
|
|
- jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
|
|
|
|
- UploadFileEnum.FILE,
|
|
|
|
- UploadFileEnum.HTML,
|
|
|
|
- UploadFileEnum.PDF
|
|
|
|
- });
|
|
|
|
|
|
+ filePath = (String) jsonObject.get(SystemConstant.HTML_PATH);
|
|
|
|
+ url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
|
|
|
|
+ htmlFile = new File(url);
|
|
|
|
+ } else if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.HTML_PREFIX)) {
|
|
|
|
+ filePath = (String) jsonObject.get(SystemConstant.PATH);
|
|
|
|
+ url = SystemConstant.TEMP_FILES_DIR + File.separator + filePath;
|
|
|
|
+ htmlFile = ossUtil.ossDownload(filePath, url);
|
|
|
|
+ }
|
|
|
|
+ String pdfDirName = filePath.replaceAll(SystemConstant.HTML_PREFIX, SystemConstant.PDF_PREFIX).replaceAll(UploadFileEnum.HTML.name().toLowerCase(), UploadFileEnum.PDF.name().toLowerCase());
|
|
|
|
+ String destUrl = SystemConstant.PDF_TEMP_FILES_DIR + File.separator + pdfDirName;
|
|
|
|
+ HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
|
|
|
|
+ File pdfFile = new File(destUrl);
|
|
|
|
+ if (!pdfFile.getParentFile().exists()) {
|
|
|
|
+ pdfFile.getParentFile().mkdirs();
|
|
|
|
+ pdfFile.createNewFile();
|
|
|
|
+ }
|
|
|
|
+ String fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
|
|
|
|
+ if (oss) {//上传至oss
|
|
|
|
+ int pageCount = PdfUtil.addPdfPage(pdfFile);
|
|
|
|
+
|
|
|
|
+ ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
|
|
|
|
+ jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
|
|
|
|
+ if (Objects.nonNull(type) && Objects.equals(type, SystemConstant.FTL_PREFIX)) {
|
|
|
|
+ jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
|
|
|
|
+ UploadFileEnum.FILE,
|
|
|
|
+ UploadFileEnum.HTML,
|
|
|
|
+ UploadFileEnum.PDF
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
|
|
|
|
+ UploadFileEnum.FILE,
|
|
|
|
+ UploadFileEnum.PDF
|
|
|
|
+ });
|
|
|
|
+ htmlFile.delete();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ basicAttachment.setPath(jsonObject.toJSONString());
|
|
|
|
+ for (int i = 0; i < printCount; i++) {
|
|
|
|
+ pdfList.add(pdfFile.getPath());
|
|
|
|
+ }
|
|
|
|
+ this.updateById(basicAttachment);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("请求出错", e);
|
|
|
|
+ if (e instanceof ApiException) {
|
|
|
|
+ ResultUtil.error((ApiException) e, e.getMessage());
|
|
} else {
|
|
} else {
|
|
- jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
|
|
|
|
- UploadFileEnum.FILE,
|
|
|
|
- UploadFileEnum.PDF
|
|
|
|
- });
|
|
|
|
- htmlFile.delete();
|
|
|
|
|
|
+ ResultUtil.error(e.getMessage());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- basicAttachment.setPath(jsonObject.toJSONString());
|
|
|
|
- for (int i = 0; i < printCount; i++) {
|
|
|
|
- pdfList.add(pdfFile.getPath());
|
|
|
|
- }
|
|
|
|
- this.updateById(basicAttachment);
|
|
|
|
return basicAttachment;
|
|
return basicAttachment;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public static void main(String[] args) throws IOException, DocumentException {
|
|
|
|
+ File file = new File("/Users/king/Downloads/669ad9917f814bcabdd35201073885be.pdf");
|
|
|
|
+ PdfReader reader = new PdfReader(file.getPath());
|
|
|
|
+ int pageCount = reader.getNumberOfPages();
|
|
|
|
+ System.out.println("pageCount:" + pageCount);
|
|
|
|
+//
|
|
|
|
+ if (Math.abs(pageCount % 2) == 1) {//取偶
|
|
|
|
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
+ PdfStamper stamper = new PdfStamper(reader, baos);
|
|
|
|
+ stamper.insertPage(pageCount + 1, reader.getPageSizeWithRotation(1));
|
|
|
|
+ stamper.close();
|
|
|
|
+ FileOutputStream fileOutputStream = new FileOutputStream(file);
|
|
|
|
+ IOUtils.write(baos.toByteArray(), fileOutputStream);
|
|
|
|
+ }
|
|
|
|
+ pageCount = reader.getNumberOfPages();
|
|
|
|
+ System.out.println("pageCount:" + pageCount);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 删除附件
|
|
* 删除附件
|
|
*
|
|
*
|