Wiki source code of Programming__WebObjects-Web Applications-Development-PDF Generation
Version 1.1 by smmccraw on 2007/07/08 09:46
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | == Overview == | ||
2 | |||
3 | You might want to also review the [[Returning a File>>Programming__WebObjects-Web Applications-Development-Examples-Return a File]] example. | ||
4 | |||
5 | == Zak Burke's Example == | ||
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 | |||
9 | {{panel}} | ||
10 | |||
11 | private ByteArrayOutputStream pdf() | ||
12 | { | ||
13 | // binary container for PDF | ||
14 | ByteArrayOutputStream out = null; | ||
15 | |||
16 | try | ||
17 | { | ||
18 | // assume these exist | ||
19 | String xsl, xml; | ||
20 | |||
21 | TransformerFactory transformerFactory = TransformerFactory.newInstance(); | ||
22 | |||
23 | // configure FOP, apache's XML->PDF transformer | ||
24 | Fop fop = new Fop(MimeConstants.MIME_PDF); | ||
25 | out = new ByteArrayOutputStream(); | ||
26 | fop.setOutputStream(out); | ||
27 | |||
28 | // configure XSLT transformer | ||
29 | Source xsltSrc = new StreamSource(new StringReader(xsl)); | ||
30 | Transformer transformer = transformerFactory.newTransformer(xsltSrc); | ||
31 | |||
32 | // pipe XSL transformation through to FOP | ||
33 | Result res = new SAXResult(fop.getDefaultHandler()); | ||
34 | |||
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); | ||
40 | } | ||
41 | catch (Exception e) | ||
42 | { | ||
43 | // actually, catch the following, one by one: | ||
44 | // TransformerConfigurationException | ||
45 | // FOPException | ||
46 | // TransformerFactoryConfigurationError | ||
47 | // TransformerException | ||
48 | } | ||
49 | |||
50 | return out; | ||
51 | } | ||
52 | |||
53 | public void appendToResponse(WOResponse response, WOContext context) | ||
54 | { | ||
55 | ByteArrayOutputStream out = pdf(); | ||
56 | |||
57 | // without data, show the PDF page, which is just an error message. | ||
58 | if (out == null) | ||
59 | super.appendToResponse(response, context); | ||
60 | |||
61 | // assume this exists | ||
62 | String filename; | ||
63 | |||
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 | } | ||
69 | |||
70 | {{/panel}} | ||
71 | |||
72 | == Related Links == | ||
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 | * [[WebPDF Reports>>http://www.secondmove.com/WebPDFReports/index.shtml.en]] | ||
85 | * [[XSL:FO Examples>>http://www.dpawson.co.uk/xsl/sect3/]] | ||
86 | * [[ReportMill>>http://www.reportmill.com/product/]] | ||
87 | |||
88 | Category:WebObjects |