Wiki source code of Development-PDF Generation

Last modified by Pascal Robert on 2013/05/03 09:29

Hide last authors
Pascal Robert 6.1 1 == Overview ==
smmccraw 1.1 2
Pascal Robert 14.1 3 You might want to also review the [[Returning a File>>doc:documentation.Home.How-tos.Development-Examples.Development-Examples-Return a File.WebHome]] example.
smmccraw 1.1 4
Theodore Petrosky 11.1 5 (5-2-2013) JasperReports was added to Wonder last year. Kieran Kelleher has a wonderful (forgive the pun) presentation from 2010 Integrating and using JasperReports in your WebObjects App. Availabe in the WebObjects podcasts.
Theodore Petrosky 10.1 6
Pascal Robert 6.1 7 == Zak Burke's Example ==
smmccraw 1.1 8
9 It is important to add "-Djava.awt.headless=true" to the "Additional Arguments" list to suppress a console window running WOBootstrap that will try to pop up and then fail to close, which seems to prevent the thread from returning, which causes the app to hang.
10
Pascal Robert 3.1 11 {{code}}
smmccraw 1.1 12
Pascal Robert 3.1 13 private ByteArrayOutputStream pdf()
14 {
15 // binary container for PDF
16 ByteArrayOutputStream out = null;
Pascal Robert 6.1 17
Pascal Robert 3.1 18 try
smmccraw 1.1 19 {
Pascal Robert 3.1 20 // assume these exist
21 String xsl, xml;
Pascal Robert 6.1 22
Pascal Robert 3.1 23 TransformerFactory transformerFactory = TransformerFactory.newInstance();
Pascal Robert 6.1 24
Pascal Robert 3.1 25 // configure FOP, apache's XML->PDF transformer
26 Fop fop = new Fop(MimeConstants.MIME_PDF);
27 out = new ByteArrayOutputStream();
28 fop.setOutputStream(out);
Pascal Robert 6.1 29
Pascal Robert 3.1 30 // configure XSLT transformer
31 Source xsltSrc = new StreamSource(new StringReader(xsl));
32 Transformer transformer = transformerFactory.newTransformer(xsltSrc);
Pascal Robert 6.1 33
Pascal Robert 3.1 34 // pipe XSL transformation through to FOP
35 Result res = new SAXResult(fop.getDefaultHandler());
Pascal Robert 6.1 36
Pascal Robert 3.1 37 // grab XML input stream
38 Source src = new StreamSource(new StringReader(xml));
39
40 // Start the transformation and rendering process
41 transformer.transform(src, res);
smmccraw 1.1 42 }
Pascal Robert 3.1 43 catch (Exception e)
44 {
45 // actually, catch the following, one by one:
46 // TransformerConfigurationException
47 // FOPException
48 // TransformerFactoryConfigurationError
49 // TransformerException
50 }
Pascal Robert 6.1 51
Pascal Robert 3.1 52 return out;
53 }
smmccraw 1.1 54
Pascal Robert 3.1 55 public void appendToResponse(WOResponse response, WOContext context)
56 {
57 ByteArrayOutputStream out = pdf();
Pascal Robert 6.1 58
Pascal Robert 3.1 59 // without data, show the PDF page, which is just an error message.
60 if (out == null)
61 super.appendToResponse(response, context);
Pascal Robert 6.1 62
Pascal Robert 3.1 63 // assume this exists
64 String filename;
Pascal Robert 6.1 65
Pascal Robert 3.1 66 response.setHeader("application/pdf", "Content-Type");
67 response.setHeader("" + out.size() + "", "Content-Length");
68 response.setHeader("attachment;filename=" + filename, "Content-Disposition");
69 response.setContent(new NSData(out.toByteArray()));
70 }
smmccraw 1.1 71
Pascal Robert 3.1 72 {{/code}}
73
Pascal Robert 6.1 74 == Related Links ==
smmccraw 1.1 75
Theodore Petrosky 12.1 76 * [[FyTek Exe/DLL PDF Libraries>>url:http://www.fytek.com||shape="rect"]]
77 * [[Java PDF Libraries>>url:http://schmidt.devlib.org/java/libraries-pdf.html||shape="rect"]]
78 * [[Apache FOP>>url:http://xmlgraphics.apache.org/fop/||shape="rect"]]
79 * [[Deirdre Saoirse Moen's FOP Example>>url:http://deirdre.net/posts/2005/06/webobjects-and-pdf-generation/||shape="rect"]]
80 * [[JClass>>url:http://www.sitraka.com/software/jclass||shape="rect"]]
81 * [[Etymon PJ>>url:http://www.etymon.com/pj/||shape="rect"]]
82 * [[PDFLib>>url:http://www.pdflib.com/||shape="rect"]]
83 * [[PD4ML>>url:http://pd4ml.com/||shape="rect"]]
84 * [[iText>>url:http://www.lowagie.com/iText||shape="rect"]]
85 * [[Hugi Karlmenn's iText Example>>url:http://hugi.karlmenn.is/page/webobjects||shape="rect"]]
86 * [[XSL:FO Examples>>url:http://www.dpawson.co.uk/xsl/sect3/||shape="rect"]]
87 * [[ReportMill>>url:http://www.reportmill.com/product/||shape="rect"]]