From 4ce181bd784aeb873a4751826c227161d35de2b5 Mon Sep 17 00:00:00 2001 From: wzp <2040239371@qq.com> Date: 星期二, 17 六月 2025 11:29:16 +0800 Subject: [PATCH] fix:修复报告文件名里有tab键导致的bug --- src/main/java/com/ots/project/tool/PdfUtil.java | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 files changed, 104 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/ots/project/tool/PdfUtil.java b/src/main/java/com/ots/project/tool/PdfUtil.java index 5bd4622..39db254 100644 --- a/src/main/java/com/ots/project/tool/PdfUtil.java +++ b/src/main/java/com/ots/project/tool/PdfUtil.java @@ -3,9 +3,9 @@ import com.aspose.words.*; import lombok.extern.slf4j.Slf4j; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; +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,39 +16,129 @@ /** * 璁剧疆鐢ㄦ埛瀛椾綋浣嶇疆 */ - public static void setFont(){ - String[] fonts = {"C:\\Users\\澶уご\\AppData\\Local\\Microsoft\\Windows\\Fonts\\","C:\\Users\\datou\\AppData\\Local\\Microsoft\\Windows\\Fonts\\","/usr/share/fonts"}; + public static void setFont() { + 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); + FolderFontSource folderFontSource = new FolderFontSource(fonts[i], true); fontSources.add(folderFontSource); } FontSourceBase[] fontSourceBases = fontSources.toArray(new FontSourceBase[fontSources.size()]); FontSettings.getDefaultInstance().setFontsSources(fontSourceBases); } - public static void convertPDF(String input){ +// 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 */ } + } + } + } + + /** + * docker word杞琾df + * @param profile + * @param docx2pdfPath + * @param fileName + */ + public static void dockerConvertPDF(String profile,String docx2pdfPath,String fileName){ + try { + 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("docker鎵ц鍛戒护:{}", command); + log.info("docker鎵ц杩斿洖"); + while ((line = in.readLine()) != null) { + log.info(line); + } + in.close(); + proc.waitFor(); } catch (Exception e) { e.printStackTrace(); } } + /** + * aspose-words:jdk17:23.4 鐗堟湰 + */ + public static void registerWord() throws Exception { + // 鏋勯�犱竴涓敞鍐屼俊鎭� + Class<?> zzXgCClass = Class.forName("com.aspose.words.zzXgC"); + Constructor<?> constructors = zzXgCClass.getDeclaredConstructors()[0]; + constructors.setAccessible(true); + Object instance = constructors.newInstance("zzW5k", "zzYON"); + // zzXFN = 1 + java.lang.reflect.Field zzXFN = zzXgCClass.getDeclaredField("zzXFN"); + zzXFN.setAccessible(true); + zzXFN.set(instance, 1); + + // 鎶婃敞鍐屼俊鎭斁鍒� zzYVA杩欎釜绫讳腑鏉� + Class<?> zzYVAClass = Class.forName("com.aspose.words.zzYVA"); + java.lang.reflect.Field zzwP = zzYVAClass.getDeclaredField("zzwP"); + zzwP.setAccessible(true); + ArrayList<Object> zzwPValue = new ArrayList<>(); + zzwPValue.add(instance); + zzwP.set(null, zzwPValue); + + // 鐢熸垚鏂囨。浼氭帀杩欎釜鏉ュ垽鏂� zzXQo + Class<?> zzXQoClass = Class.forName("com.aspose.words.zzXQo"); + java.lang.reflect.Field zzHA = zzXQoClass.getDeclaredField("zzHA"); + zzHA.setAccessible(true); + zzHA.set(null, 128); + java.lang.reflect.Field zzWod = zzXQoClass.getDeclaredField("zzWod"); + zzWod.setAccessible(true); + zzWod.set(null, false); + } + public static void main(String[] args) { //convertPDF("D:\\娴嬭瘎绯荤粺\\home\\鏋梍PAQ_GS_TH.docx"); - convertPDF("C:\\Users\\澶уご\\Desktop\\MAQ缁勫埆鎶ュ憡\\娴嬭瘯_MAQTR_IA_EN.docx"); + convertPDF("C:\\Users\\mac\\Desktop\\API_Fan_IA_CN.docx"); } } -- Gitblit v1.9.1