cloudify / sPDF

Create PDFs from Scala using plain old HTML and CSS. Uses wkhtmltopdf on the back-end which renders HTML using Webkit.
Other
199 stars 53 forks source link

sPDF

sPDF ( pronounced speedy-f ) is a Scala library that makes it super easy to create complex PDFs from plain old HTML, CSS and Javascript.

On the backend it uses wkhtmltopdf which renders HTML using Webkit.

sPDF is heavily inspired by Ruby's PdfKit gem.

The main features of sPDF are:

The source HTML can reference to images and stylesheet files as long as the URLs point to the absolute path of the source file. It's also possible to embed javascript code in the pages, wkhtmltopdf will wait for the document ready event before generating the PDF.

Installation

Add the following to your sbt build for Scala 2.10, 2.11 and 2.12:

libraryDependencies += "io.github.cloudify" %% "spdf" % "1.4.0"

Add the following to your sbt build for Scala 2.9:

libraryDependencies += "io.github.cloudify" %% "spdf" % "1.3.1"

Usage

    import io.github.cloudify.scala.spdf._
    import java.io._
    import java.net._

    // Create a new Pdf converter with a custom configuration
    // run `wkhtmltopdf --extended-help` for a full list of options
    val pdf = Pdf(new PdfConfig {
      orientation := Landscape
      pageSize := "Letter"
      marginTop := "1in"
      marginBottom := "1in"
      marginLeft := "1in"
      marginRight := "1in"
    })

    val page = <html><body><h1>Hello World</h1></body></html>

    // Save the PDF generated from the above HTML into a Byte Array
    val outputStream = new ByteArrayOutputStream
    pdf.run(page, outputStream)

    // Save the PDF of Google's homepage into a file
    pdf.run(new URL("http://www.google.com"), new File("google.pdf"))

If you want to use sPDF in headless mode on debian you'll need to call to wkhtmltopdf through a virtualizer like xvfb-run. This is because wkhtmltopdf does not support running in headless mode on debian through the apt package. To use sPDF in this kind of environment you need to use WrappedPdf instead of Pdf. For Example:

    import io.github.cloudify.scala.spdf._
    import java.io._
    import java.net._

    // Create a new Pdf converter with a custom configuration
    // run `wkhtmltopdf --extended-help` for a full list of options
    val pdf = WrappedPdf(Seq("xvfb-run", "wkhtmltopdf"), new PdfConfig {
      orientation := Landscape
      pageSize := "Letter"
      marginTop := "1in"
      marginBottom := "1in"
      marginLeft := "1in"
      marginRight := "1in"
    })

    val page = <html><body><h1>Hello World</h1></body></html>

    // Save the PDF generated from the above HTML into a Byte Array
    val outputStream = new ByteArrayOutputStream
    pdf.run(page, outputStream)

    // Save the PDF of Google's homepage into a file
    pdf.run(new URL("http://www.google.com"), new File("google.pdf"))

Installing wkhtmltopdf

Visit the wkhtmltopdf downloads page and install the appropriate package for your platform.

Troubleshooting

NoExecutableException

Make sure wkhtmltopdf is installed and your JVM is running with the correct PATH environment variable.

If that doesn't work you can manually set the path to wkhtmltopdf when you create a new Pdf instance:


val pdf = Pdf("/opt/bin/wkhtmltopdf", PdfConfig.default)

Resources aren't included in the PDF

Images, CSS, or JavaScript does not seem to be downloading correctly in the PDF. This is due to the fact that wkhtmltopdf does not know where to find those files. Make sure you are using absolute paths (start with forward slash) to your resources. If you are using PDFKit to generate PDFs from a raw HTML source make sure you use complete paths (either file paths or urls including the domain).

Notes

Asynchronous conversion

sPDF relyies on Scala's scala.sys.process.Process class to execute wkhtmltopdf and pipe input/output data.

The execution of wkhtmltopdf and thus the conversion to PDF is blocking. If you need the processing to be asynchronous you can wrap the call inside a Future.


val pdf = Pdf(PdfConfig.default)

val result = Future { pdf.run(new URL("http://www.google.com"), new File("google.pdf")) }

Contributing

Release / Publish

Roadmap

Copyright

Copyright (c) 2013, 2014 Federico Feroldi. See LICENSE for details.