java - 作为应用程序要求,我试图从 ftl(模板与 html 相同)获取 pdf,但由于异常而失败
问题描述
以下是我运行项目时的错误
java.lang.NoSuchMethodError: org.xhtmlrenderer.layout.SharedContext: method <init>()V not found at org.xhtmlrenderer.pdf.ITextRenderer.<init>(ITextRenderer.java:107) at org.xhtmlrenderer.pdf.ITextRenderer.<init>(ITextRenderer.java:98)
我正在尝试独立运行该应用程序。我已将所有必要的 jar 添加到类路径中。
但不知道为什么我会收到这个错误。请建议
请在此处找到项目 https://github.com/anju051989/service-client.git
以下是将模板转换为 pdf 的代码片段
private byte[] convertFtlTOPdf(ByteArrayOutputStream fbos) throws PdfSystemException {
//log.info("[pdfUtils.MPA base] start generating PDF");
if(null !=fbos){
//log.debug("File size is :"+ fbos.size()/1024/1024);
}
ByteArrayOutputStream pbos = new ByteArrayOutputStream();
try {
DocumentBuilderFactoryImpl factory = new DocumentBuilderFactoryImpl();
// DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setIgnoringComments(true);
factory.setValidating(false);
factory.setNamespaceAware(true);
factory.setFeature("http://xml.org/sax/features/namespaces", false);
factory.setFeature("http://xml.org/sax/features/validation", false);
factory.setFeature("http://apache.org/xml/features/dom/defer-node-expansion", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
long start1 = System.currentTimeMillis();
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId)
throws SAXException, IOException {
if (systemId.contains("foo.dtd")) {
return new InputSource(new StringReader(""));
} else {
return null;
}
}
});
Document doc = builder.parse(new ByteArrayInputStream(fbos.toByteArray()));
//log.debug("end DocumentBuilder " + "Total time: " + (System.currentTimeMillis() - start1)/1000 + "s");
long start = System.currentTimeMillis();
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(doc, null);
renderer.layout();
renderer.createPDF(pbos);
//log.debug("end created pdf " + "Total time: " + (System.currentTimeMillis() - start)/1000 + "s");
return pbos.toByteArray();
} catch (Exception e) {
throw new PdfSystemException(e);
} finally {
try {
pbos.close();
} catch (IOException e) {
throw new PdfSystemException(e);
}
//log.info("[pdfUtils.MPA base] end of create PDF");
}
}
此外,如果在 Spring Boot 中使用 ftl 模板创建 pdf 的任何推荐方式请提出建议。谢谢
解决方案
推荐阅读
- jquery - 使用 jquery MVC 对多个模型的数据表进行 CRUD 操作
- swift - swift:检查数组中的值是否存在条件
- rust - 将字节打包到 u32 与将它们存储在 vec 之间的性能差异
? - amazon-web-services - 从 AWS Athena 获取的结果在时间戳列中具有不同的值
- c - qemu中`ObjectClass`和`Object`的区别
- python - 在熊猫数据框中,如何根据列值过滤行,进行计算并将结果分配给新列?
- javascript - 如果我有 3 个嵌套数据,如何将 JSON 数据添加到我的数据表中?
- android - 带有 ViewPager 的 Android TabLayout 在旋转时重复片段内容
- algorithm - 与肯汤普森的六度分离
- node.js - 无法在 heroku 上正确部署节点 js 应用程序