<tfoot id='dXw8u'></tfoot>
        <legend id='dXw8u'><style id='dXw8u'><dir id='dXw8u'><q id='dXw8u'></q></dir></style></legend>

      1. <i id='dXw8u'><tr id='dXw8u'><dt id='dXw8u'><q id='dXw8u'><span id='dXw8u'><b id='dXw8u'><form id='dXw8u'><ins id='dXw8u'></ins><ul id='dXw8u'></ul><sub id='dXw8u'></sub></form><legend id='dXw8u'></legend><bdo id='dXw8u'><pre id='dXw8u'><center id='dXw8u'></center></pre></bdo></b><th id='dXw8u'></th></span></q></dt></tr></i><div id='dXw8u'><tfoot id='dXw8u'></tfoot><dl id='dXw8u'><fieldset id='dXw8u'></fieldset></dl></div>

        <small id='dXw8u'></small><noframes id='dXw8u'>

          <bdo id='dXw8u'></bdo><ul id='dXw8u'></ul>

        FopFactory.newInstance() 时出现 Fop 异常

        Fop exception when FopFactory.newInstance()(FopFactory.newInstance() 时出现 Fop 异常)

            • <small id='phepM'></small><noframes id='phepM'>

                <tbody id='phepM'></tbody>
              <tfoot id='phepM'></tfoot>

                <legend id='phepM'><style id='phepM'><dir id='phepM'><q id='phepM'></q></dir></style></legend>
                  <bdo id='phepM'></bdo><ul id='phepM'></ul>
                  <i id='phepM'><tr id='phepM'><dt id='phepM'><q id='phepM'><span id='phepM'><b id='phepM'><form id='phepM'><ins id='phepM'></ins><ul id='phepM'></ul><sub id='phepM'></sub></form><legend id='phepM'></legend><bdo id='phepM'><pre id='phepM'><center id='phepM'></center></pre></bdo></b><th id='phepM'></th></span></q></dt></tr></i><div id='phepM'><tfoot id='phepM'></tfoot><dl id='phepM'><fieldset id='phepM'></fieldset></dl></div>
                  本文介绍了FopFactory.newInstance() 时出现 Fop 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 struts 2,我正在尝试使用 fop 从 xml 和 xsl 创建一个 pdf 文件.我在这两个 url 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/

                  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
                  ");
                                      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 动作

                  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>
                  

                  最后是 xml2PdfManager.convertXML2PDF 方法:

                  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; 
                  

                  当我执行 FopFactory.newInstance();

                  when i do the FopFactory.newInstance();

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

                  Any sugestion to help me woud be great!!

                  推荐答案

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

                  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.

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

                  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>
                  

                  方法

                  private InputStream inputStream;
                  
                  public String xmlToPdf() {
                                  try {
                                      System.out.println("FOP ExampleXML2PDF
                  ");
                                      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!

                  这篇关于FopFactory.newInstance() 时出现 Fop 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)
                  How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)
                  Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)
                  Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)
                  How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)
                  How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)
                  1. <tfoot id='nUaZ9'></tfoot>

                        <tbody id='nUaZ9'></tbody>

                        <small id='nUaZ9'></small><noframes id='nUaZ9'>

                        <i id='nUaZ9'><tr id='nUaZ9'><dt id='nUaZ9'><q id='nUaZ9'><span id='nUaZ9'><b id='nUaZ9'><form id='nUaZ9'><ins id='nUaZ9'></ins><ul id='nUaZ9'></ul><sub id='nUaZ9'></sub></form><legend id='nUaZ9'></legend><bdo id='nUaZ9'><pre id='nUaZ9'><center id='nUaZ9'></center></pre></bdo></b><th id='nUaZ9'></th></span></q></dt></tr></i><div id='nUaZ9'><tfoot id='nUaZ9'></tfoot><dl id='nUaZ9'><fieldset id='nUaZ9'></fieldset></dl></div>
                          <bdo id='nUaZ9'></bdo><ul id='nUaZ9'></ul>

                          <legend id='nUaZ9'><style id='nUaZ9'><dir id='nUaZ9'><q id='nUaZ9'></q></dir></style></legend>