当前位置: 首页 > 面试题库 >

FopFactory.newInstance()时出现Fop异常

从阎宝
2023-03-14
问题内容

我正在使用struts
2,并且正在尝试使用fop从xml和xsl创建pdf文件。我根据这两个网址http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup和http://justcode.wordpress开发了我的代码.com / 2009/01/16 / generare-pdf-con-
struts2-fop-xml-e-xslt /

这是我的代码,我正在尝试使用两种不同的方式

public class JspToPdfTest extends ActionSupport{

private InputStream inputStream;
private Xml2PdfManager xml2PdfManager;

public InputStream getInputStream() {
    return inputStream;
}
public void setInputStream(InputStream inputStream) {
    this.inputStream = inputStream;
}   
public JspToPdfTest(){
    xml2PdfManager = new Xml2PdfManager();
}

public String doPrint()  {
    String xml = "C:\\Users\\Administrator\\workspace\\path\\actions\\forms\\testeFOPxml.xml";
    String xslPath = "C:\\Users\\Administrator\\workspace\\path\\actions\\forms\\xml2fo.xsl";
    InputStream xmlInput = new ByteArrayInputStream(xml.getBytes());

    ByteArrayOutputStream out = (ByteArrayOutputStream) xml2PdfManager.convertXML2PDF(xmlInput, new File(xslPath));

    inputStream = new ByteArrayInputStream(out.toByteArray());
    return SUCCESS;
}

public String xmlToPdf() {
                try {
                    System.out.println("FOP ExampleXML2PDF\n");
                    System.out.println("Preparing...");

                    File baseDir = new File("C:\\Users\\Administrator\\workspace\\path\\actions\\forms\\");
                    File outDir = new File(baseDir, "out");
                    outDir.mkdirs();

                    File xmlfile = new File(baseDir, "testeFOPxml.xml");
                    File xsltfile = new File(baseDir, "xml2fo.xsl");
                    File pdffile = new File(outDir, "ResultXML2PDF.pdf");

                    final FopFactory fopFactory = FopFactory.newInstance();

                    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

                    OutputStream out = new java.io.FileOutputStream(pdffile);
                    out = new java.io.BufferedOutputStream(out);

                    try {
                        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

                    TransformerFactory factory = TransformerFactory.newInstance();
                    Transformer transformer = factory.newTransformer(new StreamSource(xsltfile));

                        transformer.setParameter("versionParam", "2.0");

                        Source src = new StreamSource(xmlfile);

                        Result res = new SAXResult(fop.getDefaultHandler());

                        transformer.transform(src, res);
                    } finally {
                        out.close();
                    }
                } catch (Exception e) {
                e.printStackTrace(System.err);
                    System.exit(-1);
                }
            return SUCCESS;
            }
}

和struts.xml动作

<action name="printDomAction2" class="com.somePackage.actions.JspToPdfTest"  method="xmlToPdf"> -->
        <result type="stream" name="success"> 
            <param name="contentType">application/pdf</param>
            <param name="contentDisposition">filename="dom.pdf"</param> 
            <param name="bufferSize">1024</param> 
        </result> 
        <interceptor-ref name="defaultStack"/> 
    </action>

    <action name="printDomAction" class="com.somePackage.actions.JspToPdfTest"  method="doPrint">
        <result type="stream" name="success">
            <param name="contentType">application/pdf</param>
            <param name="contentDisposition">filename="dom.pdf"</param>
            <param name="bufferSize">1024</param>
        </result>
        <interceptor-ref name="defaultStack"/>
    </action>

最终使用xml2PdfManager.convertXML2PDF方法:

public OutputStream convertXML2PDF(InputStream xml, File xsl) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    FopFactory fopFactory = FopFactory.newInstance();

    TransformerFactory tFactory = TransformerFactory.newInstance();

    try {
    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

    Source src = new StreamSource(xml);

    Source xsltSrc = new StreamSource(xsl);
    Transformer transformer = tFactory.newTransformer(xsltSrc);

    Result res = new SAXResult(fop.getDefaultHandler());
    transformer.transform(src, res);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
    return out;
    }

在两种情况下,我都有:

java.lang.NoSuchMethodError:     
org.apache.xmlgraphics.util.Service.providerNames(Ljava/lang/Class;)Ljava/util/Iterator;

当我做FopFactory.newInstance();

任何帮助我的建议都会很棒!


问题答案:

我解决了我的问题!我有xmlgraphics-commons-1.3而不是xmlgraphics-commons-1.5。这很奇怪,因为我使用了fop
bin附带的所有库。

顺便说一下,转换并在浏览器中显示pdf文件的代码如下:

<action name="printDomAction" class="com.somePackage.actions.JspToPdfTest"  method="xmlToPdf">
        <result type="stream" name="success">
            <param name="contentType">application/pdf</param>
            <param name="contentDisposition">filename="ResultXML2PDF.pdf"</param>
            <param name="bufferSize">1024</param>
        </result>
        <interceptor-ref name="defaultStack"/>
    </action>

和方法

private InputStream inputStream;

public String xmlToPdf() {
                try {
                    System.out.println("FOP ExampleXML2PDF\n");
                    System.out.println("Preparing...");

                    // Setup directories
                    File baseDir = new File("C:\\Users\\Administrator\\workspace\\path\\actions\\forms\\");
                    File outDir = new File(baseDir, "out");
                    outDir.mkdirs();

                    // Setup input and output files
                    File xmlfile = new File(baseDir, "testeFOPxml.xml");
                    File xsltfile = new File(baseDir, "xml2fo.xsl");
                    File pdffile = new File(outDir, "ResultXML2PDF.pdf");

                    System.out.println("Input: XML (" + xmlfile + ")");
                    System.out.println("Stylesheet: " + xsltfile);
                    System.out.println("Output: PDF (" + pdffile + ")");
                    System.out.println();
                    System.out.println("Transforming...");

                    // configure fopFactory as desired
                    final FopFactory fopFactory = FopFactory.newInstance();

                    FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
                    // configure foUserAgent as desired

                    ByteArrayOutputStream out = new ByteArrayOutputStream();

                    TransformerFactory tFactory = TransformerFactory.newInstance();


                    Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,foUserAgent, out);
                    //Setup input
                    Source src = new StreamSource(xmlfile);

                    //Setup Transformer
                    Source xsltSrc = new StreamSource(xsltfile);
                    Transformer transformer = tFactory.newTransformer(xsltSrc);

                    //Make sure the XSL transformation's result is piped through to FOP
                    Result res = new SAXResult(fop.getDefaultHandler());
                    transformer.transform(src, res);

                    System.out.println("Success!");
                    ByteArrayOutputStream baos = out;
                    inputStream = new ByteArrayInputStream(baos.toByteArray());
                } catch (Exception e) {
                e.printStackTrace(System.err);
                    System.exit(-1);
                }

            return SUCCESS;
            }

感谢大家的提示和帮助!



 类似资料:
  • 我正在使用fop将fo文件转换为pdf。请参阅例外: 我使用jTidy转换xhtml和xalan从antennahouse将xhtml转换为xsl样式的fo文件。 它是fo文件的第44行: 如何解决这个问题。谢谢 更新:XHTML 代码 这是我的java代码

  • 原因:组件组织。neo4j。内核impl。交易XaDataSourceManager@2a792260'已成功初始化,但无法启动。请参阅随附的原因例外。 CausedBy:组件org.neo4j.kernel.impl.nioneo.xa.NeoStoreXaDataSource@23d7c3d2已成功初始化,但无法启动。请参阅附件中的原因异常。 原因:没有架构索引提供程序组织。neo4j。内核应

  • 以下是在sun.reflect.nativeMethodAccessorImpl.Invoke0(本机方法)在sun.reflect.nativeMethodAccessorImpl.Invoke(未知源)在sun.reflect.NativeMethodAccessorImpl.Invoke(未知源)在java.lang.Reflect.Method.Invoke(未知源)在com.codena

  • 我正在尝试运行MipSdk-Pol 获取策略失败,请求失败,http状态代码:204 当使用用户凭证运行预编译文件API示例时,也会发生同样的情况: 据我所知,我有一个E5试用版,策略和标签都设置正确。我很乐意得到任何建议,谢谢!

  • 我的ArrayIndexOutOfBounds 8出现错误,我不确定原因-我的图像从0开始。行处理似乎突出显示为

  • 我正在将Jenkins服务器升级到2.357,并将Java从1.8升级到11以支持它。 然而,当我启动Jenkins时,它失败了,并给出了以下错误 Java版本 看起来它指向了线程“main”java中的异常。lang.IllegalArgumentException:期望----前缀=值,但未找到值,但我似乎无法理解。如果您能帮忙,我们将不胜感激!