Giter VIP home page Giter VIP logo

play20-pdf's Introduction

Play 2.0 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

I have provided a simple example application at https://github.com/joergviola/play20-pdf/samples/pdf-sample.

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

@(message: String)

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

Then this template, after having imported util.pdf.PDF, can simply be rendered as:

	public static Result document() {
		return PDF.ok(document.render("Your new application is ready."));
	}

where PDF.ok is a simple shorthand notation for:

	ok(PDF.toBytes(document.render("Your new application is ready."))).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. The module adds /conf/resources/fonts to the list of directories searched for font files.

Installation

Currently, the module is hosted at http://www.joergviola.de/releases/. Therefore, including the following lines in your Build.scala will resolve it:

val appDependencies = Seq(
  ...
      "de.joergviola" %% "play-pdf" % "0.6-SNAPSHOT"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = JAVA).settings(
  ...
  resolvers += Resolver.url("Violas Play Modules", url("http://www.joergviola.de/releases/"))(Resolver.ivyStylePatterns)
)

After the next restart of Play!, the module is available. If you are using an IDE like Eclipse, remember to re-generate your project files.

License

Released under the MIT license; see the file LICENSE.

Releases

0.5 11.06.2013 Fix with higher UTF-8 codes, documentBaseURL Thanks Wolfert de Kraker
0.4 08.02.2013 Play 2.1
0.4 04.02.2013 Play 2.1.RC4, remote images
0.3 15.06.2012 CSS handling
0.2 21.05.2012 Font handling
0.1 18.05.2012 Initial release

play20-pdf's People

Contributors

joergviola avatar fhars avatar raisercostin avatar gertv avatar wolfert avatar

Watchers

James Cloos avatar Neal Rigney avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.