|
@@ -45,6 +45,7 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
import javax.annotation.Resource;
|
|
|
import java.io.File;
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
import java.io.IOException;
|
|
|
import java.math.BigDecimal;
|
|
|
import java.time.LocalDateTime;
|
|
@@ -332,6 +333,103 @@ public class PrintCommonServiceServiceImpl implements PrintCommonService {
|
|
|
return basicAttachment;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 保存html附件和该html转成pdf的附件
|
|
|
+ * @param fileName 文件名称
|
|
|
+ * @param htmlContent html内容
|
|
|
+ * @param userId 创建人
|
|
|
+ * @return 两个附件对象
|
|
|
+ */
|
|
|
+ @Transactional
|
|
|
+ @Override
|
|
|
+ public BasicAttachment saveAttachmentHtmlAndPdf(String fileName, String htmlContent, Long userId) {
|
|
|
+ BasicAttachment basicAttachment = null;
|
|
|
+ try {
|
|
|
+ byte[] bytes = htmlContent.getBytes();
|
|
|
+ int size = bytes.length;
|
|
|
+ boolean oss = dictionaryConfig.sysDomain().isOss();
|
|
|
+ LocalDateTime nowTime = LocalDateTime.now();
|
|
|
+ StringJoiner stringJoiner = new StringJoiner("");
|
|
|
+ if (!oss) {
|
|
|
+ stringJoiner.add(SystemConstant.TEMP_FILES_DIR).add(File.separator);
|
|
|
+ }
|
|
|
+ stringJoiner.add(UploadFileEnum.HTML.getTitle()).add(File.separator)
|
|
|
+ .add(String.valueOf(nowTime.getYear())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getDayOfMonth()));
|
|
|
+
|
|
|
+ JSONObject jsonObject = new JSONObject();
|
|
|
+ stringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.HTML_PREFIX);
|
|
|
+ String fileMd5 = null;
|
|
|
+
|
|
|
+ if (oss) {//上传至oss
|
|
|
+ String dirName = stringJoiner.toString().replaceAll("\\\\", "/");
|
|
|
+ fileStoreUtil.ossUpload(dirName, htmlContent, fileStoreUtil.getUploadEnumByPath(dirName).getFssType());
|
|
|
+ jsonObject.put(SystemConstant.TYPE, SystemConstant.OSS);
|
|
|
+ jsonObject.put(SystemConstant.PATH, dirName);
|
|
|
+ String url = SystemConstant.TEMP_FILES_DIR + File.separator + dirName;
|
|
|
+ File localHtmlFile = fileStoreUtil.ossDownload(dirName, url, fileStoreUtil.getUploadEnumByPath(dirName).getFssType());
|
|
|
+ StringJoiner pdfStringJoiner = new StringJoiner("");
|
|
|
+ pdfStringJoiner.add(UploadFileEnum.PDF.getTitle()).add(File.separator);
|
|
|
+ pdfStringJoiner.add(String.valueOf(nowTime.getYear())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getMonthValue())).add(File.separator)
|
|
|
+ .add(String.format("%02d", nowTime.getDayOfMonth()));
|
|
|
+ pdfStringJoiner.add(File.separator).add(SystemConstant.getUuid()).add(SystemConstant.PDF_PREFIX);
|
|
|
+
|
|
|
+ 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 = asposePdfUtil.documentToPdf(localHtmlFile.getPath(), destUrl, PaperSize.A3);
|
|
|
+ File pdfFile = new File(destUrl);
|
|
|
+ if (!pdfFile.exists()) {
|
|
|
+ pdfFile.getParentFile().mkdirs();
|
|
|
+ pdfFile.createNewFile();
|
|
|
+ }
|
|
|
+ fileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
|
|
|
+// ossUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)));
|
|
|
+ pdfDirName = pdfDirName.replaceAll("\\\\", "/");
|
|
|
+ fileStoreUtil.ossUpload(pdfDirName, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(fileMd5)), fileStoreUtil.getUploadEnumByPath(pdfDirName).getFssType());
|
|
|
+// localHtmlFile.delete();
|
|
|
+ jsonObject.put(SystemConstant.PDF_PATH, pdfDirName);
|
|
|
+ // htmlMd5
|
|
|
+ jsonObject.put("htmlMd5", DigestUtils.md5Hex(new FileInputStream(localHtmlFile)));
|
|
|
+ jsonObject.put("pdfMd5", fileMd5);
|
|
|
+ }else {//上传至服务器
|
|
|
+ File finalFile = new File(stringJoiner.toString());
|
|
|
+ if (!finalFile.exists()) {
|
|
|
+ finalFile.getParentFile().mkdirs();
|
|
|
+ finalFile.createNewFile();
|
|
|
+ }
|
|
|
+ FileCopyUtils.copy(bytes, new File(stringJoiner.toString()));
|
|
|
+ fileMd5 = DigestUtils.md5Hex(new FileInputStream(stringJoiner.toString()));
|
|
|
+ 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);
|
|
|
+ jsonObject.put(SystemConstant.PDF_PATH, destUrl);
|
|
|
+ jsonObject.put("htmlMd5", DigestUtils.md5Hex(new FileInputStream(finalFile)));
|
|
|
+ jsonObject.put("pdfMd5", fileMd5);
|
|
|
+ }
|
|
|
+ jsonObject.put(SystemConstant.UPLOAD_TYPE, new UploadFileEnum[]{
|
|
|
+ UploadFileEnum.HTML, UploadFileEnum.PDF
|
|
|
+ });
|
|
|
+ basicAttachment = new BasicAttachment(jsonObject.toJSONString(), fileName, SystemConstant.HTML_PREFIX, new BigDecimal(size), fileMd5, userId);
|
|
|
+ basicAttachmentService.save(basicAttachment);
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("请求出错", e);
|
|
|
+ basicAttachmentService.deleteAttachment(basicAttachment);
|
|
|
+ if (e instanceof ApiException) {
|
|
|
+ ResultUtil.error((ApiException) e, e.getMessage());
|
|
|
+ } else {
|
|
|
+ ResultUtil.error(e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return basicAttachment;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 保存附件公用
|
|
|
*
|