Giter Club home page Giter Club logo

spdf's Introduction

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:

  • full support of wkhtmltopdf extended parameters (see the source of the PdfConfig trait)
  • can read HTML from several sources: java.io.File, java.io.InputStream, java.net.URL, scala.xml.Elem, and String
  • can write PDFs to File and OutputStream

Th 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.9.1, Scala 2.9.2, Scala 2.9.3, and Scala 2.10.x!):

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

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"))

Installing wkhtmltopdf

Visit the wkhtmltopdf homepage.

Linux

Download the static binary for your architecture at the wkhtmltopdf downloads list, extract the file to a directory that is in your PATH, such as /opt or /usr/local/bin and run from there.

Debian / Ubuntu

	apt-get install wkhtmltopdf

Windows

Download the installer from the wkhtmltopdf downloads list and tell Pdf where it is.

Mac

Download the disk image from wkhtmltopdf downloads list and copy the file to a directory in your PATH.

Otherwise, if you have homebrew, run:

brew install wkhtmltopdf

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

  • 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.

Roadmap

  • 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

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

spdf's People

Contributors

cloudify avatar

Watchers

Marcel Krcah avatar  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.