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]