我尝试通过以下代码将一个简单的 docx 文件转换为 PDF:
try {
InputStream in = testXdocReportDoc.class.getResourceAsStream("testXdocReportDocV2.docx");
IXDocReport report = XDocReportRegistry.getRegistry().loadReport(in, TemplateEngineKind.Velocity);
IContext context = report.createContext();
Project project = new Project("test1", "test2", "test3", "test4", "test5");
context.put("project", project);
File tempFile = new File(f.getAbsolutePath().replace("pdf", "docx"));
OutputStream out = new FileOutputStream(tempFile);
report.process(context, out);
out.flush();
out.close();
in.close();
IConverter converter2 = RemoteConverter.builder()
.baseFolder(new File(HistorisationDevisManager.getAbsolutePathDevisPdf(devis)))
.workerPool(20, 25, 2, TimeUnit.SECONDS)
.requestTimeout(10, TimeUnit.SECONDS)
.baseUri("http://localhost:8080")
.build();
Future<Boolean> conversion = converter2
.convert(tempFile).as(DocumentType.DOCX)
.to(bo).as(DocumentType.PDF)
.prioritizeWith(1000) // optional
.schedule();
conversion.get();
try {
OutputStream outputStream = new FileOutputStream(f);
bo.writeTo(outputStream);
} catch (IOException e) {
e.printStackTrace();
}
bo.close();
} catch (Exception e) {
e.printStackTrace();
}
}
一切似乎都正常,我的 docx 是一个简单的文件,里面有“测试”,但是当我试图打开生成的 PDF 时,它被破坏了一些,我不知道为什么......