|
|
@@ -5,13 +5,9 @@ import io.minio.MinioClient;
|
|
|
import io.minio.UploadObjectArgs;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
-import org.springframework.scheduling.annotation.Async;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import java.io.File;
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.IOException;
|
|
|
-import java.nio.file.Files;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
@Component
|
|
|
@@ -56,16 +52,16 @@ public class PdfUtils {
|
|
|
}
|
|
|
|
|
|
// @Async
|
|
|
- public void convertToPdf(String objectName, String fileId) throws Exception {
|
|
|
+ public String convertToPdf(String objectName, String fileId) throws Exception {
|
|
|
long start = System.currentTimeMillis();
|
|
|
File tempFile = null;
|
|
|
-
|
|
|
+ String outputPath=null;
|
|
|
try {
|
|
|
// 1. 下载源文件
|
|
|
tempFile = downloadFile(objectName);
|
|
|
|
|
|
// 2. 执行转换
|
|
|
- String outputPath = convertToPdfLocal(tempFile.getAbsolutePath(), "convert");
|
|
|
+ outputPath = convertToPdfLocal(tempFile.getName(),tempFile.getAbsolutePath(), "convert");
|
|
|
|
|
|
// 3. 上传转换结果
|
|
|
uploadToMinio(outputPath, fileId);
|
|
|
@@ -74,9 +70,11 @@ public class PdfUtils {
|
|
|
if (tempFile != null && tempFile.exists()) {
|
|
|
tempFile.delete();
|
|
|
}
|
|
|
- cleanTempDir();
|
|
|
+ cleanTempDir(tempFile.getAbsolutePath(),outputPath);
|
|
|
}
|
|
|
System.out.println("总耗时:" + (System.currentTimeMillis() - start) + "ms");
|
|
|
+
|
|
|
+ return outputPath;
|
|
|
}
|
|
|
|
|
|
private File downloadFile(String objectName) throws Exception {
|
|
|
@@ -105,46 +103,53 @@ public class PdfUtils {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private String convertToPdfLocal(String filePath, String targetFolder) {
|
|
|
- String osName = System.getProperty("os.name").toLowerCase();
|
|
|
- String outputPath = tempDir + "converted_" + System.currentTimeMillis() + ".pdf";
|
|
|
+ private String convertToPdfLocal(String fileName,String filePath, String targetFolder) {
|
|
|
+ // String osName = System.getProperty("os.name").toLowerCase();
|
|
|
+ fileName=fileName.substring(0, fileName.lastIndexOf("."));
|
|
|
+ String outputPath = tempDir + fileName + ".pdf";
|
|
|
// 在执行转换前确保输出目录存在
|
|
|
- ensureDirectoryExists(outputPath);
|
|
|
- try {
|
|
|
- ProcessBuilder processBuilder = new ProcessBuilder();
|
|
|
- if (osName.contains("windows")) {
|
|
|
- processBuilder.command(
|
|
|
- "cmd.exe", "/c",
|
|
|
- "soffice", "--headless", "--convert-to", "pdf",
|
|
|
- filePath, "--outdir", tempDir);
|
|
|
- } else {
|
|
|
- processBuilder.command(
|
|
|
- "soffice", "--headless", "--convert-to", "pdf:writer_pdf_Export",
|
|
|
- filePath, "--outdir", tempDir);
|
|
|
- }
|
|
|
+ // ensureDirectoryExists(outputPath);
|
|
|
+ // try {
|
|
|
+// ProcessBuilder processBuilder = new ProcessBuilder();
|
|
|
+// if (osName.contains("windows")) {
|
|
|
+// processBuilder.command(
|
|
|
+// "cmd.exe", "/c",
|
|
|
+// "soffice", "--headless", "--convert-to", "pdf",
|
|
|
+// filePath, "--outdir", tempDir);
|
|
|
+// } else {
|
|
|
+// processBuilder.command(
|
|
|
+// "soffice", "--headless", "--convert-to", "pdf:writer_pdf_Export",
|
|
|
+// filePath, "--outdir", tempDir);
|
|
|
+// }
|
|
|
|
|
|
- Process process = processBuilder.start();
|
|
|
- int exitCode = process.waitFor();
|
|
|
- if (exitCode != 0) {
|
|
|
- throw new RuntimeException("转换失败,退出码:" + exitCode);
|
|
|
- }
|
|
|
+ FileToPdfUtils.officeToPdf(filePath, tempDir);
|
|
|
|
|
|
|
|
|
- // 查找生成的PDF文件
|
|
|
- File outputDir = new File(tempDir);
|
|
|
- File[] files = outputDir.listFiles((dir, name) ->
|
|
|
- name.toLowerCase().endsWith(".pdf") && !name.equals(filePath));
|
|
|
|
|
|
- if (files != null && files.length > 0) {
|
|
|
- File pdfFile = files[0];
|
|
|
- Files.move(pdfFile.toPath(), new File(outputPath).toPath());
|
|
|
- return outputPath;
|
|
|
- }
|
|
|
- throw new FileNotFoundException("未找到生成的PDF文件");
|
|
|
+// Process process = processBuilder.start();
|
|
|
+// int exitCode = process.waitFor();
|
|
|
+// if (exitCode != 0) {
|
|
|
+// throw new RuntimeException("转换失败,退出码:" + exitCode);
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// // 查找生成的PDF文件
|
|
|
+// File outputDir = new File(tempDir);
|
|
|
+// File[] files = outputDir.listFiles((dir, name) ->
|
|
|
+// name.toLowerCase().endsWith(".pdf") && !name.equals(filePath));
|
|
|
+//
|
|
|
+// if (files != null && files.length > 0) {
|
|
|
+// File pdfFile = files[0];
|
|
|
+// Files.move(pdfFile.toPath(), new File(outputPath).toPath());
|
|
|
+// return outputPath;
|
|
|
+// }
|
|
|
+// throw new FileNotFoundException("未找到生成的PDF文件");
|
|
|
|
|
|
- } catch (IOException | InterruptedException e) {
|
|
|
- throw new RuntimeException("转换过程中发生错误", e);
|
|
|
- }
|
|
|
+// } catch (IOException | InterruptedException e) {
|
|
|
+// throw new RuntimeException("转换过程中发生错误", e);
|
|
|
+// }
|
|
|
+
|
|
|
+ return outputPath;
|
|
|
}
|
|
|
|
|
|
private void uploadToMinio(String filePath, String fileId) throws Exception {
|
|
|
@@ -158,16 +163,20 @@ public class PdfUtils {
|
|
|
.build());
|
|
|
}
|
|
|
|
|
|
- private void cleanTempDir() {
|
|
|
- File[] files = new File(tempDir).listFiles();
|
|
|
- if (files != null) {
|
|
|
- for (File file : files) {
|
|
|
- if (file.getName().startsWith("converted_") ||
|
|
|
- file.getName().startsWith("temp_")) {
|
|
|
- file.delete();
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ private void cleanTempDir(String filePath,String outputPath) {
|
|
|
+// File[] files = new File(tempDir).listFiles();
|
|
|
+// if (files != null) {
|
|
|
+// for (File file : files) {
|
|
|
+// if (file.getName().startsWith("converted_") ||
|
|
|
+// file.getName().startsWith("temp_")|| file.getName().startsWith("Temp")) {
|
|
|
+// file.delete();
|
|
|
+// }
|
|
|
+// }
|
|
|
+// }
|
|
|
+ File file = new File(filePath);
|
|
|
+ file.delete();
|
|
|
+ File pdffile = new File(outputPath);
|
|
|
+ pdffile.delete();
|
|
|
}
|
|
|
|
|
|
|