[测评系统]--测评系统核心代码库
wzp
2025-08-01 95eb8a2a75cbeebcb3393d404c1952bd59b8989a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package com.ots.project.tool;
 
import com.aspose.words.*;
import lombok.extern.slf4j.Slf4j;
 
import java.io.*;
import java.lang.reflect.Constructor;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
 
@Slf4j
public class PdfUtil {
 
    /**
     * 设置用户字体位置
     */
    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);
            fontSources.add(folderFontSource);
        }
        FontSourceBase[] fontSourceBases = fontSources.toArray(new FontSourceBase[fontSources.size()]);
        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("【转换】doc转pdf耗时:{}秒", ((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();
            String output = input.replaceAll(".docx", ".pdf");
            long old = System.currentTimeMillis();
 
            fileInput = new FileInputStream(input);
            pres = new Document(fileInput);
 
            out = new FileOutputStream(output);
            pres.save(out, SaveFormat.PDF);
 
            long now = System.currentTimeMillis();
            log.info("【转换】doc转pdf耗时:{}秒", ((now - old) / 1000.0));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 确保资源释放
            if (pres != null) pres = null; // 关键:释放Document原生资源
            if (fileInput != null) {
                try { fileInput.close(); } catch (IOException e) { /* ignore */ }
            }
            if (out != null) {
                try { out.close(); } catch (IOException e) { /* ignore */ }
            }
        }
    }
 
    /**
     * docker word转pdf
     * @param profile
     * @param docx2pdfPath
     * @param fileName
     * 这个有时候docker转化会卡死,2025.7.15拟采用pdfDockerUtil新方法处理
     */
    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\\mac\\Desktop\\API_Fan_IA_CN.docx");
    }
 
}