HTML-to-PDF with Ruby


Code Examples

DocRaptor Ruby Native Client Library

This is a Ruby gem for using DocRaptor API to convert HTML to PDF and XLSX.


Add the following to your Gemfile.

gem "docraptor"

Then run:

bundle install


See examples for runnable examples with file output, error handling, etc.

DocRaptor.configure do |dr|
  dr.username = "YOUR_API_KEY_HERE"
  # dr.debugging = true

$docraptor =

response = $docraptor.create_doc(
  test:             true,                                         # test documents are free but watermarked
  document_content: "<html><body>Hello World</body></html>",      # supply content directly
  # document_url:   "", # or use a url
  name:             "docraptor-ruby.pdf",                         # help you find a document later
  document_type:    "pdf",                                        # pdf or xls or xlsx
  # javascript:       true,                                       # enable JavaScript processing
  # prince_options: {
  #   media: "screen",                                            # use screen styles instead of print styles
  #   baseurl: "",                                # pretend URL when using document_content
  # },

Docs created like this are limited to 60 seconds to render, check out the async example which allows 10 minutes.

We have guides for doing some of the common things:

Converting HTML into PDF and XLS documents with Ruby is fast and painless with DocRaptor. Just POST to our Docs API with your HTML content and some required parameters, either as a form serialized string or a JSON object.

Below you’ll find complete examples for Ruby, but our API Reference Guide has all the details about the complete workings of the Docs API.


With DocRaptor, your PDF content is HTML and its style is CSS - things you already know! Even still, it can be difficult to get your PDF to look just right. Check out our Style and Formatting Documentation for everything related getting a perfectly styled PDF!