Wiki source code of Development-PDF Generation
Version 9.1 by Theodore Petrosky on 2013/05/03 09:25
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
6.1 | 1 | == Overview == |
![]() |
1.1 | 2 | |
![]() |
7.1 | 3 | You might want to also review the [[Returning a File>>Development-Examples-Return a File]] example. |
![]() |
1.1 | 4 | |
![]() |
6.1 | 5 | == Zak Burke's Example == |
![]() |
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 | |||
![]() |
3.1 | 9 | {{code}} |
![]() |
1.1 | 10 | |
![]() |
3.1 | 11 | private ByteArrayOutputStream pdf() |
12 | { | ||
13 | // binary container for PDF | ||
14 | ByteArrayOutputStream out = null; | ||
![]() |
6.1 | 15 | |
![]() |
3.1 | 16 | try |
![]() |
1.1 | 17 | { |
![]() |
3.1 | 18 | // assume these exist |
19 | String xsl, xml; | ||
![]() |
6.1 | 20 | |
![]() |
3.1 | 21 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); |
![]() |
6.1 | 22 | |
![]() |
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); | ||
![]() |
6.1 | 27 | |
![]() |
3.1 | 28 | // configure XSLT transformer |
29 | Source xsltSrc = new StreamSource(new StringReader(xsl)); | ||
30 | Transformer transformer = transformerFactory.newTransformer(xsltSrc); | ||
![]() |
6.1 | 31 | |
![]() |
3.1 | 32 | // pipe XSL transformation through to FOP |
33 | Result res = new SAXResult(fop.getDefaultHandler()); | ||
![]() |
6.1 | 34 | |
![]() |
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); | ||
![]() |
1.1 | 40 | } |
![]() |
3.1 | 41 | catch (Exception e) |
42 | { | ||
43 | // actually, catch the following, one by one: | ||
44 | // TransformerConfigurationException | ||
45 | // FOPException | ||
46 | // TransformerFactoryConfigurationError | ||
47 | // TransformerException | ||
48 | } | ||
![]() |
6.1 | 49 | |
![]() |
3.1 | 50 | return out; |
51 | } | ||
![]() |
1.1 | 52 | |
![]() |
3.1 | 53 | public void appendToResponse(WOResponse response, WOContext context) |
54 | { | ||
55 | ByteArrayOutputStream out = pdf(); | ||
![]() |
6.1 | 56 | |
![]() |
3.1 | 57 | // without data, show the PDF page, which is just an error message. |
58 | if (out == null) | ||
59 | super.appendToResponse(response, context); | ||
![]() |
6.1 | 60 | |
![]() |
3.1 | 61 | // assume this exists |
62 | String filename; | ||
![]() |
6.1 | 63 | |
![]() |
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 | } | ||
![]() |
1.1 | 69 | |
![]() |
3.1 | 70 | {{/code}} |
71 | |||
![]() |
6.1 | 72 | == Related Links == |
![]() |
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/]] |