Commit 116526fd authored by 徐俊's avatar 徐俊

xujun

parent 9d182f57
...@@ -163,21 +163,28 @@ ...@@ -163,21 +163,28 @@
<artifactId>poi-tl</artifactId> <artifactId>poi-tl</artifactId>
<version>1.10.0</version> <version>1.10.0</version>
</dependency> </dependency>
<!-- Apache POI - 用于处理 Microsoft Office 文档 -->
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId> <artifactId>poi</artifactId>
<version>4.1.2</version> <version>5.2.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId> <artifactId>poi-ooxml</artifactId>
<version>4.1.2</version> <version>5.2.3</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.poi</groupId> <groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId> <artifactId>poi-scratchpad</artifactId>
<version>4.1.2</version> <version>5.2.3</version>
</dependency>
<!-- Apache PDFBox - 用于创建和操作 PDF -->
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.27</version>
</dependency> </dependency>
<dependency> <dependency>
......
...@@ -215,6 +215,7 @@ public class ComProjectController extends BaseController<ComProjectService, ComP ...@@ -215,6 +215,7 @@ public class ComProjectController extends BaseController<ComProjectService, ComP
@RequestMapping("/projectExport/{id}") @RequestMapping("/projectExport/{id}")
@PostMapping @PostMapping
public ResponseDataModel<String> projectExport(@PathVariable String id) throws DocumentException, IOException { public ResponseDataModel<String> projectExport(@PathVariable String id) throws DocumentException, IOException {
ComProjectDTO dto = comProjectService.getProjectById(id); ComProjectDTO dto = comProjectService.getProjectById(id);
List<SystemParameter> list = systemParameterService.getListByType(67); List<SystemParameter> list = systemParameterService.getListByType(67);
String outputPath = "D:\\申请书和合同书.pdf"; String outputPath = "D:\\申请书和合同书.pdf";
......
package com.yiboshi.science.utils;public class WordToPdfConverter { package com.yiboshi.science.utils;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.util.List;
public class WordToPdfConverter {
private static final Logger logger = LoggerFactory.getLogger(WordToPdfConverter.class);
/**
* 将Word文档转换为PDF
* @param wordPath Word文档路径
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public static boolean convertToPdf(String wordPath, String pdfPath) {
// 检查文件是否存在
File wordFile = new File(wordPath);
if (!wordFile.exists()) {
logger.error("Word文件不存在: {}", wordPath);
return false;
}
// 检查输出目录是否存在
File pdfFile = new File(pdfPath);
File parentDir = pdfFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
try {
if (wordPath.toLowerCase().endsWith(".docx")) {
return convertDocxToPdf(wordPath, pdfPath);
} else if (wordPath.toLowerCase().endsWith(".doc")) {
return convertDocToPdf(wordPath, pdfPath);
} else {
logger.error("不支持的文件格式: {}", wordPath);
return false;
}
} catch (Exception e) {
logger.error("Word转PDF失败", e);
return false;
}
}
/**
* 将Word文档转换为PDF(字节数组版本)
* @param wordBytes Word文档的字节数组
* @param pdfPath 输出PDF路径
* @return 转换是否成功
*/
public static boolean convertToPdf(byte[] wordBytes, String pdfPath) {
if (wordBytes == null || wordBytes.length == 0) {
logger.error("Word文档字节数组为空");
return false;
}
// 检查输出目录是否存在
File pdfFile = new File(pdfPath);
File parentDir = pdfFile.getParentFile();
if (!parentDir.exists()) {
parentDir.mkdirs();
}
try {
// 创建临时文件
File tempFile = File.createTempFile("temp", ".docx");
try (FileOutputStream fos = new FileOutputStream(tempFile)) {
fos.write(wordBytes);
}
// 转换文件
boolean result = convertToPdf(tempFile.getAbsolutePath(), pdfPath);
// 删除临时文件
tempFile.delete();
return result;
} catch (Exception e) {
logger.error("Word转PDF失败", e);
return false;
}
}
/**
* 转换DOCX文件到PDF
*/
private static boolean convertDocxToPdf(String docxPath, String pdfPath) throws IOException {
try (FileInputStream fis = new FileInputStream(docxPath);
XWPFDocument document = new XWPFDocument(fis);
PDDocument pdfDocument = new PDDocument()) {
// 获取文档内容
List<XWPFParagraph> paragraphs = document.getParagraphs();
// 创建PDF页面
PDPage page = new PDPage();
pdfDocument.addPage(page);
// 加载字体
PDType0Font font = PDType0Font.load(pdfDocument,
WordToPdfConverter.class.getResourceAsStream("/fonts/simsun.ttc"));
// 创建内容流
try (PDPageContentStream contentStream =
new PDPageContentStream(pdfDocument, page)) {
float y = page.getMediaBox().getHeight() - 50;
// 写入段落
for (XWPFParagraph para : paragraphs) {
String text = para.getText();
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(50, y);
contentStream.showText(text);
contentStream.endText();
y -= 15;
}
}
// 保存PDF
pdfDocument.save(pdfPath);
return true;
}
}
/**
* 转换DOC文件到PDF
*/
private static boolean convertDocToPdf(String docPath, String pdfPath) throws IOException {
try (FileInputStream fis = new FileInputStream(docPath);
HWPFDocument document = new HWPFDocument(fis);
PDDocument pdfDocument = new PDDocument()) {
// 获取文档内容
String text = document.getDocumentText();
// 创建PDF页面
PDPage page = new PDPage();
pdfDocument.addPage(page);
// 加载字体
PDType0Font font = PDType0Font.load(pdfDocument,
WordToPdfConverter.class.getResourceAsStream("/fonts/simsun.ttc"));
// 创建内容流
try (PDPageContentStream contentStream =
new PDPageContentStream(pdfDocument, page)) {
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(50, page.getMediaBox().getHeight() - 50);
contentStream.showText(text);
contentStream.endText();
}
// 保存PDF
pdfDocument.save(pdfPath);
return true;
}
}
/**
* 检查文件是否为Word文档
*/
public static boolean isWordFile(String fileName) {
if (fileName == null || fileName.trim().isEmpty()) {
return false;
}
String lowerFileName = fileName.toLowerCase();
return lowerFileName.endsWith(".doc") || lowerFileName.endsWith(".docx");
}
} }
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