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