Tapestry Magic #10: Integrating With Flying Saucer, Generating PDF

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!!

Tagged: , ,

One thought on “Tapestry Magic #10: Integrating With Flying Saucer, Generating PDF

  1. NetDawg October 19, 2013 at 12:47 AM Reply

    Most promising solution. Can you provide a bit more detail or pointers to a project that actually renders PDF and HTML – both from the same request. Specifically, with HTML, including graphics, being rendered first, featuring a link to the corresponding PDF that looks exactly the same.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: