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

From version 3.1
edited by Pascal Robert
on 2007/09/03 14:13
Change comment: There is no comment for this version
To version 1.1
edited by smmccraw
on 2007/07/08 09:46
Change comment: There is no comment for this version

Summary

Details

Page properties
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.probert
1 +XWiki.smmccraw
Content
... ... @@ -1,74 +1,74 @@
1 1  == Overview ==
2 2  
3 -You might want to also review the [[Returning a File>>Web Applications-Development-Examples-Return a File]] example.
3 +You might want to also review the [[Returning a File>>Programming__WebObjects-Web Applications-Development-Examples-Return a File]] example.
4 4  
5 5  == Zak Burke's Example ==
6 6  
7 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 8  
9 -{{code}}
9 +{{panel}}
10 10  
11 -private ByteArrayOutputStream pdf()
12 -{
13 - // binary container for PDF
14 - ByteArrayOutputStream out = null;
15 -
16 - try
11 + private ByteArrayOutputStream pdf()
17 17   {
18 - // assume these exist
19 - String xsl, xml;
13 + // binary container for PDF
14 + ByteArrayOutputStream out = null;
20 20  
21 - TransformerFactory transformerFactory = TransformerFactory.newInstance();
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 + }
22 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);
50 + return out;
51 + }
52 +
53 + public void appendToResponse(WOResponse response, WOContext context)
54 + {
55 + ByteArrayOutputStream out = pdf();
27 27  
28 - // configure XSLT transformer
29 - Source xsltSrc = new StreamSource(new StringReader(xsl));
30 - Transformer transformer = transformerFactory.newTransformer(xsltSrc);
57 + // without data, show the PDF page, which is just an error message.
58 + if (out == null)
59 + super.appendToResponse(response, context);
31 31  
32 - // pipe XSL transformation through to FOP
33 - Result res = new SAXResult(fop.getDefaultHandler());
61 + // assume this exists
62 + String filename;
34 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);
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()));
40 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 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 -}
70 +{{/panel}}
69 69  
70 -{{/code}}
71 -
72 72  == Related Links ==
73 73  
74 74  * [[FyTek Exe/DLL PDF Libraries>>http://www.fytek.com]]