DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Tapestry Magic: Integrating With Flying Saucer, Generating PDF

Tapestry Magic: Integrating With Flying Saucer, Generating PDF

Taha Siddiqi user avatar by
Taha Siddiqi
·
Jun. 12, 11 · Java Zone · Interview
Like (0)
Save
Tweet
5.86K Views

Join the DZone community and get the full member experience.

Join For Free

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 {
response.disableCompression();
renderer.createPDF(response.getOutputStream("application/pdf"));
} 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 {

@SuppressWarnings("unchecked")
@Contribute(ComponentEventResultProcessor.class)
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;

@Inject
private JavaScriptSupport javaScriptSupport;

@Inject
private ComponentResources resources;

@Inject
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) {
writer.end();
}

public String getClientId() {
return assignedClientId;
}

ITextRenderer onCreatePDF() {
ITextRenderer renderer = new ITextRenderer();
renderer.setDocument(pageRenderLinkSource.createPageRenderLink(page)
.toAbsoluteURI());
renderer.layout();
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/

PDF

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • How to Get GDPR and Customer Communications Right
  • Terraform Controller: Cloud Resource Self-Service
  • Kubernetes Service Types Explained In-Detail
  • SSH Tutorial: Nice and Easy [Video]

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo