Coder Social home page Coder Social logo

play2-pdf's Introduction

Play 2.7.x PDF module

Sonatype maven

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. This project is a fork of https://github.com/benjohnde/play20-pdf .

Usage

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

@(message: String)

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

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

	@Inject
	public PdfGenerator pdfGenerator;

	public Result document() {
		return pdfGenerator.ok(document.render("Your new application is ready."), "http://localhost:9000");
	}

where pdfGenerator.ok is a simple shorthand notation for:

	ok(pdfGenerator.toBytes(document.render("Your new application is ready."), "http://localhost:9000")).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"})

Installation

Currently, the module is hosted at Maven Central Repository. Therefore, including the following lines in your build.sbt will resolve it:

libraryDependencies ++= Seq(
  ...
      "it.innove" % "play2-pdf" % "x.x.x"
)

where:

Play version play2-pdf vesion
2.7 1.9.x
2.6 1.8.x

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.

Configuration

If you need additional configuration, then modify your ApplicationModule accordingly:

public class ApplicationModule extends AbstractModule {
  @Override
  protected void configure() {
    bind(PdfGenerator.class).toProvider(PdfGeneratorProvider.class).asEagerSingleton();
  }
  
  static class PdfGeneratorProvider implements Provider<PdfGenerator> {
    private final PdfGenerator pdfGenerator;

    @Inject
    public PdfGeneratorProvider(final Environment environment) {
      this.pdfGenerator = new PdfGenerator(environment);
      this.pdfGenerator.loadLocalFonts(asList("fonts/opensans-regular.ttf"));
    }

    @Override
    public PdfGenerator get() {
      return pdfGenerator;
    }
  }
}

License

Released under the MIT license; see the file LICENSE.

Releases

https://github.com/innoveit/play2-pdf/releases

play2-pdf's People

Contributors

marcosinigaglia avatar joergviola avatar fhars avatar eximius313 avatar rvangsgaard avatar gpgekko avatar raisercostin avatar gertv avatar mgosk avatar wolfert 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.