错误:Java中XML格式的PDF

时间:2018-08-20 17:03:15

标签: java apache-fop

我刚刚找到了一个可以帮助将XML文件转换为PDF文件的代码,但是当我编写代码时却得到了错误:

进口:

软件包PdfXml;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
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 static void convertToPDF() throws IOException, FOPException, TransformerException {
                // the XSL FO file
                File xsltFile = new File(RESOURCES_DIR + "//template.xsl");
                // the XML file which provides the input
                StreamSource xmlSource = new StreamSource(new File(RESOURCES_DIR + "//data.xml"));
                // create an instance of fop factory
                FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
                // a user agent is needed for transformation
                FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
                // Setup output
                OutputStream out;
                out = new java.io.FileOutputStream(OUTPUT_DIR + "//output.pdf");

                try {
                    // Construct fop with desired output format
                    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

                    // Setup XSLT
                    TransformerFactory factory = TransformerFactory.newInstance();
                    Transformer transformer = factory.newTransformer(new StreamSource(xsltFile));

                    // Resulting SAX events (the generated FO) must be piped through to
                    // FOP
                    Result res = new SAXResult(fop.getDefaultHandler());

                    // Start XSLT transformation and FOP processing
                    // That's where the XML is first transformed to XSL-FO and then
                    // PDF is created
                    transformer.transform(xmlSource, res);
                } finally {
                    out.close();
                }
            }

我在“ FopFactory fopFactory = FopFactory.newInstance(new File(”。“)。toURI());”上收到错误,完全是newInstance:“ FopFactory类型的newInstance()方法不适用于该参数(URI)”。

我需要添加一个.jar吗?

java版本:1.8.0

0 个答案:

没有答案