Using play-pdf

Bart's picture
Mon, 04/29/2013 - 13:45 -- Bart


This blog post should help you provide downloadable pdf's through a webapplication built with the Play! Framework. Jörg Viola provides a module for Play! that allows us to create the pdf from a HTML page.


In my setup I used Play! 2.0 and the module at Jörg's github. Note that a newer version for Play 2.1 is available. For us to be able to use the module we need to add it to our dependencies in

object ApplicationBuild extends Build {
    val appName         = "play-pdf"
    val appVersion      = "1.0"
    val appDependencies = Seq(
      "pdf" % "pdf_2.9.1" % "0.3",
    val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
      resolvers += Resolver.url("Violas Play Modules", url(""))(Resolver.ivyStylePatterns)

After that we only need to import util.pdf.PDF in the controller we wish to use to create the PDF.


The module allows us to create a pdf from from our Play! templates if those templates generate XHTML.

@(message: String)

Instead of rendering the template as text/html as we normally do we will render it as application/pdf using the module.

def generatePdf = Action {
  Ok(PDF.toBytes(views.html.index("My first pdf")).as("application/pdf")


Any link or button that is linked to our controller method generatePdf will ask the user to download our PDF. As you can see in this very simple example, it's very easy to create PDF's with dynamic content. Since you can pass all the parameters and styling you like to a Play! template and the module will generate a PDF from that template, nothing is stopping you from creating nice PDF's containing all the data from your applications's model and styling that document with your CSS.