J2EE developer with over 7 years of experience in designing and implementing enterprise j2ee solutions based on open source technologies like Tapestry, Hibernate, Spring. Current interests include Tapestry, Plastic, Spock, Scala. Taha is a DZone MVB and is not an employee of DZone and has posted 40 posts at DZone. You can read more from them at their website. View Full User Profile

Tapestry Magic: Integrating With Flying Saucer, Generating PDF

  • submit to reddit

Even Tapestry has limitations!!. One of the limitation is not being able to get hold of the generated response for use, e.g. to send as an email or convert to PDF/Image etc. There are two ways of doing it(atleast Google search results say so). One is to use something like tapx-templating where in you have to create another Tapestry web-instance which can be used for generating content. The second one is to use an HTTP client to get the content from the Tapestry application as suggested by Josh Canfield in this mail.

We will use the second option to create a small tapestry module to integrate Tapestry with Flying Saucer. We will first add a new ComponentEventResultProcessor to process ITextRenderer. In Tapestry, an event handler can return different types of objects and each type is handled by its own ComponentEventResultProcessor. Flying Saucer uses ITextRenderer for rendering PDF. We will setup an ITextRenderer and then return it from our event handler. Based on the type of return value, ITextRendererResultProcessor will be called to create a response.

The ITextRendererResultProcessor is simple

public class ITextRendererResultProcessor implements
ComponentEventResultProcessor<ITextRenderer> {

private Response response;

public ITextRendererResultProcessor(Response response) {
this.response = response;

public void processResultValue(ITextRenderer renderer) throws IOException {
try {
} catch (DocumentException e) {
throw new RuntimeException("Failed to create PDF", e);


In the processResultValue() method, we use the renderer to create PDF by passing it the Response‘s OutputStream. We set the content-type to “application/pdf” so that browser will recognize the response as PDF.

We contribute this to the ComponentEventResultProcessor service

public class ExportsModule {

public void contributeComponentEventResultProcessor(
MappedConfiguration<Class, ComponentEventResultProcessor> configuration){
configuration.addInstance(ITextRenderer.class, ITextRendererResultProcessor.class);

Finally we create a simple component which can be used to create pdf for an Tapestry page.

public class PDFConverter implements ClientElement {
@Parameter(value = "prop:componentResources.id", defaultPrefix = BindingConstants.LITERAL, allowNull = false)
private String clientId;

@Parameter(value = "prop:componentResources.pageName", defaultPrefix = BindingConstants.LITERAL, allowNull = false)
private String page;

private String assignedClientId;

private JavaScriptSupport javaScriptSupport;

private ComponentResources resources;

private PageRenderLinkSource pageRenderLinkSource;

void setupRender() {
assignedClientId = javaScriptSupport.allocateClientId(clientId);

void beginRender(MarkupWriter writer) {
Link link = resources.createEventLink("createPDF", page);
writer.element("a", "href", link.toAbsoluteURI(), "id", getClientId());

void afterRender(MarkupWriter writer) {

public String getClientId() {
return assignedClientId;

ITextRenderer onCreatePDF() {
ITextRenderer renderer = new ITextRenderer();
return renderer;


It is a simple component. It implements ClientElement interface so that the user can get unique javascript id at runtime. The rest is a simple rendering of a hyperlink tag with an event handler link. On clicking that link, onCreatePDF() event handler is called which gets the url for the page parameter and sets up the ITextRenderer.

That is it!!

From http://tawus.wordpress.com/2011/05/02/tapestry-magic-10/

Published at DZone with permission of Taha Siddiqi, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)