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");
}
}
Aggregations