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/upload/reports/"+uuid+".docx"; // 包含图表的 Word 文件
|
String pdfFilePath = "/data/upload/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);
|
}
|
|
}
|