Over a million developers have joined DZone.

Tapestry Magic: Integrating With Flying Saucer, Generating PDF

· Java Zone

Easily build powerful user management, authentication, and authorization into your web and mobile applications. Download this Forrester report on the new landscape of Customer Identity and Access Management, brought to you in partnership with Stormpath.

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/

The Java Zone is brought to you by Stormpath—a complete, pre-built User Management API. Want to learn how to use JWTs to protect microservices from CSRF and more? Check out this on-demand webinar with our Java Developer Evangelist, Micah Silverman.


The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}