Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java FopFactory类型中的方法newInstance(FopFactoryConfig)不适用于参数()_Java_Xml_Xslt_Compiler Errors_Apache Fop - Fatal编程技术网

Java FopFactory类型中的方法newInstance(FopFactoryConfig)不适用于参数()

Java FopFactory类型中的方法newInstance(FopFactoryConfig)不适用于参数(),java,xml,xslt,compiler-errors,apache-fop,Java,Xml,Xslt,Compiler Errors,Apache Fop,我需要使用XSL-ApacheFOP(Java)将XML转换为PDF文档。我收到下面的错误类型FopFactory中的方法newInstance(FopFactoryConfig)不适用于下面一行的参数() // create an instance of fop factory FopFactory fopFactory = FopFactory.newInstance(); 请查找我的完整java代码: public static void main (String args[])

我需要使用XSL-ApacheFOP(Java)将XML转换为PDF文档。我收到下面的错误类型FopFactory中的方法newInstance(FopFactoryConfig)不适用于下面一行的参数()

// create an instance of fop factory
    FopFactory fopFactory = FopFactory.newInstance();
请查找我的完整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();  // Error throwing in this line..
    // 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/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>

org.apache.xmlgraphics
fop
2.2
net.sf.saxon
萨克森河
9.8.0-11

查看您的ApacheFop版本的API文档,我认为您需要提供一个基本URI或配置文件,例如

FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
将当前工作目录的URI设置为基本URI