|
@@ -1,94 +1,82 @@
|
|
|
package com.ims.eval.util;
|
|
|
|
|
|
-import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
|
|
|
-import com.artofsolving.jodconverter.DocumentConverter;
|
|
|
-import com.artofsolving.jodconverter.DocumentFormat;
|
|
|
-import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
|
|
|
-import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
|
|
|
-import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
|
|
|
+import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
|
|
|
+import org.apache.pdfbox.pdmodel.PDDocument;
|
|
|
+import org.apache.pdfbox.pdmodel.PDPage;
|
|
|
+import org.apache.pdfbox.pdmodel.PDPageContentStream;
|
|
|
+import org.apache.pdfbox.pdmodel.font.PDType1Font;
|
|
|
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
|
|
|
+import org.apache.poi.ss.usermodel.Workbook;
|
|
|
+import org.apache.poi.ss.usermodel.WorkbookFactory;
|
|
|
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
|
|
|
|
|
|
import java.io.*;
|
|
|
-import java.net.HttpURLConnection;
|
|
|
-import java.net.URL;
|
|
|
-import java.net.URLConnection;
|
|
|
-import java.nio.file.Files;
|
|
|
|
|
|
/**
|
|
|
* @author hlf
|
|
|
- * @date 2023/5/25 14:22
|
|
|
- * 文件说明:文件格式转换工具类
|
|
|
+ * @date 2023/5/25 17:45
|
|
|
+ * 文件说明:
|
|
|
*/
|
|
|
public class FileConvertUtil {
|
|
|
|
|
|
/**
|
|
|
- * 默认转换后文件后缀
|
|
|
- */
|
|
|
- private static final String DEFAULT_SUFFIX = "pdf";
|
|
|
- /**
|
|
|
- * openoffice_port
|
|
|
- */
|
|
|
- private static final Integer OPENOFFICE_PORT = 8100;
|
|
|
-
|
|
|
- /**
|
|
|
- * 方法描述 office文档转换为PDF(处理本地文件)
|
|
|
+ * 将 DOCX 文件转换为 PDF 格式并保存在上传路径下
|
|
|
*
|
|
|
- * @param sourcePath 源文件路径
|
|
|
- * @param suffix 源文件后缀
|
|
|
- * @return InputStream 转换后文件输入流
|
|
|
+ * @param docxFile 需要转换的 DOCX 文件
|
|
|
+ * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
|
|
|
+ * @throws IOException 抛出 IO 异常
|
|
|
*/
|
|
|
- public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
|
|
|
- File inputFile = new File(sourcePath);
|
|
|
- InputStream inputStream = Files.newInputStream(inputFile.toPath());
|
|
|
- return covertCommonByStream(inputStream, suffix);
|
|
|
+ public static void convertDocxToPdf(File docxFile, String pdfPath) throws IOException {
|
|
|
+ try (InputStream inputStream = new FileInputStream(docxFile)) {
|
|
|
+ XWPFDocument document = new XWPFDocument(inputStream);
|
|
|
+ File targetFile = new File(pdfPath);
|
|
|
+ targetFile.getParentFile().mkdirs();
|
|
|
+ try (OutputStream outputStream = new FileOutputStream(targetFile)) {
|
|
|
+ PdfConverter.getInstance().convert(document, outputStream, null);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 方法描述 office文档转换为PDF(处理网络文件)
|
|
|
+ * 将 XLS、XLSX 文件转换为 PDF 格式并保存在上传路径下
|
|
|
*
|
|
|
- * @param netFileUrl 网络文件路径
|
|
|
- * @param suffix 文件后缀
|
|
|
- * @return InputStream 转换后文件输入流
|
|
|
+ * @param excelFile 需要转换的 Excel 文件对象
|
|
|
+ * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
|
|
|
+ * @throws IOException 抛出 IO 异常
|
|
|
*/
|
|
|
- public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {
|
|
|
- // 创建URL
|
|
|
- URL url = new URL(netFileUrl);
|
|
|
- // 试图连接并取得返回状态码
|
|
|
- URLConnection urlconn = url.openConnection();
|
|
|
- urlconn.connect();
|
|
|
- HttpURLConnection httpconn = (HttpURLConnection) urlconn;
|
|
|
- int httpResult = httpconn.getResponseCode();
|
|
|
- if (httpResult == HttpURLConnection.HTTP_OK) {
|
|
|
- InputStream inputStream = urlconn.getInputStream();
|
|
|
- return covertCommonByStream(inputStream, suffix);
|
|
|
+ public static void convertExcelToPdf(File excelFile, String pdfPath) throws IOException, InvalidFormatException {
|
|
|
+ try (InputStream inputStream = new FileInputStream(excelFile)) {
|
|
|
+ Workbook workbook = WorkbookFactory.create(inputStream);
|
|
|
+ PDDocument pdfDocument = new PDDocument();
|
|
|
+ for (int i = 0; i < workbook.getNumberOfSheets(); i++) {
|
|
|
+ PDPage page = new PDPage();
|
|
|
+ pdfDocument.addPage(page);
|
|
|
+ PDPageContentStream contentStream = new PDPageContentStream(pdfDocument, page);
|
|
|
+ contentStream.beginText();
|
|
|
+ contentStream.setFont(PDType1Font.TIMES_BOLD, 12);
|
|
|
+ contentStream.setLeading(14.5f);
|
|
|
+ contentStream.newLineAtOffset(25, 725);
|
|
|
+ String[] sheetData = workbook.getSheetAt(i).getRow(0).toString().split(",");
|
|
|
+ for (String data : sheetData) {
|
|
|
+ contentStream.showText(data.trim());
|
|
|
+ contentStream.newLine();
|
|
|
+ }
|
|
|
+ contentStream.endText();
|
|
|
+ contentStream.close();
|
|
|
+ }
|
|
|
+ pdfDocument.save(pdfPath);
|
|
|
+ pdfDocument.close();
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 方法描述 将文件以流的形式转换
|
|
|
+ * 将 DOC 文件转换为 PDF 格式并保存在上传路径下
|
|
|
*
|
|
|
- * @param inputStream 源文件输入流
|
|
|
- * @param suffix 源文件后缀
|
|
|
- * @return InputStream 转换后文件输入流
|
|
|
- */
|
|
|
- public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {
|
|
|
- ByteArrayOutputStream out = new ByteArrayOutputStream();
|
|
|
- OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_PORT);
|
|
|
- connection.connect();
|
|
|
- DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
|
|
|
- DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
|
|
|
- DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);
|
|
|
- DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);
|
|
|
- converter.convert(inputStream, sourceFormat, out, targetFormat);
|
|
|
- connection.disconnect();
|
|
|
- return outputStreamConvertInputStream(out);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 方法描述 outputStream转inputStream
|
|
|
+ * @param docFile 需要转换的 DOC 文件对象
|
|
|
+ * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
|
|
|
+ * @throws IOException 抛出 IO 异常
|
|
|
*/
|
|
|
- public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
|
|
|
- ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
|
|
|
- return new ByteArrayInputStream(baos.toByteArray());
|
|
|
+ public static void convertDocToPdf(File docFile, String pdfPath) throws IOException {
|
|
|
+ convertDocxToPdf(docFile, pdfPath);
|
|
|
}
|
|
|
}
|