liujie
2026-04-09 3adfcc5a9829a0231bf95c1a7e3aaad1d28b05d5
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
146
147
148
149
150
151
152
153
154
155
156
157
158
package com.estate.expert.admin.utils.wordtopdf;
 
import org.springframework.stereotype.Component;
 
import java.io.File;
import java.util.ArrayList;
import java.util.List;
 
/**
 * Word 转 PDF 测试类
 */
@Component
public class WordToPdfTest {
    
    public static void main(String[] args) {
        System.out.println("=== Word 转 PDF 测试开始 ===");
        
        // 获取转换器实例
        WordToPdfConverter converter = WordToPdfConverter.getInstance();
        
        try {
            // 测试1:单文件转换
            String s = testSingleFileConversion("1769582460614");
        } catch (Exception e) {
            System.err.println("测试过程中发生错误: " + e.getMessage());
            e.printStackTrace();
        } finally {
            // 关闭转换器
            converter.shutdown();
        }
        
        System.out.println("=== Word 转 PDF 测试结束 ===");
    }
 
    /**
     * 测试单文件转换
     */
    public static String testSingleFileConversion(String uuid) {
        System.out.println("\n--- 测试1:单文件转换 ---");
        WordToPdfConverter converter = WordToPdfConverter.getInstance();
        // 请修改为您的 Word 文件路径
        String wordFilePath = "/data/uploads/reports/"+uuid+".docx"; // 包含图表的 Word 文件
        String pdfFilePath = "/data/uploads/reports/"+uuid+".pdf";
        
        File wordFile = new File(wordFilePath);
        if (!wordFile.exists()) {
            System.out.println("测试文件不存在,跳过单文件测试: " + wordFilePath);
            System.out.println("请创建测试文件或修改路径");
            return  "";
        }
        
        System.out.println("输入文件: " + wordFile.getAbsolutePath());
        System.out.println("输出文件: " + pdfFilePath);
        
        // 执行转换
        boolean success = converter.convert(wordFile, new File(pdfFilePath));
        
        if (success) {
            System.out.println("✓ 单文件转换成功");
            System.out.println("PDF 文件已生成: " + pdfFilePath);
            
            // 检查文件大小
            File pdfFile = new File(pdfFilePath);
            if (pdfFile.exists()) {
                long sizeKB = pdfFile.length() / 1024;
                System.out.println("PDF 文件大小: " + sizeKB + " KB");
            }
        } else {
            System.out.println("✗ 单文件转换失败");
        }
        return "http://xllpgzx.com/report/"+uuid+".pdf";
    }
    
    /**
     * 测试批量转换
     */
    private static void testBatchConversion(WordToPdfConverter converter) {
        System.out.println("\n--- 测试2:批量转换 ---");
        
        // 准备测试文件列表
        List<File> wordFiles = new ArrayList<>();
        File testDir = new File("F:\\test\\documents");
        
        if (testDir.exists() && testDir.isDirectory()) {
            File[] files = testDir.listFiles((dir, name) -> 
                name.toLowerCase().endsWith(".doc") || 
                name.toLowerCase().endsWith(".docx")
            );
            
            if (files != null && files.length > 0) {
                for (File file : files) {
                    wordFiles.add(file);
                }
            }
        }
        
        if (wordFiles.isEmpty()) {
            System.out.println("批量测试目录中没有 Word 文件,跳过批量测试");
            System.out.println("请将测试文件放入: F:\\test\\documents\\");
            return;
        }
        
        System.out.println("找到 " + wordFiles.size() + " 个 Word 文件");
        
        // 输出目录
        File outputDir = new File("F:\\test\\batch_output");
        
        // 执行批量转换
        int successCount = converter.batchConvert(wordFiles, outputDir);
        
        System.out.println("批量转换结果: 成功 " + successCount + "/" + wordFiles.size());
        System.out.println("输出目录: " + outputDir.getAbsolutePath());
    }
    
    /**
     * 测试错误处理
     */
    private static void testErrorHandling(WordToPdfConverter converter) {
        System.out.println("\n--- 测试3:错误处理 ---");
        
        // 测试1:不存在的文件
        System.out.println("测试1:不存在的文件");
        try {
            boolean result = converter.convert(
                new File("F:\\test\\nonexistent.docx"),
                new File("F:\\test\\output\\nonexistent.pdf")
            );
            System.out.println("结果: " + (result ? "成功" : "失败"));
        } catch (Exception e) {
            System.out.println("预期错误: " + e.getMessage());
        }
        
        // 测试2:不支持的文件格式
        System.out.println("\n测试2:不支持的文件格式");
        try {
            File tempFile = File.createTempFile("test", ".txt");
            boolean result = converter.convert(
                tempFile,
                new File("F:\\test\\output\\test.pdf")
            );
            System.out.println("结果: " + (result ? "成功" : "失败"));
            tempFile.delete();
        } catch (Exception e) {
            System.out.println("预期错误: " + e.getMessage());
        }
        
        // 测试3:服务状态检查
        System.out.println("\n测试3:服务状态检查");
        boolean isRunning = converter.isServiceRunning();
        System.out.println("转换服务状态: " + (isRunning ? "运行中" : "已停止"));
        
        // 测试4:转换统计
        System.out.println("\n测试4:转换统计");
        int count = converter.getConversionCount();
        System.out.println("总转换次数: " + count);
    }
 
}