Play 2.3.x PDF : Basic example for generating PDF documents dynamically from Play Application with the help of play2-pdf module


alt tag

In this blog I have created a basic example for generating PDF documents dynamically from Play Application with the help of play2-pdf module

Play 2.3.x PDF module: This module helps generating PDF documents dynamically from your Play! web application. It simply renders your HTML- and CSS-based templates to PDF. It is based on the Flying Saucer library, which in turn uses iText for PDF generation.

Usage

You can use a standard Play! Scala template like this one:

@(message: String)

@main("Play!ng PDF") {
    Image: <img src="/public/images/favicon.png"/><br/>
    Hello world! <br/>
    @message <br/>
}

Check the below template list for more complex template design:

  1. Letter PDF
  2. News Letter PDF

Then this template, after having imported it.innove.PdfGenerator, can simply be rendered as:

    def letter: Action[AnyContent] = Action { implicit request =>
        Ok(PdfGenerator.toBytes(views.html.letter("Play!ng PDF"), host)).as("application/pdf")
    }

Template rules

Templates must generate XHTML.

If the template is using an image, stylesheet, etc., it usually is loaded via an http call. The PDF modules tries to optimize that resource loading: If you specify the URI as a path into the classpath of your Play! app, the resource is loaded directly instead. See the above sample template for an example.

Of course you can link to CSS files in your class path also, but be aware not to use the media="screen"qualifier.

Fonts you use must be explicitely packaged with your app.

<html>
    <head>
        <style type="text/css"><!--
        body {
            ...
            font-family: FreeSans;
        }
        -->style> 
    head>
    <body>
        ...
    body>
html>

Since the FreeSans font is not available to the java VM, you are required to add the corresponding font file, “FreeSans.ttf” to your Play! app. You can add your fonts with PdfGenerator.loadLocalFonts method, for example if you put the font in the folder /conf/fonts you load the font with:

PdfGenerator.loadLocalFonts(new String[]{"fonts/FreeSans.ttf"})

alt tag

alt tag

  • Used Play2-PDF Module to generate PDF from HTML template
  • Embedded JS & CSS libraries with WebJars
  • Integrating with a CSS framework (Twitter Bootstrap 3.1.1)
  • Bootswatch-Cerulean with Twitter Bootstrap 3.1.1 to improve the look and feel of the application

Instructions


  • To run the Play Framework, you need JDK 6 or later
  • Install Typesafe Activator if you do not have it already. You can get it from here
  • Execute ./activator clean compile to build the product
  • Execute ./activator run to execute the product
  • playing-pdf-module should now be accessible at localhost:9000

References


About Anand Kumar Singh

Hi, I’m Anand Singh and I specialize in Computer Software. I’m passionate about what I do, and I love to help people. Nothing is more fulfilling than being part of a team with similar interests, and an organization that values its employees. I'm Lead Software Engineer at Sapient. Having 5+ years of experience in Java, Grails/Groovy and Scala language. Anand also knows about Javascript, jQuery, CSS and other UI design technologies. Certified developer in: * Principles of Reactive Programming – Coursera * Functional Programming Principles in Scala – Coursera * Functional Program Design in Scala – Coursera * Introduction to Big Data with Apache Spark – edX * Oracle Certified Associate (OCA) * Sun Certified Java Programmer (SCJP) I'm passionate to work with upcoming technologies and bleeding platforms providing the new way to programming & development. He is actively involved in development & maintenance of several software projects. He has been enjoying Scala for more than a year now. Anand completed his MCA from IGNOU. As a Developer, I'm continuing my journey by actively collaborating with other Developers in my field. I'm actively contributing on the TechMonad & Playing Scala platform. I likes to spend his free time in Cooking, watching Discovery for new Technology and visiting new places with friends.
This entry was posted in Bootstrap, Bootswatch, CSS, Play Framework, Play2-PDF, Web, WebJars and tagged , , , , , , , , , , , . Bookmark the permalink.

6 Responses to Play 2.3.x PDF : Basic example for generating PDF documents dynamically from Play Application with the help of play2-pdf module

  1. rajaa says:

    Please I have a question, how can we create multiple page in the same pdf document

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s