【问题标题】:Apache FOP - The method getDefaultHandler() is undefined for the type FopApache FOP - 方法 getDefaultHandler() 未为 Fop 类型定义
【发布时间】:2018-05-05 14:45:21
【问题描述】:

我需要使用 XSL - Apache FOP (Java) 将 XML 转换为 PDF 文档。我收到以下错误The method getDefaultHandler() is undefined for the type Fop 以下行

 Result res = new SAXResult(fop.getDefaultHandler());

请找到我完整的 JAVA 代码。

public static void main (String args[])
    {
        // the XSL FO file
        File xsltfile = new File("sample2.xsl");
        // the XML file from which we take the name
        StreamSource source = new StreamSource(new File("sample2.xml"));
        // creation of transform source
        StreamSource transformSource = new StreamSource(xsltfile);
        // create an instance of fop factory
        FopFactory fopFactory = FopFactory.newInstance();
        // a user agent is needed for transformation
        FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
        // to store output
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        Transformer xslfoTransformer;
        try
        {
            xslfoTransformer = getTransformer(transformSource);
            // Construct fop with desired output format
                Fop fop;
            try
            {
                fop = fopFactory.newFop
                    (MimeConstants.MIME_PDF, foUserAgent, outStream);
                // Resulting SAX events (the generated FO) 
                // must be piped through to FOP
                        Result res = new SAXResult(fop.getDefaultHandler());

                // Start XSLT transformation and FOP processing
                try
                {
                        // everything will happen here..
                    xslfoTransformer.transform(source, res);
                    // if you want to get the PDF bytes, use the following code
                    //return outStream.toByteArray();

                    // if you want to save PDF file use the following code
                    File pdffile = new File("Result.pdf");
                    OutputStream out = new java.io.FileOutputStream(pdffile);
                                out = new java.io.BufferedOutputStream(out);
                                FileOutputStream str = new FileOutputStream(pdffile);
                                str.write(outStream.toByteArray());
                                str.close();
                                out.close();

                }
                catch (TransformerException e) {
                    throw e;
                }
            }
            catch (FOPException e) {
                throw e;
            }
        }
        catch (TransformerConfigurationException e)
        {
            throw e;
        }
        catch (TransformerFactoryConfigurationError e)
        {
            throw e;
        }
    }

    private static Transformer getTransformer(StreamSource streamSource)
    {
        // setup the xslt transformer
        net.sf.saxon.TransformerFactoryImpl impl = 
                new net.sf.saxon.TransformerFactoryImpl();

        try {
            return impl.newTransformer(streamSource);

        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        }
        return null;
    }

请找到我的依赖项:

<!-- https://mvnrepository.com/artifact/fop/fop -->
<dependency>
    <groupId>fop</groupId>
    <artifactId>fop</artifactId>
    <version>0.20.5</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.apache.xmlgraphics/fop -->
<dependency>
    <groupId>org.apache.xmlgraphics</groupId>
    <artifactId>fop</artifactId>
    <version>2.2</version>
</dependency>

<!-- https://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE -->
<dependency>
    <groupId>net.sf.saxon</groupId>
    <artifactId>Saxon-HE</artifactId>
    <version>9.8.0-11</version>
</dependency>

【问题讨论】:

  • 您使用的是什么版本的 FOP?请包括您的依赖配置。
  • @RobinGreen - 我还添加了我的依赖项详细信息..

标签: java xml compiler-errors xsl-fo apache-fop


【解决方案1】:

您的依赖项中有an old 和新版本的 fop。因此,您的 IDE 或 Java 编译器必须选择没有该方法的旧版本。从依赖项中删除旧版本。

【讨论】:

  • 非常感谢,它对我有用.. 但现在与创建 newInstance 的 `FopFactory fopFactory = FopFactory.newInstance();` 行遇到了不同的问题。我对 ApacheFOP 非常陌生,对这些问题不太确定,也没有从 Google 获得太多帮助。
  • 我已经为同一个https://stackoverflow.com/questions/50197362/the-method-newinstancefopfactoryconfig-in-the-type-fopfactory-is-not-applicabl发布了单独的问题
猜你喜欢
  • 2014-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 2012-04-22
  • 2020-11-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多