From b6a1cee9c6fbfe0a1f8f7c410c637248e29239e5 Mon Sep 17 00:00:00 2001
From: wzp <2040239371@qq.com>
Date: 星期五, 18 七月 2025 10:22:17 +0800
Subject: [PATCH] fix:修复docker不能正常清理的问题。设置60s超时时间

---
 src/main/java/com/ots/project/tool/PdfUtil.java |   81 ++++++++++++++++++++++++++--------------
 1 files changed, 52 insertions(+), 29 deletions(-)

diff --git a/src/main/java/com/ots/project/tool/PdfUtil.java b/src/main/java/com/ots/project/tool/PdfUtil.java
index e7d60a2..20f9b63 100644
--- a/src/main/java/com/ots/project/tool/PdfUtil.java
+++ b/src/main/java/com/ots/project/tool/PdfUtil.java
@@ -5,6 +5,7 @@
 
 import java.io.*;
 import java.lang.reflect.Constructor;
+import java.text.MessageFormat;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -16,7 +17,7 @@
      * 璁剧疆鐢ㄦ埛瀛椾綋浣嶇疆
      */
     public static void setFont() {
-        String[] fonts = {"C:\\Users\\澶уご\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "C:\\Users\\datou\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "/usr/share/fonts"};
+        String[] fonts = {"C:\\Windows\\Fonts", "C:\\Users\\datou\\AppData\\Local\\Microsoft\\Windows\\Fonts\\", "/usr/share/fonts"};
         List<FontSourceBase> fontSources = new ArrayList(Arrays.asList(FontSettings.getDefaultInstance().getFontsSources()));
         for (int i = 0; i < fonts.length; i++) {
             FolderFontSource folderFontSource = new FolderFontSource(fonts[i], true);
@@ -26,52 +27,74 @@
         FontSettings.getDefaultInstance().setFontsSources(fontSourceBases);
     }
 
+//    public static void convertPDF(String input) {
+//        try {
+//            registerWord();
+//            setFont();
+//            Document pres = null;
+//            //鎵嬪姩鏇挎崲杈撳嚭pdf鍚嶇О
+//            String output = input.replaceAll(".docx", ".pdf");
+//            long old = System.currentTimeMillis();
+//            FileInputStream fileInput = new FileInputStream(input);
+//            pres = new Document(fileInput);
+//            FileOutputStream out = new FileOutputStream(new File(output));
+//            pres.save(out, SaveFormat.PDF);
+//            out.close();
+//            long now = System.currentTimeMillis();
+//            log.info("銆愯浆鎹€�慸oc杞琾df鑰楁椂锛歿}绉�", ((now - old) / 1000.0));
+//        } catch (Exception e) {
+//            e.printStackTrace();
+//        }
+//    }
+
     public static void convertPDF(String input) {
+        FileInputStream fileInput = null;
+        FileOutputStream out = null;
+        Document pres = null;
         try {
             registerWord();
             setFont();
-            Document pres = null;
-            //鎵嬪姩鏇挎崲杈撳嚭pdf鍚嶇О
             String output = input.replaceAll(".docx", ".pdf");
             long old = System.currentTimeMillis();
-            FileInputStream fileInput = new FileInputStream(input);
+
+            fileInput = new FileInputStream(input);
             pres = new Document(fileInput);
-            FileOutputStream out = new FileOutputStream(new File(output));
+
+            out = new FileOutputStream(output);
             pres.save(out, SaveFormat.PDF);
-            out.close();
+
             long now = System.currentTimeMillis();
-            log.info("doc杞琾df鑰楁椂锛歿}绉�", ((now - old) / 1000.0));
+            log.info("銆愯浆鎹€�慸oc杞琾df鑰楁椂锛歿}绉�", ((now - old) / 1000.0));
         } catch (Exception e) {
             e.printStackTrace();
+        } finally {
+            // 纭繚璧勬簮閲婃斁
+            if (pres != null) pres = null; // 鍏抽敭锛氶噴鏀綝ocument鍘熺敓璧勬簮
+            if (fileInput != null) {
+                try { fileInput.close(); } catch (IOException e) { /* ignore */ }
+            }
+            if (out != null) {
+                try { out.close(); } catch (IOException e) { /* ignore */ }
+            }
         }
     }
 
     /**
-     * 鎵цpython鑴氭湰 docx杞琾df
-     * @param filePath
+     * docker word杞琾df
+     * @param profile
+     * @param docx2pdfPath
+     * @param fileName
+     * 杩欎釜鏈夋椂鍊檇ocker杞寲浼氬崱姝伙紝2025.7.15鎷熼噰鐢╬dfDockerUtil鏂版柟娉曞鐞�
      */
-    public static void convertPythonPDF(String pythonScript,String filePath){
+    public static void dockerConvertPDF(String profile,String docx2pdfPath,String fileName){
         try {
-            File file = new File(filePath);
-            if(!file.exists()){
-                file.mkdirs();
-            }
-            //鎵嬪姩鏇挎崲杈撳嚭pdf鍚嶇О
-            String output = filePath.replaceAll(".docx", ".pdf");
-
-            String os = System.getProperty("os.name");
-            String[] args1;
-            if (os.contains("Windows")){
-                args1 = new String[] { "python", pythonScript, filePath, output };
-            } else {
-                args1 = new String[] { "python3", pythonScript, filePath, output };
-            }
-            // 鎵цpy鏂囦欢
-            Process proc = Runtime.getRuntime().exec(args1);
+            String command = MessageFormat.format(docx2pdfPath,profile,fileName);
+            // 鎵цdocker 鍛戒护
+            Process proc = Runtime.getRuntime().exec(command);
             BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream(), "GBK"));
             String line = null;
-            log.info("python鎵ц鍛戒护:{}", Arrays.toString(args1));
-            log.info("python鎵ц杩斿洖");
+            log.info("docker鎵ц鍛戒护:{}", command);
+            log.info("docker鎵ц杩斿洖");
             while ((line = in.readLine()) != null) {
                 log.info(line);
             }
@@ -116,7 +139,7 @@
 
     public static void main(String[] args) {
         //convertPDF("D:\\娴嬭瘎绯荤粺\\home\\鏋梍PAQ_GS_TH.docx");
-        convertPDF("C:\\Users\\澶уご\\Desktop\\鏋�3_CIAQ_IA_CN.docx");
+        convertPDF("C:\\Users\\mac\\Desktop\\API_Fan_IA_CN.docx");
     }
 
 }

--
Gitblit v1.9.1