本文介绍了Fop例外FopFactory.newInstance()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用struts 2,我正在尝试使用fop从xml和xsl创建一个pdf文件。我在这个url的基础上开发我的代码和



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

  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\\\\\\\\\\\\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\\\
);
System.out.println(准备...);

文件baseDir =新文件(C:\\Users\\Administrator\\\workspace\\\\\\action\\\\\\\\\\\\\\\\\\\\\\\\ \\);
文件outDir = new File(baseDir,out);
outDir.mkdirs();

文件xmlfile = new File(baseDir,testeFOPxml.xml);
文件xsltfile = new File(baseDir,xml2fo.xsl);
文件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();
变压器变压器= factory.newTransformer(new StreamSource(xsltfile));

transformer.setParameter(versionParam,2.0);

源src = new StreamSource(xmlfile);

结果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 =printDomAction2class =com.somePackage.actions.JspToPdfTestmethod =xmlToPdf> - > 
< result type =streamname =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 =printDomActionclass =com.somePackage.actions.JspToPdfTestmethod =doPrint>
< result type =streamname =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>

finnaly 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);

源src = new StreamSource(xml);

源xsltSrc = new StreamSource(xsl);
变压器变压器= tFactory.newTransformer(xsltSrc);

结果res = new SAXResult(fop.getDefaultHandler());
transformer.transform(src,res);
} catch(Exception e){
e.printStackTrace(System.err);
}
退出;
}

在这两种情况下我都有:

  java.lang.NoSuchMethodError:
org.apache.xmlgraphics.util.Service.providerNames(Ljava / lang / Class;)Ljava / util / Iterator ;当我做FopFactory.newInstance();


$ b $时,

任何帮助,帮助我很好!

解决方案

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



顺便说一下,在浏览器上转换和显示的代码是pdf文件: / p>

 < action name =printDomActionclass =com.somePackage.actions.JspToPdfTestmethod =xmlToPdf> 
< result type =streamname =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>

和方法

私人InputStream inputStream; 

public String xmlToPdf(){
try {
System.out.println(FOP ExampleXML2PDF\\\
);
System.out.println(准备...);

//安装目录
文件baseDir =新文件(C:\\\\\\\\\\\\\\\\ \\形式\\);
文件outDir = new File(baseDir,out);
outDir.mkdirs();

//设置输入和输出文件
文件xmlfile = new File(baseDir,testeFOPxml.xml);
文件xsltfile = new File(baseDir,xml2fo.xsl);
文件pdffile = new File(outDir,ResultXML2PDF.pdf);

System.out.println(Input:XML(+ xmlfile +));
System.out.println(样式表:+ xsltfile);
System.out.println(Output:PDF(+ pdffile +));
System.out.println();
System.out.println(Transforming ...);

//根据需要配置fopFactory
final FopFactory fopFactory = FopFactory.newInstance();

FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
//根据需要配置foUserAgent

ByteArrayOutputStream out = new ByteArrayOutputStream();

TransformerFactory tFactory = TransformerFactory.newInstance();


Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,foUserAgent,out);
//设置输入
源src = new StreamSource(xmlfile);

//安装Transformer
源xsltSrc = new StreamSource(xsltfile);
变压器变压器= tFactory.newTransformer(xsltSrc);

//确保XSL转换的结果通过管道传递到FOP
结果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;
}

感谢大家的提示和帮助!


i'm using struts 2 and i'm trying to use fop to create a pdf file from xml and xsl. I develop my code in base this two url http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/java/embedding/ExampleXML2PDF.java?view=markup and http://justcode.wordpress.com/2009/01/16/generare-pdf-con-struts2-fop-xml-e-xslt/

here's my code, i'm trying using two different ways

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

and the struts.xml action

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

finnaly the xml2PdfManager.convertXML2PDF method:

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

in both situations i've got:

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

when i do the FopFactory.newInstance();

Any sugestion to help me woud be great!!

解决方案

I solved my problem! I had the xmlgraphics-commons-1.3 instead of the xmlgraphics-commons-1.5. It's strange because i used all the libraries that came with the fop bin.

By the way the code that convert and show on the browser the pdf file is the follow:

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

and the method

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

Thanks everybody for the tips and the help!

这篇关于Fop例外FopFactory.newInstance()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 09:46