Apache FOP the method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()

时光总嘲笑我的痴心妄想 提交于 2019-12-12 11:35:26

问题


I am getting the following error:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:     The method newInstance(FopFactoryConfig) in the type FopFactory is not applicable for the arguments ()

    at fopdemo.fopvass.PDFHandler.createPDFFile(PDFHandler.java:42)
    at fopdemo.fopvass.TestPDF.main(TestPDF.java:40)

This is my code:

package fopdemo.fopvass;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.sax.SAXResult;
import javax.xml.transform.stream.StreamSource;

import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.apps.Fop;
import org.apache.fop.apps.FopFactory;
import org.apache.fop.apps.MimeConstants;

public class PDFHandler {
    public static final String EXTENSION = ".pdf";
    public String PRESCRIPTION_URL = "template.xsl";

    public String createPDFFile(ByteArrayOutputStream xmlSource, String templateFilePath) throws IOException {
        File file = File.createTempFile("" + System.currentTimeMillis(), EXTENSION);
        URL url = new File(templateFilePath + PRESCRIPTION_URL).toURI().toURL();
        // creation of transform source
        StreamSource transformSource = new StreamSource(url.openStream());
        // 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 pdfoutStream = new ByteArrayOutputStream();
        StreamSource source = new StreamSource(new ByteArrayInputStream(xmlSource.toByteArray()));
        Transformer xslfoTransformer;
        try {
            TransformerFactory transfact = TransformerFactory.newInstance();

            xslfoTransformer = transfact.newTransformer(transformSource);
            // Construct fop with desired output format
            Fop fop;
            try {
                fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, pdfoutStream);
                // 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 save PDF file use the following code
                    OutputStream out = new java.io.FileOutputStream(file);
                    out = new java.io.BufferedOutputStream(out);
                    FileOutputStream str = new FileOutputStream(file);
                    str.write(pdfoutStream.toByteArray());
                    str.close();
                    out.close();

                } catch (TransformerException e) {
                    e.printStackTrace();
                }
            } catch (FOPException e) {
                e.printStackTrace();
            }
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerFactoryConfigurationError e) {
            e.printStackTrace();
        }
        return file.getPath();
    }

    public ByteArrayOutputStream getXMLSource(EmployeeData data) throws Exception {
        JAXBContext context;

        ByteArrayOutputStream outStream = new ByteArrayOutputStream();

        try {
            context = JAXBContext.newInstance(EmployeeData.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(data, System.out);
            m.marshal(data, outStream);
        } catch (JAXBException e) {

            e.printStackTrace();
        }
        return outStream;

    }
}

This is where it is called:

package fopdemo.fopvass;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
/**
 * @author Debasmita.Sahoo
 *
 */
public class TestPDF {
    public static void main(String args[]){
        System.out.println("Hi Testing");
        ArrayList employeeList = new ArrayList();
        String templateFilePath ="C:/Paula/Proyectos/fop/fopvass/resources/";

        Employee e1= new Employee();
        e1.setName("Debasmita1 Sahoo");
        e1.setEmployeeId("10001");
        e1.setAddress("Pune");
        employeeList.add(e1);

        Employee e2= new Employee();
        e2.setName("Debasmita2 Sahoo");
        e2.setEmployeeId("10002");
        e2.setAddress("Test");
        employeeList.add(e2);

        Employee e3= new Employee();
        e3.setName("Debasmita3 Sahoo");
        e3.setEmployeeId("10003");
        e3.setAddress("Mumbai");
        employeeList.add(e3);

        EmployeeData data = new EmployeeData();
        data.setEemployeeList(employeeList);
        PDFHandler handler = new PDFHandler();

        try {
            ByteArrayOutputStream streamSource = handler.getXMLSource(data);

            handler.createPDFFile(streamSource,templateFilePath);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

回答1:


This is your problem:

FopFactory fopFactory = FopFactory.newInstance();

If you look at the code for that method, newInstance(), it takes a parameter. You need to supply that parameter.

From the documentation (provided by Mathias Muller), under 'Basic Usage':

FopFactory fopFactory = FopFactory.newInstance(new File("C:/Temp/fop.xconf"));

You need to provide it a File object.



来源:https://stackoverflow.com/questions/34821013/apache-fop-the-method-newinstancefopfactoryconfig-in-the-type-fopfactory-is-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!