Community contributed extensions

PDF module

The PDF module allows you to render PDF document. It is rather simple to use as it only requires to import a static file.

Enable the PDF module for the application

In the /conf/application.conf file, enable the PDF module by adding this line:

# The PDF module
module.pdf=${play.path}/modules/pdf

Using the PDF module

The renderPDF() method:

import static play.modules.pdf.PDF.*;

And then use the renderPDF() method as you would use the render() method.

The renderPDF will first look for a .pdf template. If there are none, it will fall back to the .html template. You can use a media=“print” css to customize the PDF to render. The result is a PDF file. The render method uses the same template as for rendering HTML.

Please refer to the samples-and-test for an example.

Rendering multiple documents

You can render multiple documents which will be concatenated for you. For this you must pass an instance
of MultiPDFDocuments as first argument, which contains a file name and a list of PDFDocument
which each contain a template path and Options.

Each PDFDocument can have its extra set of rendered parameters, in addition to those provided by the
renderPDF() call.

Passing options

If one of your parameters to renderPDF() is an instance of PDF.Options you can specify the following options:

OptionTypeDescriptionDefault
FOOTERStringFooter contentNone
FOOTER_TEMPLATEStringPath to a template used to produce the footer contentNone
HEADERStringHeader contentNone
HEADER_TEMPLATEStringPath to a template used to produce the header contentNone
filenameStringThe name of the PDF file to producetemplateName + ".pdf"
pageSizeIHtmlToPdfTransformer.PageSizeThe page size and marginsIHtmlToPdfTransformer.A4P

If one of your parameters to renderPDF() is a constant String (not passed by a local variable) that represents
the path to the template to render instead of one deduced from the current action name.

Note that the IHtmlToPdfTransformer.A4 sizes are wrong, so you should probably make your own sizes to correct them.

Note that if you use templates for headers and footers you have to include the CSS inline rules in the templates, since
the ones from the main document template will not be applied.

About CSS

You can define CSS in your pages as follows:

Import:

<link media="print" type="text/css" rel="stylesheet" href="/public/stylesheets/foo.css"/>

Or inline:

<style media="print"><!-- foo { bar: gee; } --> </style>

See the CSS specs for options relative to page sizes, margins and breaks.

Note that there’s a special rule inserted by the PDF renderer that sets the page margins to those specified in the page size
option. If you wish to override it you can use the !important flag on every CSS margin or padding value.

Paging tables

If you want your table headers to follow on page breaks, use the thead element for your header rows and the following
special CSS attribute on your table:

-fs-table-paginate: paginate;