Search in sources :

Example 1 with Template

use of play.templates.Template in project play-cookbook by spinscale.

the class RenderPDF method apply.

@Override
public void apply(Request request, Response response) {
    Template template = TemplateLoader.load(templateFile);
    response.setHeader("Content-Disposition", "inline; filename=\"" + request.actionMethod + ".pdf\"");
    setContentTypeIfNotSet(response, "application/pdf");
    try {
        Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, response.out);
        Transformer transformer = tFactory.newTransformer();
        Scope.RenderArgs args = Scope.RenderArgs.current();
        String content = template.render(args.data);
        Source src = new StreamSource(IOUtils.toInputStream(content));
        javax.xml.transform.Result res = new SAXResult(fop.getDefaultHandler());
        transformer.transform(src, res);
    } catch (FOPException e) {
        Logger.error(e, "Error creating pdf");
    } catch (TransformerException e) {
        Logger.error(e, "Error creating pdf");
    }
}
Also used : Transformer(javax.xml.transform.Transformer) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Template(play.templates.Template) FOPException(org.apache.fop.apps.FOPException) Scope(play.mvc.Scope) SAXResult(javax.xml.transform.sax.SAXResult) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Source (javax.xml.transform.Source)1 Transformer (javax.xml.transform.Transformer)1 TransformerException (javax.xml.transform.TransformerException)1 SAXResult (javax.xml.transform.sax.SAXResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 FOPException (org.apache.fop.apps.FOPException)1 Fop (org.apache.fop.apps.Fop)1 Scope (play.mvc.Scope)1 Template (play.templates.Template)1