gpt4 book ai didi

java - FopFactory.newInstance() 时出现 Fop 异常

转载 作者:行者123 更新时间:2023-11-30 09:24:49 25 4
gpt4 key购买 nike

我正在使用 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/

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

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

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

感谢大家的提示和帮助!

关于java - FopFactory.newInstance() 时出现 Fop 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15506922/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com