I am using jasperreports to generate .pdf files and tapestry to deliver
it to the user.


>From the .page

   <component id="GENINVOICE_PDF" type="Submit">
      <binding name="listener"
expression="listeners.downloadInvoicePDF"/>
    </component>

>From the .java

    public void downloadInvoicePDF(IRequestCycle cycle) {
        try {
            HttpServletResponse response =
cycle.getRequestContext().getResponse();
            ServletOutputStream out = response.getOutputStream();

            OrderBillGenerator gen = new OrderBillGenerator(getOrder());
            ByteArrayOutputStream reportStream =
gen.generateOrderBillPDF();

            byte[] buff = reportStream.toByteArray();

            response.setHeader("Content-disposition", "inline;
filename=demo.pdf");
            response.setContentType("application/pdf");
            response.setContentLength(buff.length);

            out.write(buff);

            response.flushBuffer();

            out.close();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }



Cheers,
Detlef


-----Original Message-----
From: Craig Turner [mailto:[EMAIL PROTECTED] 
Sent: Dienstag, 28. Marz 2006 03:11
To: Tapestry users
Subject: Re: tapestry for PDFs and DOCs.


This is a response to quite an old thread - is it possible to do this in

tapestry-3.0.3? (I'm guessing hivemodule.xml is a tapestry4 
configuration issue)

Alternatively, is it possible to somehow change the IEngine after the 
page has been constructed? If so then I could handle the PDF page as 
though it were a normal page and then during the attach get it to change

its engine to my special PDF output version. I suppose yet another 
alternative would be to write the PDF handling part in straight servlets

and then configure my container to use those servlets for that data. Are

there people on the list who have implemented PDF reports as part of 
their apps with strong feelings about any ofthese strategies?

>> Hi Rusty, I hope this helps you:
>>
>> Below the code to stream pdf's with a custom engine service.
>>
>> In my opinion the best solution is to use a custom Engine service, I
>> use one to render to PDF with itext. Notice that the 
>> HttpServletResponse and request services get autowired to your
service 
>> via setter dependency injection so that you can use it to stream the 
>> pdf to the browser. I use the same type of service not only to stream

>> pdf but anything you want such as images, xml or any other markup.
>>
>> Experts, please correct me if I'm wrong, but this approach does not
>> open new httpsessions and gives you access to all the 
>> httpServletResponse and request stuff.
>>
>> Raul Raja.
>>
>> //hivemodule.xml
>>
>> <contribution 
>> configuration-id="tapestry.services.ApplicationServices">
>> <service name="pdf" object="service:CMS.PDFServer" />
>> </contribution>
>>
>>
>> <service-point id="PDFServer"
>> interface="org.apache.tapestry.engine.IEngineService">
>>  <invoke-factory model="singleton">
>>  <construct class="com.estudiowebs.CMS.webservices.PDFServer" />
>> </invoke-factory>
>> </service-point>
>>
>>
>> //Java
>>
>> public class PDFServer implements IEngineService {
>>
>>  private void makePdf(HttpServletRequest request, HttpServletResponse
>> response) {
>>    try {
>>      Document document = new Document();
>>      ByteArrayOutputStream baos = new ByteArrayOutputStream();
>>      PdfWriter.getInstance(document, baos);
>>      document.open();
>>
>>
>>      document.add(new Paragraph("Hello World"));
>>      document.add(Chunk.NEWLINE);
>>
>>
>>     document.close();
>>
>>      // setting some response headers
>>      response.setHeader("Expires", "0");
>>      response.setHeader("Cache-Control", "must-revalidate,
>> post-check=0, pre-check=0");
>>      response.setHeader("Pragma", "public");
>>      // setting the content type
>>      response.setContentType("application/pdf");
>>      // the contentlength is needed for MSIE!!!
>>      response.setContentLength(baos.size());
>>      // write ByteArrayOutputStream to the ServletOutputStream
>>      ServletOutputStream out = response.getOutputStream();
>>      baos.writeTo(out);
>>      out.flush();
>>
>>        } catch (Exception e2) {
>>                //do whatever
>>        }
>>
>>        public String getName() {
>>      return "pdf";
>>        }
>>
>>       private HttpServletResponse response;
>>
>>        public void setResponse (HttpServletResponse response) {
>>      this.response = response;
>>        }
>>
>>       private HttpServletRequest request;
>>
>>        public void setRequest (HttpServletRequest request) {
>>      this.request = request;
>>        }
>>
>>       public void service(IRequestCycle arg0) throws IOException {
>>
>>     makePdf(request, response);
>>
>>       }
>>
>>        public ILink getLink(boolean arg0, Object arg1) {
>>       // TODO Auto-generated method stub
>>       return null;
>>       }
>> }
>>
>>
>> You can test that it works pointing your browser to: 
>> http://yourserver.com:8080/yourtapestryapp/app?service=pdf
>>
>>
>>
>> Rusty Phillips wrote:
>>
>>>  I'd like to use tapestry to render things besides HTML files, but 
>>> I'm  not quite clear on what needs to happen.  The biggest non-html 
>>> thing I'd  like to implement is PDFs via UJAC (which is an 
>>> XML-to-PDF library that  uses itext).  So the idea is that I'd make 
>>> a Tapestry template which is  then converted into UJAC XML which is 
>>> then rendered and sent to the  browser.
>>>
>>>
>>>
>>>  From the docs, I can see that I'll need to subclass  
>>> org.apache.tapestry.AbstractPage to override the 
>>> getResponseContentType  to set the content-type.
>>>
>>>  This one is fairly obvious.
>>>
>>>  How I set the other http headers isn't quite so obvious to me, but 
>>> it  there is some mention of doing it with the beginResponse method.

>>> I  don't see exactly how that works.  Does beginResponse have access

>>> to the  servlet request object somewhere that I'm not aware of?
>>>
>>>
>>>
>>>  Once I've done that, it looks like I would override one of the 
>>> methods  that gets called at the end of the rendering cycle to 
>>> convert the XML  output into PDF output.  Same thing:  I'm not quite

>>> sure how it works.  I would want to render all of the child 
>>> components, and then render the  result in a completely different 
>>> way.
>>>
>>>
>>>
>>>  Does anyone have any thoughts about how to do this kind of thing?
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: 
>> [EMAIL PROTECTED]
>>
>>
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to