|
@@ -1,82 +0,0 @@
|
|
|
-package com.ims.eval.util;
|
|
|
-
|
|
|
-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.*;
|
|
|
-
|
|
|
-/**
|
|
|
- * @author hlf
|
|
|
- * @date 2023/5/25 17:45
|
|
|
- * 文件说明:
|
|
|
- */
|
|
|
-public class FileConvertUtil {
|
|
|
-
|
|
|
- /**
|
|
|
- * 将 DOCX 文件转换为 PDF 格式并保存在上传路径下
|
|
|
- *
|
|
|
- * @param docxFile 需要转换的 DOCX 文件
|
|
|
- * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
|
|
|
- * @throws IOException 抛出 IO 异常
|
|
|
- */
|
|
|
- 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);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将 XLS、XLSX 文件转换为 PDF 格式并保存在上传路径下
|
|
|
- *
|
|
|
- * @param excelFile 需要转换的 Excel 文件对象
|
|
|
- * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
|
|
|
- * @throws IOException 抛出 IO 异常
|
|
|
- */
|
|
|
- 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();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 将 DOC 文件转换为 PDF 格式并保存在上传路径下
|
|
|
- *
|
|
|
- * @param docFile 需要转换的 DOC 文件对象
|
|
|
- * @param pdfPath 转换生成的 PDF 文件在服务器中存放的路径
|
|
|
- * @throws IOException 抛出 IO 异常
|
|
|
- */
|
|
|
- public static void convertDocToPdf(File docFile, String pdfPath) throws IOException {
|
|
|
- convertDocxToPdf(docFile, pdfPath);
|
|
|
- }
|
|
|
-}
|