我解决了我的问题!我有xmlgraphics-commons-1.3而不是xmlgraphics-commons-1.5。这很奇怪,因为我使用了fop
bin附带的所有库。
顺便说一下,转换并在浏览器中显示pdf文件的代码如下:
<action name="printDomAction" 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 ExampleXML2PDFn"); 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; }
感谢大家的提示和帮助!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)