首页 > 解决方案 > 作为应用程序要求,我试图从 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 的任何推荐方式请提出建议。谢谢

标签: javapdf-generationflying-saucer

解决方案


推荐阅读