Skip to content
Dan Bernier edited this page Nov 6, 2015 · 27 revisions

Welcome to PDFKit!

PDFKit is a ruby gem that allows you to generate PDFs using only HTML/CSS. It requires wkhtmltopdf.

On this page are instructions for getting started with PDFKit. If you run into trouble, you can find information in the README and on the troubleshooting page.

Installing the gem

gem install pdfkit

Install WKHTMLTOPDF

See Installing WKHTMLTOPDF

Getting Started

# PDFKit.new takes the HTML and any options for wkhtmltopdf
# run `wkhtmltopdf --extended-help` for a full list of options
kit = PDFKit.new(html, :page_size => 'Letter')
kit.stylesheets << '/path/to/css/file'

# Get an inline PDF
pdf = kit.to_pdf

# Save the PDF to a file
file = kit.to_file('/path/to/save/pdf')

# PDFKit.new can optionally accept a URL or a File.
# Stylesheets can not be added when source is provided as a URL of File.
kit = PDFKit.new('http://google.com')
kit = PDFKit.new(File.new('/path/to/html'))

# Add any kind of option through meta tags
PDFKit.new('<html><head><meta name="pdfkit-page_size" content="Letter"')
Using cookies in scraping

If you want to pass a cookie to cookie to pdfkit to scrape a website, you can pass it in a hash:

kit = PDFKit.new(url, cookie: {cookie_name, cookie_value})

Continue setting up: