https://github.com/benjohnde/play-pdf
A PDF module for the Play framework
https://github.com/benjohnde/play-pdf
java pdf pdf-generation play-framework playframework
Last synced: 12 months ago
JSON representation
A PDF module for the Play framework
- Host: GitHub
- URL: https://github.com/benjohnde/play-pdf
- Owner: benjohnde
- License: mit
- Created: 2012-05-18T21:29:32.000Z (almost 14 years ago)
- Default Branch: master
- Last Pushed: 2018-01-26T14:51:38.000Z (about 8 years ago)
- Last Synced: 2025-04-06T19:39:52.605Z (about 1 year ago)
- Topics: java, pdf, pdf-generation, play-framework, playframework
- Language: Java
- Homepage:
- Size: 14.3 MB
- Stars: 109
- Watchers: 11
- Forks: 68
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# play-pdf
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 created an example application https://github.com/benjohnde/play-pdf/tree/master/example.
You can use a standard Play! scala template like this one:
```html
@(message: String)
@main("Welcome to Play 2.6.2") {
Image: 
Hello world!
@message
}
```
Then this template, after having imported `de.benjohn.play.pdf.PDF`, can simply be rendered as:
```java
public static Result document() {
return PDF.ok(document.render("Your new application is ready."));
}
```
where ```PDF.ok``` is a simple shorthand notation for:
```java
public static Result document() {
return 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
body { font-family: FreeSans; }
...
```
The module imports font resources as specified in your [application configuration](https://github.com/benjohnde/play-pdf/blob/master/example/conf/application.conf).
Specify fonts as follows:
```bash
pdf.fonts = ["Arial.ttf", "Helvetica.ttf"]
```
Each font file should be placed in your Play app configuration folder, so that they will be included.
See also the example project.
## Installation
Currently the module is not hosted anywhere. In order to use it, you need to publish it locally in the current play-maven-repository. Therefore:
```bash
git clone https://github.com/benjohnde/play-pdf.git
cd play-pdf/module
sbt publish-local
```
Then, add to your libraryDependencies in your `build.sbt`:
```scala
libraryDependencies ++= Seq(
"de.benjohn.play" %% "pdf" % "1.1.3"
)
```
After the next restart of the `sbt`, the module should be available.
If you are using an IDE like Eclipse, remember to re-generate your project files.
## Kudos
- [joergviola](https://github.com/joergviola) initial creator of this project