Giter Club home page Giter Club logo

play2-scala-pdf's Introduction

License Master Build Status Maven Central

Play [2.4 to 2.8] PDF module

play2-scala-pdf is a Play Framework module to help generate PDF documents dynamically from Play Framework web application.

It simply renders Play Framework HTML and CSS-based view templates to PDF via Flying Saucer library, which uses OpenPDF, an open-source LGPL and MPL version of an older fork of iText for PDF generation.

Supported Play Framework and Scala Versions

The supported Scala and Play versions as follows:

Scala 2.10 Scala 2.11 Scala 2.12 Scala 2.13
Play 2.4 YES YES
Play 2.5 YES
Play 2.6 YES YES
Play 2.7 YES YES YES
Play 2.8 YES YES

Play Framework Java

If you are using Play Framework Java, check out https://github.com/innoveit/play2-pdf.

play2-scala-pdf is a fork of the project above with the aim to reduce the final distribution size for Play Framework Scala projects by rebasing the module to Play Framework Scala core (i.e. avoid including Play Framework Java additions in Play Framework Scala projects).

Installation

Create a PDF generator factory method in your application's Guice module:

@Provides
def providePdfGenerator(): PdfGenerator = {
  val pdfGen = new PdfGenerator()
  pdfGen.loadLocalFonts(Seq("fonts/opensans-regular.ttf"))
  pdfGen
}

Currently, the module is hosted at Maven Central Repository. Include the following lines in build.sbt, then reload SBT to resolve and enable the module:

libraryDependencies ++= Seq(
  ...
  "com.hhandoko" %% "play28-scala-pdf" % "4.3.0" // Use `play27-scala-pdf` for Play 2.7.x apps, etc.
)

Remember to add Sonatype snapshots repository to use snapshot releases:

resolvers ++= Seq(
  Resolver.sonatypeRepo("snapshots")
)

NOTE: If you are using an IDE like Eclipse, remember to re-generate your project files.

Usage

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

@(message: String)

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

Then this template, after having imported com.hhandoko.play.pdf.PdfGenerator, can simply be rendered as:

class HomeController @Inject() (pdfGen: PdfGenerator) extends Controller {

    def examplePdf = Action { pdfGen.ok(views.html.example(), "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

Please observe the following constraints to avoid issues when using this module:

  • Avoid using media="screen" qualifier on linked CSS in <head>, otherwise you must provide specific print stylesheets (i.e. media="print")
  • Non-system fonts must be loaded explicitly (see below for example)
  • External assets such as images need to be loaded as base64-encoded string
  • External such as stylesheets will be loaded as per normal HTML page load

NOTE: If the specified URI is a path into Play Framework app classpath, the resource is loaded directly instead. See the above sample template for an example.

Fonts can be loaded by invoking PdfGenerator.loadLocalFonts method. For example:

  • if fonts are stored under /conf/fonts (or other project path mapped to the classpath),
  • it can be loaded by invoking pdfGenerator.loadLocalFonts(Seq("fonts/FreeSans.ttf"))

NOTE: Non-system fonts in this context refers to WebFonts, fonts not available to the Java VM, or other fonts not included as part of normal OS distribution

Contributing

We follow the "fork-and-pull" Git workflow.

  1. Fork the repo on GitHub
  2. Commit changes to a branch in your fork (use snake_case convention):
    • For technical chores, use chore/ prefix followed by the short description, e.g. chore/do_this_chore
    • For new features, use feature/ prefix followed by the feature name, e.g. feature/feature_name
    • For bug fixes, use bug/ prefix followed by the short description, e.g. bug/fix_this_bug
  3. Rebase or merge from "upstream"
  4. Submit a PR "upstream" with your changes

Please read CONTRIBUTING for more details.

License

The MIT License (MIT)

Original   - Copyright (c) 2014 Jöerg Viola, Marco Sinigaglia
Derivative - Copyright (c) 2016 - 2020 play2-scala-pdf Contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

play2-scala-pdf is released under the MIT license. See the LICENSE file for further details.

Open Sans font by Steve Matteson is released under the Apache 2 license. See the Open Sans Google Fonts page for further details.

Play Framework logo is a trademark of Lightbend.

Releases

https://github.com/hhandoko/play2-scala-pdf/releases

play2-scala-pdf's People

Contributors

fhars avatar gertv avatar hhandoko avatar joergviola avatar marcosinigaglia avatar mgosk avatar raisercostin avatar wolfert avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

play2-scala-pdf's Issues

Support for PDF encryption

It would be nice to add support for PDF encryption (which the underlying rendering library already implements).
I imagine this would be exposed via optional parameters to the toBytes and toStream generator methods.

Bundled `routes` file overrides root / parent project routes

Expected Behaviour

Application pages should be able to be accessed normally after importing the PDF generation library.


## Actual Behaviour

Play framework shows Action not found info page instead of the desired application page.


## Steps to Reproduce 1. Import the library and use it in an existing project (or new project with defined routes) 2. Try to access the existing project route (e.g. `/` or `/login`) 3. Observe `Action not found` info page will be displayed.

Scala 2.13 release

Play 2.7.3 is compiled against Scala 2.13, and scalatestplus-play is also available for 2.13, so I think it should be possible to release play2-scala-pdf as well.

Thanks!

Replace dependency on itextpdf with openpdf

Outcome Desired

Remove dependency on itextpdf, as its licensing requirements are restrictive. Replace with dependencies to openpdf.

https://github.com/LibrePDF/OpenPDF


In build.sbt
libraryDependencies += "org.xhtmlrenderer" % "flying-saucer-pdf" % "9.1.6"
libraryDependencies += "org.xhtmlrenderer" % "flying-saucer-core" % "9.1.6"
libraryDependencies += "com.github.librepdf" % "openpdf" % "1.0.4"

Definition of Done

  • All tests pass
  • PDF examples are generated as before
  • itextpdf-*.jar is no longer required

How to Demo

Confirm all test cases complete successfully.

Vertical text

Outcome Desired

Write vertical text in @right-middle.

I tried

@@right-middle {
  content: "test;
  writing-mode: vertical-rl;
}

But it doesn't work. Any suggestion?

Is it possible to get rid of the margin on the generated PDF?

Hi,

I'm trying to generate a PDF using this HTML:

<html>
<head>
<style>
  body {background-color: grey; margin: 0 }
</style>
</head>
<body>
 Howdy!
 <br/><br/>
 You may not know it yet, but this is best PDF in the world!
 <br/><br/>
 Keep it green!
</body>
</html>

and feeding it into the PdfGenerator.toBytes method. However, the PDF that gets generate always has a white margin around the body.

For this specific toy example, I wanted the whole page to have a grey background. Is this possible? Maybe I'm missing any specific CSS (definitely not my strongest asset).

Thanks!

flyingsaucer update 9.1.18

Hi, i would like to use the library (with Play 2.6 and scala 2.11) for creating PDFs. Unfortunately there is still a bug with box sizing which should be fixed with the flyingsaucer v9.1.18 release.
Can you update the version?

Chore - Update Play Framework 2.4 and 2.5 to latest

Context

Play Framework 2.5 branch's latest version is 2.5.15 and 2.4 branch is 2.4.11. The module and examples are several patch versions behind.


Definition of Done

The module and examples are updated to the latest Play Framework version.

Scala 2.12 version?

Hi!

Do you have any plans for releasing the version for Scala 2.12? It seems that the library already works fine with Play 2.6, but a proper release that supports new Scala would be great.

Thanks!

Arabic characters not converted to PDF

Expected Behaviour

Aabic Charaters are not converted to pdf format


Actual Behaviour

Arabic text is left blank on pdf file, while it's correctly displayed on html format:
image


Steps to Reproduce

1- set an exemle.scala.html with arabic characters
2- Load an arabic font in ApplicationModule.scala using pdfGen.loadLocalFonts(Seq("fonts/TAHOMA_0.TTF"))
-->

Failure to build: Could not find a suitable constructor in com.hhandoko.play.pdf.PdfGenerator.

Expected Behaviour

A simple project should build.


Actual Behaviour

Could not find a suitable constructor in com.hhandoko.play.pdf.PdfGenerator. 

Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument 
constructor that is not private.
 at com.hhandoko.play.pdf.PdfGenerator.class(PdfGenerator.scala:53)

Steps to Reproduce

build.sbt

 name := "testpdf"
 version := "1.0" 
 lazy val `testpdf` = (project in file(".")).enablePlugins(PlayScala)
resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"
resolvers += "Akka Snapshot Repository" at "http://repo.akka.io/snapshots/"
scalaVersion := "2.12.2"
libraryDependencies ++= Seq( jdbc , ehcache , ws , specs2 % Test , guice,
"com.hhandoko" %% "play2-scala-pdf" % "3.0.0.P26"  )
unmanagedResourceDirectories in Test <+=  baseDirectory ( _ /"target/web/public/test" )  

HomeController.scala

package controllers

import com.hhandoko.play.pdf.PdfGenerator
import javax.inject._
import play.api.mvc._

/**
 * This controller creates an `Action` to handle HTTP requests to the
 * application's home page.
 */
@Singleton
class HomeController @Inject()(pdfGen:PdfGenerator,cc: ControllerComponents) extends 
AbstractController(cc) {

  /**
  * Create an Action to render an HTML page with a welcome message.
  * The configuration in the `routes` file means that this method
  * will be called when the application receives a `GET` request with
  * a path of `/`.
  */
  def index = Action {
   pdfGen.ok(views.html.index("Your new application is ready."),"http://localhost:9000")
 }

 }

Chore - Update `flying-saucer` version

Context

The latest flying-saucer library is available, which fixes issues with broken links to older bouncycastle dependencies.


Definition of Done

Update flying-saucer to version9.1.6 on both modules (Play 2.4 and 2.5).

Exception thrown when trying out the provided example

Context

Running you example, I would expect it to work, instead I experience the following exception:
Could not find a suitable constructor in com.builtamont.play.pdf.PdfGenerator. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private.
at com.builtamont.play.pdf.PdfGenerator.class(PdfGenerator.scala:53)
while locating com.builtamont.play.pdf.PdfGenerator

Also, this coed does not compile since your "example" template expect a message, and you call it without.


Definition of Done

Working code, or updated documentation that explain the use of inject into HomeController

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.