Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

java - The method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()

I have a requirement to convert XML to PDF document for that am using XSL - Apache FOP (Java). Am getting the below error The method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments () for the below line

// create an instance of fop factory
    FopFactory fopFactory = FopFactory.newInstance();

Please find my full java code:

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;
}

Please find the below dependencies am using :

<!-- 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Check the API documentation for your version of Apache FOP, I think you need to provide a base URI or a configuration file e.g.

FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());

to set up the current working directory's URI as the base URI.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...