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:
- full support of
wkhtmltopdf
extended parameters (see the source of thePdfConfig
trait) - can read HTML from several sources:
java.io.File
,java.io.InputStream
,java.net.URL
,scala.xml.Elem
, andString
- can write PDFs to
File
andOutputStream
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.
Add the following to your sbt build for Scala 2.10, 2.11 and 2.12:
libraryDependencies += "io.github.simplifier-ag" %% "spdf" % "1.4.1"
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 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"))
Visit the wkhtmltopdf downloads page and install the appropriate package for your platform.
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)
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).
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")) }
- Fork the project.
- Make your feature addition or bug fix.
- Add tests for it. This is important so I don't break it in a future version unintentionally.
- Commit, do not mess with build settings, version, or history.
- Send me a pull request. Bonus points for topic branches.
release cross with-defaults
- check out released version
publishSigned
sonatypeRelease
- Full support for extended options
- Full support for input types
- Streaming API (with
scalaz-stream
) - Simplified API with implicits
- Integration with Play for streaming PDFs in HTTP responses
Copyright (c) 2013, 2014 Federico Feroldi. See LICENSE
for details.