Wiki source code of Development-PDF Generation

Version 9.1 by Theodore Petrosky on 2013/05/03 09:25

Hide last authors
Pascal Robert 6.1 1 == Overview ==
smmccraw 1.1 2
Pascal Robert 7.1 3 You might want to also review the [[Returning a File>>Development-Examples-Return a File]] example.
smmccraw 1.1 4
Pascal Robert 6.1 5 == Zak Burke's Example ==
smmccraw 1.1 6
7 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.
8
Pascal Robert 3.1 9 {{code}}
smmccraw 1.1 10
Pascal Robert 3.1 11 private ByteArrayOutputStream pdf()
12 {
13 // binary container for PDF
14 ByteArrayOutputStream out = null;
Pascal Robert 6.1 15
Pascal Robert 3.1 16 try
smmccraw 1.1 17 {
Pascal Robert 3.1 18 // assume these exist
19 String xsl, xml;
Pascal Robert 6.1 20
Pascal Robert 3.1 21 TransformerFactory transformerFactory = TransformerFactory.newInstance();
Pascal Robert 6.1 22
Pascal Robert 3.1 23 // configure FOP, apache's XML->PDF transformer
24 Fop fop = new Fop(MimeConstants.MIME_PDF);
25 out = new ByteArrayOutputStream();
26 fop.setOutputStream(out);
Pascal Robert 6.1 27
Pascal Robert 3.1 28 // configure XSLT transformer
29 Source xsltSrc = new StreamSource(new StringReader(xsl));
30 Transformer transformer = transformerFactory.newTransformer(xsltSrc);
Pascal Robert 6.1 31
Pascal Robert 3.1 32 // pipe XSL transformation through to FOP
33 Result res = new SAXResult(fop.getDefaultHandler());
Pascal Robert 6.1 34
Pascal Robert 3.1 35 // grab XML input stream
36 Source src = new StreamSource(new StringReader(xml));
37
38 // Start the transformation and rendering process
39 transformer.transform(src, res);
smmccraw 1.1 40 }
Pascal Robert 3.1 41 catch (Exception e)
42 {
43 // actually, catch the following, one by one:
44 // TransformerConfigurationException
45 // FOPException
46 // TransformerFactoryConfigurationError
47 // TransformerException
48 }
Pascal Robert 6.1 49
Pascal Robert 3.1 50 return out;
51 }
smmccraw 1.1 52
Pascal Robert 3.1 53 public void appendToResponse(WOResponse response, WOContext context)
54 {
55 ByteArrayOutputStream out = pdf();
Pascal Robert 6.1 56
Pascal Robert 3.1 57 // without data, show the PDF page, which is just an error message.
58 if (out == null)
59 super.appendToResponse(response, context);
Pascal Robert 6.1 60
Pascal Robert 3.1 61 // assume this exists
62 String filename;
Pascal Robert 6.1 63
Pascal Robert 3.1 64 response.setHeader("application/pdf", "Content-Type");
65 response.setHeader("" + out.size() + "", "Content-Length");
66 response.setHeader("attachment;filename=" + filename, "Content-Disposition");
67 response.setContent(new NSData(out.toByteArray()));
68 }
smmccraw 1.1 69
Pascal Robert 3.1 70 {{/code}}
71
Pascal Robert 6.1 72 == Related Links ==
smmccraw 1.1 73
74 * [[FyTek Exe/DLL PDF Libraries>>http://www.fytek.com]]
75 * [[Java PDF Libraries>>http://schmidt.devlib.org/java/libraries-pdf.html]]
76 * [[Apache FOP>>http://xmlgraphics.apache.org/fop/]]
77 * [[Deirdre Saoirse Moen's FOP Example>>http://deirdre.net/posts/2005/06/webobjects-and-pdf-generation/]]
78 * [[JClass>>http://www.sitraka.com/software/jclass]]
79 * [[Etymon PJ>>http://www.etymon.com/pj/]]
80 * [[PDFLib>>http://www.pdflib.com/]]
81 * [[PD4ML>>http://pd4ml.com/]]
82 * [[iText>>http://www.lowagie.com/iText]]
83 * [[Hugi Karlmenn's iText Example>>http://hugi.karlmenn.is/page/webobjects]]
84 * [[XSL:FO Examples>>http://www.dpawson.co.uk/xsl/sect3/]]
85 * [[ReportMill>>http://www.reportmill.com/product/]]