Commit 7badd3a1 authored by hongguangwu's avatar hongguangwu

MVP1.7.11-是否自动生成解除劳动合同书 0否1是

parent 6a7130f1
......@@ -121,6 +121,12 @@
<version>2.0.27</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.3</version>
</dependency>
</dependencies>
<build>
......
package com.yifu.cloud.plus.v1.yifu.social.util;
import com.itextpdf.text.pdf.PdfWriter;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig;
......@@ -15,13 +16,11 @@ import org.jodconverter.local.office.LocalOfficeManager;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files;
import java.nio.file.Path;
......@@ -105,10 +104,14 @@ public class WordToImageUtil {
return "-";
}
// 在生产环境中,建议在整个应用生命周期中共享一个OfficeManager:
public OfficeManager officeManager;
public void initOfficeManager() {
this.officeManager = LocalOfficeManager.builder()
/*private static OfficeManager officeManager;
@PostConstruct
public void init() {
String tempDir = System.getProperty("java.io.tmpdir");
String officeHome = tempDir + "officeManager";
officeManager = LocalOfficeManager.builder()
.officeHome(officeHome)
.portNumbers(2003)
.build();
try {
......@@ -116,7 +119,8 @@ public class WordToImageUtil {
} catch (OfficeException e) {
e.printStackTrace();
}
}
}*/
public String convertAndAddSeal(String inputDocx, InputStream sealImagePath, String outputImagePath
, String projectRoot, RestTemplate restTemplate, SocialFriendConfig socialFriendConfig
, TSocialFriendBackLog backLog) throws IOException, OfficeException {
......@@ -124,9 +128,8 @@ public class WordToImageUtil {
File pdfFile = null;
File imgFile = null;
try {
initOfficeManager();
// Step 1: Word 转为 PDF(中间格式)
pdfFile = new File(inputDocx.replace(".docx", ".pdf"));
/*pdfFile = new File(inputDocx.replace(".docx", ".pdf"));
Map<String, Object> pdfProps = new HashMap<>();
pdfProps.put("FilterName", "writer_pdf_Export");
pdfProps.put("SelectPdfVersion", 1); // PDF/A-1
......@@ -136,7 +139,9 @@ public class WordToImageUtil {
.build()
.convert(new File(inputDocx))
.to(pdfFile)
.execute();
.execute();*/
convertDocxToPdf(inputDocx, PDF_TEMP);
// Step 2: PDF 转为 PNG 图片(可使用 Apache PDFBox 或 ImageMagick 转换)
BufferedImage pageImage = convertPdfToImage(pdfFile.getAbsolutePath());
......@@ -172,6 +177,22 @@ public class WordToImageUtil {
return "---";
}
public void convertDocxToPdf(String inputPath, String outputPath) throws Exception {
FileInputStream fis = new FileInputStream(inputPath);
XWPFDocument document = new XWPFDocument(fis);
com.itextpdf.text.Document pdfDoc = new com.itextpdf.text.Document();
PdfWriter.getInstance(pdfDoc, new FileOutputStream(outputPath));
pdfDoc.open();
for (XWPFParagraph para : document.getParagraphs()) {
pdfDoc.add(new com.itextpdf.text.Paragraph(para.getText()));
}
pdfDoc.close();
document.close();
}
private File compressImage(String projectRoot, File originalFile) throws IOException {
BufferedImage image = ImageIO.read(originalFile);
int quality = 100; // 从100%开始
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment