Fork me on GitHub

What is wkpdf

wkpdf is a command line tool for rendering HTML to PDF using WebKit and RubyCocoa on Mac OS X.

Although there are plenty of browsers available for Mac OS X, I could not find a command-line tool that allows for downloading a website and storing the rendered website as PDF. This was my motivation for creating wkpdf. The application uses Apple WebKit for rendering the HTML pages, thus the result should look similar to what you get when printing the webpage with Safari.

Since wkpdf is based on the state-of-the-art WebKit HTML rendering framework, it provides high-quality web standard compliant HTML rendering with support for advanced CSS2/CSS3 styling.

There are plenty of use cases for wkpdf. For example, you may use wkpdf for:

Installation

wkpdf is an open-source software project. Its source code is hosted at github.com. For easy installation wkpdf is available as a Ruby gem package from rubygems.org, which means that wkpdf can be easily installed on Mac OS X 10.5.x and later with the RubyGems package manager that comes pre-installed with Mac OS X.

Installing wkpdf requires a recent version of RubyGems. If you don't have installed RubyGems version 1.3.3 or later, you need to update to the latest version with:

sudo gem update --system

Now you are ready to proceed with the installation of wkpdf with the the following command:

sudo gem install wkpdf

After wkpdf has been installed once, you can always update to the latest version with:

gem update wkpdf

Usage

The following command shows the basic usage of wkpdf. The content of the apple.com website is rendered to a file apple.pdf in the current directory.

wkpdf --source http://www.apple.com --output apple.pdf

wkpdf supports many options that affect how a HTML page is rendered and allow for example to set the page size and orientation, select specific style-sheet media, ignore HTTP errors etc. You can get a brief decription of wkpdfs options with:

wkpdf --help

The following example renders the apple.com website to a PDF document in A5 format in landscape orientation:

wkpdf --source http://www.webkit.org --paper A5 --orientation landscape \
      --output webkit.pdf

Many websites provide specific CSS stylesheets for printing, which suppress superfluous graphical elements in order to generate tidy printouts. If you want to render a website with all the graphical icing to exactly match the view in a browser, you can explicitly force the use of the CSS screen stylesheet and enable the printing of background images as in the following example:

wkpdf --source http://www.slashdot.org --stylesheet-media screen \
      --print-background yes --output sd.pdf

The following example suppresses pagination that is, the whole Slashdot front page is rendered as a single PDF page that extends as needed to fit the whole content.

wkpdf --source http://www.slashdot.org --stylesheet-media screen \
      --print-background yes --paginate no --output sd.pdf

Developers

If you are a developer and you would like to extend wkpdf you can browse and clone wkpdf's git repository at: http://github.com/plessl/wkpdf.

Recipes and FAQ

A list of useful recipes and frequently asked questions can be found in the wkpdf Wiki.

Questions and Support

For questions regarding wkpdf contact: wkpdf@plesslweb.ch

Please use the issue tracker at github for reporting bugs.

License

wkpdf is available under the terms of the MIT license:

Copyright (c) 2007-12 Christian Plessl

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.