Wiki source code of Development-PDF Generation

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

Show last authors
1 == Overview ==
2
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.
4
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.
6
7 == Zak Burke's Example ==
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
11 {{code}}
12
13 private ByteArrayOutputStream pdf()
14 {
15 // binary container for PDF
16 ByteArrayOutputStream out = null;
17
18 try
19 {
20 // assume these exist
21 String xsl, xml;
22
23 TransformerFactory transformerFactory = TransformerFactory.newInstance();
24
25 // configure FOP, apache's XML->PDF transformer
26 Fop fop = new Fop(MimeConstants.MIME_PDF);
27 out = new ByteArrayOutputStream();
28 fop.setOutputStream(out);
29
30 // configure XSLT transformer
31 Source xsltSrc = new StreamSource(new StringReader(xsl));
32 Transformer transformer = transformerFactory.newTransformer(xsltSrc);
33
34 // pipe XSL transformation through to FOP
35 Result res = new SAXResult(fop.getDefaultHandler());
36
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);
42 }
43 catch (Exception e)
44 {
45 // actually, catch the following, one by one:
46 // TransformerConfigurationException
47 // FOPException
48 // TransformerFactoryConfigurationError
49 // TransformerException
50 }
51
52 return out;
53 }
54
55 public void appendToResponse(WOResponse response, WOContext context)
56 {
57 ByteArrayOutputStream out = pdf();
58
59 // without data, show the PDF page, which is just an error message.
60 if (out == null)
61 super.appendToResponse(response, context);
62
63 // assume this exists
64 String filename;
65
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 }
71
72 {{/code}}
73
74 == Related Links ==
75
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"]]