Apache FOP FopFactory类型中的newInstance(FopFactoryConfig)方法不适用于arguments()

时间:2016-01-15 22:34:56

标签: apache apache-fop

我收到以下错误:

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)

这是我的代码:

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;

    }
}

这就是所谓的地方:

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

2 个答案:

答案 0 :(得分:8)

这是你的问题:

FopFactory fopFactory = FopFactory.newInstance();

如果查看该方法的代码newInstance(),则需要一个参数。您需要提供该参数。

来自documentation(由Mathias Muller提供),在“基本用法”下:

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

您需要提供一个File对象。

答案 1 :(得分:0)

FopFactory.newInstance()支持

FopFactory.newInstance(File)
FopFactory.newInstance(Uri)
FopFactory.newInstance(Uri, InputStream)
FopFactory.newInstance(FopFactoryConfig)

我在Windows和Mac OS(即Unix \ Linux之类的NFS)上成功实现了以下代码

URI uri = new File(config.getRoot()).toPath().toUri();
FopFactory fopFactory = FopFactory.newInstance(uri);

另一方面,这不起作用(我不明白为什么)

URI uri = new File(config.getRoot()).toUri();