Commit a537a4b4 authored by hongguangwu's avatar hongguangwu

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

parent 7badd3a1
...@@ -107,24 +107,15 @@ ...@@ -107,24 +107,15 @@
<type>jar</type> <type>jar</type>
</dependency> </dependency>
<!-- word转pdf -->
<dependency> <dependency>
<groupId>org.jodconverter</groupId> <groupId>com.deepoove</groupId>
<artifactId>jodconverter-local</artifactId> <artifactId>poi-tl</artifactId>
<version>4.4.0</version> <version>1.8.0</version>
</dependency> </dependency>
<!-- pdf转图片 -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency>
<dependency> <dependency>
<groupId>com.itextpdf</groupId> <groupId>com.luhuiguo</groupId>
<artifactId>itextpdf</artifactId> <artifactId>aspose-words</artifactId>
<version>5.5.13.3</version> <version>23.1</version>
</dependency> </dependency>
</dependencies> </dependencies>
......
...@@ -27,8 +27,6 @@ import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig; ...@@ -27,8 +27,6 @@ import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig;
import com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog; import com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog;
import com.yifu.cloud.plus.v1.yifu.social.mapper.TAttaInfoMapper; import com.yifu.cloud.plus.v1.yifu.social.mapper.TAttaInfoMapper;
import com.yifu.cloud.plus.v1.yifu.social.util.WordToImageUtil; import com.yifu.cloud.plus.v1.yifu.social.util.WordToImageUtil;
import com.yifu.cloud.plus.v1.yifu.social.util.WordToImageWithSeal;
import com.yifu.cloud.plus.v1.yifu.social.util.WordUtil;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
...@@ -121,31 +119,4 @@ public class TSocialFriendWordController { ...@@ -121,31 +119,4 @@ public class TSocialFriendWordController {
} }
return R.ok(); return R.ok();
} }
public R<String> replaceText(String inputPath, String outputPath, String outputImagePath) throws Exception {
String src = "1735185038602皖信.png";
URL url = ossUtil.getObjectUrl(null, src);
if (Common.isEmpty(url)) {
throw new RuntimeException("获取印章图片失败");
}
try (InputStream imageStream = url.openStream()) {
// 准备替换参数
Map<String, String> replacements = new HashMap<>();
replacements.put("empName", "张三丰");
replacements.put("empSex", "女");
replacements.put("empAge", "33");
replacements.put("idCard", "340826199012121212");
replacements.put("startDate", "2023年11月28日");
replacements.put("leaveReason", "个人原因");
replacements.put("leaveDate", "2025年01月20日");
replacements.put("nowDate", DateUtil.formatDatePatten(new Date(), DateUtil.CHINA_PATTEN_YMD));
// 执行替换 + 插入印章
WordUtil.replaceTextInWord(inputPath, outputPath, replacements);
WordToImageWithSeal.convertAndAddSeal(outputPath, imageStream, outputImagePath);
}
return R.ok();
}
} }
package com.yifu.cloud.plus.v1.yifu.social.util; package com.yifu.cloud.plus.v1.yifu.social.util;
import com.itextpdf.text.pdf.PdfWriter; import com.aspose.words.Document;
import com.aspose.words.SaveFormat;
import com.yifu.cloud.plus.v1.yifu.common.core.constant.CommonConstants; 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.common.core.util.Common;
import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig; import com.yifu.cloud.plus.v1.yifu.social.config.SocialFriendConfig;
import com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog; import com.yifu.cloud.plus.v1.yifu.social.entity.TSocialFriendBackLog;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.poi.xwpf.usermodel.*; import org.apache.poi.xwpf.usermodel.*;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
import javax.imageio.ImageIO; import javax.imageio.ImageIO;
import java.awt.*; import java.awt.*;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.nio.file.DirectoryNotEmptyException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Base64; import java.util.Base64;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
/** /**
...@@ -83,16 +72,6 @@ public class WordToImageUtil { ...@@ -83,16 +72,6 @@ public class WordToImageUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
Path outputPath = Paths.get(outputFilePath);
if (Files.exists(outputPath)) {
try {
Files.delete(outputPath); // 如果文件不存在会抛异常,先判断是否存在
} catch (DirectoryNotEmptyException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
if (imageStream != null) { if (imageStream != null) {
try { try {
imageStream.close(); imageStream.close();
...@@ -104,48 +83,32 @@ public class WordToImageUtil { ...@@ -104,48 +83,32 @@ public class WordToImageUtil {
return "-"; return "-";
} }
/*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 {
officeManager.start();
} catch (OfficeException e) {
e.printStackTrace();
}
}*/
public String convertAndAddSeal(String inputDocx, InputStream sealImagePath, String outputImagePath public String convertAndAddSeal(String inputDocx, InputStream sealImagePath, String outputImagePath
, String projectRoot, RestTemplate restTemplate, SocialFriendConfig socialFriendConfig , String projectRoot, RestTemplate restTemplate, SocialFriendConfig socialFriendConfig
, TSocialFriendBackLog backLog) throws IOException, OfficeException { , TSocialFriendBackLog backLog) throws Exception {
File pdfFile = null; File pdfFile = null;
Document doc = null;
File tempImgFile = null;
File imgFile = null; File imgFile = null;
try { try {
// Step 1: Word 转为 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
LocalConverter.builder()
.officeManager(officeManager)
.storeProperties(pdfProps)
.build()
.convert(new File(inputDocx))
.to(pdfFile)
.execute();*/
convertDocxToPdf(inputDocx, PDF_TEMP);
// Step 2: PDF 转为 PNG 图片(可使用 Apache PDFBox 或 ImageMagick 转换)
BufferedImage pageImage = convertPdfToImage(pdfFile.getAbsolutePath());
pdfFile = new File(inputDocx);
String filename = pdfFile.getName();
// 打开生成的 Word 文件
doc = new Document(new FileInputStream(pdfFile));
String pathPre = filename.substring(0, filename.lastIndexOf("."));
// 逐页将 Word 文件保存为图片(PNG格式)
String path = pathPre + ".png";
if (doc.getPageCount() > 0) {
Document extractedPage = doc.extractPages(0,1);
// 拼接上文件名 test1.png
// 将 Word 文件保存为图片PNG格式
extractedPage.save(path, SaveFormat.PNG);
}
tempImgFile = new File(path);
BufferedImage pageImage = ImageIO.read(tempImgFile);
// Step 3: 加盖公章 // Step 3: 加盖公章
BufferedImage sealedImage = addSealToImage(pageImage, sealImagePath); BufferedImage sealedImage = addSealToImage(pageImage, sealImagePath);
...@@ -167,30 +130,21 @@ public class WordToImageUtil { ...@@ -167,30 +130,21 @@ public class WordToImageUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} finally { } finally {
if (tempImgFile != null) {
tempImgFile.delete();
}
if (doc != null) {
doc.cleanup();
}
if (pdfFile != null) { if (pdfFile != null) {
pdfFile.delete(); pdfFile.delete();
} }
if (imgFile != null) { if (imgFile != null) {
imgFile.delete(); imgFile.delete();
} }
}
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()));
} }
return "---";
pdfDoc.close();
document.close();
} }
private File compressImage(String projectRoot, File originalFile) throws IOException { private File compressImage(String projectRoot, File originalFile) throws IOException {
...@@ -211,30 +165,36 @@ public class WordToImageUtil { ...@@ -211,30 +165,36 @@ public class WordToImageUtil {
return originalFile; return originalFile;
} }
private BufferedImage convertPdfToImage(String pdfPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(pdfPath))) {
PDFRenderer pdfRenderer = new PDFRenderer(document);
// 渲染第一页为图像,分辨率为 300 DPI
return pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
}
}
private BufferedImage addSealToImage(BufferedImage image, InputStream sealImagePath) throws IOException { private BufferedImage addSealToImage(BufferedImage image, InputStream sealImagePath) throws IOException {
BufferedImage seal = ImageIO.read(sealImagePath); BufferedImage seal = ImageIO.read(sealImagePath);
// 设置公章目标大小
int targetWidth = 200; // 宽度
int targetHeight = 200; // 高度
// 创建缩放后的公章图像
Image scaledSeal = seal.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH);
BufferedImage resizedSeal = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = resizedSeal.createGraphics();
g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2d.drawImage(scaledSeal, 0, 0, null);
g2d.dispose();
// 合成最终图像
BufferedImage combined = new BufferedImage( BufferedImage combined = new BufferedImage(
image.getWidth(), image.getWidth(),
image.getHeight(), image.getHeight(),
BufferedImage.TYPE_INT_ARGB); BufferedImage.TYPE_INT_ARGB);
Graphics2D g = combined.createGraphics(); Graphics2D g = combined.createGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.drawImage(image, 0, 0, null); g.drawImage(image, 0, 0, null);
// 设置公章位置(右下角) // 设置公章位置(右下角)
int x = 1600; int x = 550;
int y = 1150; int y = 400;
g.drawImage(seal, x, y, null); g.drawImage(resizedSeal, x, y, null);
g.dispose(); g.dispose();
return combined; return combined;
......
package com.yifu.cloud.plus.v1.yifu.social.util;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
/**
* @author hgw2
* @description word转图片
* @date 2025/5/15
*/
public class WordToImageWithSeal {
// 在生产环境中,建议在整个应用生命周期中共享一个OfficeManager:
public static OfficeManager OFFICE_MANAGER;
static {
OFFICE_MANAGER = LocalOfficeManager.builder()
.portNumbers(2002)
.build();
try {
OFFICE_MANAGER.start();
} catch (OfficeException e) {
e.printStackTrace();
}
}
public static void convertAndAddSeal(String inputDocx, InputStream sealImagePath, String outputImagePath) throws IOException, OfficeException {
// Step 1: Word 转为 PDF(中间格式)
File 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
LocalConverter.builder()
.officeManager(OFFICE_MANAGER)
.storeProperties(pdfProps)
.build()
.convert(new File(inputDocx))
.to(pdfFile)
.execute();
// Step 2: PDF 转为 PNG 图片(可使用 Apache PDFBox 或 ImageMagick 转换)
BufferedImage pageImage = convertPdfToImage(pdfFile.getAbsolutePath());
// Step 3: 加盖公章
BufferedImage sealedImage = addSealToImage(pageImage, sealImagePath);
// Step 4: 输出图片
ImageIO.write(sealedImage, "png", new File(outputImagePath));
}
private static BufferedImage convertPdfToImage(String pdfPath) throws IOException {
try (PDDocument document = PDDocument.load(new File(pdfPath))) {
PDFRenderer pdfRenderer = new PDFRenderer(document);
// 渲染第一页为图像,分辨率为 300 DPI
return pdfRenderer.renderImageWithDPI(0, 300, ImageType.RGB);
}
}
private static BufferedImage addSealToImage(BufferedImage image, InputStream sealImagePath) throws IOException {
BufferedImage seal = ImageIO.read(sealImagePath);
BufferedImage combined = new BufferedImage(
image.getWidth(),
image.getHeight(),
BufferedImage.TYPE_INT_ARGB);
Graphics2D g = combined.createGraphics();
g.drawImage(image, 0, 0, null);
// 设置公章位置(右下角)
int x = 1600;
int y = 1150;
g.drawImage(seal, x, y, null);
g.dispose();
return combined;
}
}
package com.yifu.cloud.plus.v1.yifu.social.util;
import com.yifu.cloud.plus.v1.yifu.common.core.util.Common;
import org.apache.poi.xwpf.usermodel.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
/**
* @author hgw2
* @description word工具类
* @date 2025/5/15
*/
public class WordUtil {
public static void replaceTextInWord(String filePath, String outputFilePath, Map<String, String> replacements
) throws IOException {
try (FileInputStream fis = new FileInputStream(filePath);
XWPFDocument document = new XWPFDocument(fis)) {
// 替换段落中的文本
for (XWPFParagraph paragraph : document.getParagraphs()) {
for (String key : replacements.keySet()) {
if (paragraph.getText().contains(key)) {
String newText = paragraph.getText().replace(key, replacements.get(key));
paragraph.getRuns().clear();
XWPFRun newRun = paragraph.createRun();
newRun.setText(newText);
}
}
}
String text;
// 替换表格中的文本
for (XWPFTable table : document.getTables()) {
for (XWPFTableRow row : table.getRows()) {
for (XWPFTableCell cell : row.getTableCells()) {
for (XWPFParagraph p : cell.getParagraphs()) {
for (XWPFRun run : p.getRuns()) {
text = run.getText(0);
if (Common.isNotNull(text) && replacements.get(text) != null) {
run.setText(replacements.get(text), 0);
}
}
}
}
}
}
// 保存输出文件
try (FileOutputStream fos = new FileOutputStream(outputFilePath)) {
document.write(fos);
}
}
}
}
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