Tapestry Magic: Integrating With Flying Saucer, Generating PDF

DZone 's Guide to

Tapestry Magic: Integrating With Flying Saucer, Generating PDF

· Java Zone ·
Free Resource

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/


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}