Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tapestry Wiki" for change notification.
The following page has been changed by DanielJue: http://wiki.apache.org/tapestry/Tapestry5HowToCreateADynamicPDF ------------------------------------------------------------------------------ === PdfWriterPage.java === - Here is the Java class behind the simple page. It is here that you would supply the custom data for your PDF, or at least direct an object's output into the PDF generator. With a little work, you can use this for exporting the contents of a Grid component, for example. PDF is a hugely complicated format--the spec is bigger than a phone book. The Lowagie IText library makes it easy, and it's free. + Here is the Java class behind the simple page. It is here that you would supply the custom data for your PDF, or at least direct an object's output into the PDF generator. You also can (should) supply the filename here. This implementation does not require you to add the extention, it's added for you in a later class you write. + + With a little work, you can use this for exporting the contents of a Grid component, for example. PDF is a hugely complicated format--the spec is bigger than a phone book. The Lowagie IText library makes it easy, and it's free. + [http://www.lowagie.com/iText/] + + {{{ import java.io.InputStream; @@ -56, +61 @@ public class PdfWriterPage { public StreamResponse onSubmit() { // Create PDF - InputStream is = PDFGenerator.generatePDF("Dynamically Generated PDF"); + InputStream is = PDFGenerator.generatePDF("This is the content of a Dynamically Generated PDF"); // Return response - return new PDFStreamResponse(is); + return new PDFStreamResponse(is,"MyDynamicSample"); } } }}} @@ -110, +115 @@ === PDFStreamResponse.java === This is the type of object that the method in your page will return. Note the getContentType() method. If you are planning on returning something else, like a jpg, zip, mp3, etc., you will need to find the appropriate content type String for that. Otherwise, if it is wrong or blank, your browser may display it incorrectly or give you an error. These content types help trigger plugins associated with your browser. + + Also notice the header being set in the prepareResponse() method. This is how your browser will know the default file name of the object you are streaming back. Since it also specifies "attachment", it will raise a file download box. Now if the user's browser has a plugin (i.e. Acrobat Reader) that intercepts the file type, it may simply open in a plugin and not display the file download box. + You can read more about content-disposition here: [http://www.ietf.org/rfc/rfc1806.txt] {{{ package myapp.model.services.util; @@ -122, +130 @@ public class PDFStreamResponse implements StreamResponse { private InputStream is; + private String filename; - public PDFStreamResponse(InputStream is) { + public PDFStreamResponse(InputStream is, String filename) { this.is = is; + this.filename = filename; + } public String getContentType() { @@ -136, +147 @@ } public void prepareResponse(Response arg0) { - // TODO Auto-generated method stub - + arg0.setHeader("Content-Disposition", "attachment; filename=" + + filename + ".pdf"); } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
