Java 作为应用程序要求,我试图从ftl(模板与html相同)获取pdf,但由于异常而失败

Java 作为应用程序要求,我试图从ftl(模板与html相同)获取pdf,但由于异常而失败,java,pdf-generation,flying-saucer,Java,Pdf Generation,Flying Saucer,下面是我运行项目时的错误 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>(ITextR

下面是我运行项目时的错误

 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)

另外,如果在spring boot中有任何建议使用ftl模板创建pdf的方法,请提出建议。谢谢

这看起来像是一个兼容性问题,即调用了一个无参数构造函数,但类路径上的库版本没有提供该构造函数,因此找不到该构造函数(如果我解释正确的话,这是关于
SharedContext()
构造函数的问题)。@Thomas谢谢。问题在于JAR的版本。但是仍然无法将数据写入模板。没有错误,但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");
        }
    }