DEV Community

Cover image for Quick Guide to PDF generation in Ruby
Alec Grey
Alec Grey

Posted on

Quick Guide to PDF generation in Ruby

I recently learned about some additional controller functionality that expanded beyond the simple render 'view' that I used in my first Rails project. Since my project had to do with generating lists for a user, I was understandably excited when I learned about the send_file method. The possibility of sending a user data via a PDF was always a stretch-goal, but I had no idea where to start.

Though the send_file method gave me a good starting point, I still had a huge issue: how do I create a custom PDF?

Luckily, with some google-magic, I stumbled on some great articles that helped me out, particularly this great video from Upcase. We're going to look specifically at creating custom PDFs, but I'd highly suggest checking out the video for a more in-depth look at adding this to your own Rails project

Where We're Going

We're going to take a look at the process for setting up creating a custom PDF in Ruby, including:

  • The tools/gem dependencies needed
  • How to use the tools to generate a simple test PDF
  • Customizing the layout/data of your PDF

Tools / Gems

To get started, we are going to need to install wkhtmltopdf. In your terminal, use brew to complete the install process:

$ brew install --cask wkhtmltopdf
Enter fullscreen mode Exit fullscreen mode

wkhtmltopdf is a straightforward tool that works by taking HTML and CSS, and converting it into a PDF document.

Unfortunately, wkhtmltopdf cannot be used directly in our Ruby application. This is where we will need the PDFKit gem. Either install globally, or add it to your gemfile in a rails application:

$ gem install pdfkit
Enter fullscreen mode Exit fullscreen mode

Generating a simple test PDF

PDFKit allows us access to very simply take a string of HTML code, and convert it into a PDF document, that we can download to a file location.

require 'pdfkit'

def generate_pdf_from_html(html, route)
    kit = PDFKit.new(html)
    kit.to_file(route)
end
Enter fullscreen mode Exit fullscreen mode

With our basic PDF converter method defined, let's give it some data and see what happens!

html = '<h1>Hello, Globe!</h1>'
route = './hello.pdf'

generate_pdf_from_html(html, route)
Enter fullscreen mode Exit fullscreen mode

Running this application, I now should have a new pdf document in the current directory with the name hello.pdf:

Alt Text

Spruce things up with CSS!

The great thing about this process is that wkhtmltopdf and PDFKit can take any viable HTML and convert it to PDF format. Let's take advantage of that by adding <style> to the head of our HTML!

html = "
    <head>
        <style>
            body {
                text-align: center;
                background-color: gray;
            }
            h1 {
                color: white;
            }
        </style>
    </head>
    <body>
        <h1>Doggos are cool</h1>
        <p>I like doggos, they are very cool.</p>
    </body
    "
Enter fullscreen mode Exit fullscreen mode

Now if we run our application again, we will get a stylized PDF as the result!

Alt Text

Keep in mind, these all default to an 8.5 x 11 document, which is convenient for if we want to make anything printable!

I hope this helps add a nifty tool to your belt. Again, I would highly suggest watching this video from Upcase, which gives an in-depth look at integrating this functionality into a Rails application.

Cheers.

Top comments (0)