language guide

language guide

Ruby on rails

Ruby on rails

Best Ruby on Rails Gems for PDF Generation in 2025

Marcelo Abreu, founder of pdforge

Marcelo | Founder of pdforge

Marcelo | Founder of pdforge

Feb 12, 2025

Feb 12, 2025

Overview: Top PDF gems for Ruby on Rails in 2025

The Rails ecosystem provides an abundance of powerful gems for generating PDF documents.

We’ll explore the best gems for pdf generation, comparing how they convert html to pdf or use direct rendering methods. This article delves into three main categories: browser-based gems for pixel-perfect output, non-browser-based gems for deeper control over the PDF structure, and third-party APIs that offload rendering to external services.

Three Main Categories

Browser-based solutions (Puppeteer-Ruby, Grover, WickedPDF) leverage headless browsers or rendering engines for full CSS and JavaScript support.

Non-browser-based libraries (Prawn, PDFKit, HexaPDF) use direct drawing or partial HTML rendering for efficient document creation.

Third-party APIs (pdforge) handle the entire generation pipeline externally.

Below is an invoice template that every gem will use to generate the same PDF, allowing you to compare their output firsthand and see the differences in action.

<!DOCTYPE html>
<html>
<head>
  <title>Sample Invoice with Table</title>
  <style>
    body { font-family: Arial, sans-serif; }
    h1 { color: #4A4A4A; }
    table { width: 100%; border-collapse: collapse; margin-top: 10px; }
    th, td { border: 1px solid #ccc; padding: 8px; text-align: left; }
    .total-row { font-weight: bold; }
  </style>
</head>
<body>
  <h1>Order #12345</h1>
  <p><strong>Customer:</strong> Jane Doe</p>
  <table>
    <thead>
      <tr>
        <th>Product</th>
        <th>Qty</th>
        <th>Price</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Rails Hoodie</td>
        <td>1</td>
        <td>$29.99</td>
      </tr>
      <tr class="total-row">
        <td colspan="2">Total</td>
        <td>$29.99</td>
      </tr>
    </tbody>
  </table>
</body>
</html>


Best pdf libraries ruby on rails 2025
Best pdf libraries ruby on rails 2025

Browser-Based Libraries: Convert HTML to PDF in Ruby on Rails

Browser-based gems transform HTML into PDFs by running a headless browser instance. They’re perfect for advanced JavaScript rendering, CSS transitions, or brand-heavy designs.

Generating PDF with Puppeteer-Ruby

Puppeteer-Ruby wraps the official Puppeteer library, offering precision rendering and comprehensive JavaScript support.

homepage for puppeter-ruby

Installation & Basic Setup

Add puppeteer-ruby to your Gemfile and run bundle install.

gem 'puppeteer-ruby'
bundle install

In a controller, you can incorporate Puppeteer like this:

# invoices_controller.rb
require 'puppeteer'

def generate_invoice
  Puppeteer.launch(headless: true) do |browser|
    page = browser.new_page
    page.goto("file://#{Rails.root.join('app/views/invoices/sample_invoice.html.erb')}")
    page.pdf(path: Rails.root.join('public/invoice_puppeteer.pdf').to_s, format: 'A4')
  end
  render plain: "Invoice generated with Puppeteer-Ruby!"
end

Enjoy full-page rendering, JavaScript execution for dynamic UI components, and robust CSS styling.

Ready to explore more? Unlock advanced Puppeteer usage in our Essential Puppeteer-Ruby Guide.

Generating PDF with Grover

Grover is a Rails-friendly approach to converting html to pdf using headless Chrome, often boasting simpler configuration than Puppeteer.

homepage for Grover

Installation & Configuration

Include grover in your Gemfile:

gem 'grover'
bundle install

Then configure an initializer if needed:

# config/initializers/grover.rb
Grover.configure do |config|
  config.options = {
    format: 'A4'
  }
end

Sample Code for Conversion

def generate_grover_invoice
  html_path = Rails.root.join('app/views/invoices/sample_invoice.html.erb')
  html_content = File.read(html_path)
  pdf = Grover.new(html_content).to_pdf
  File.open(Rails.root.join('public/invoice_grover.pdf'), 'wb') { |f| f.write(pdf) }
  render plain: "Invoice generated with Grover!"
end

Grover performs well under load, integrates smoothly with Rails views, and has fewer dependencies.

Want to dive deeper? Check out our Grover Integration Tips to learn more.

Generating PDF with WickedPDF

WickedPDF uses wkhtmltopdf behind the scenes, making it a classic favorite in Rails for quick and easy PDF generation.

homepage for WickedPDF

Setup & Installation

Add to Gemfile then run bundle install

gem 'wicked_pdf'
gem 'wkhtmltopdf-binary'
bundle install

Create an initializer:

# config/initializers/wicked_pdf.rb
WickedPdf.config = {
  exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf'

Rendering a Rails View

def invoice_as_pdf
  render pdf: "sample_invoice",
         template: "invoices/sample_invoice.html.erb",
         layout: "pdf_layout"
end

WickedPDF excels at generating straightforward documents but can falter with heavy JavaScript usage.

Looking for more insights? Access our Complete WickedPDF Guide now.

Non-Browser-Based Libraries: Direct PDF Generation with Ruby’s Canvas API

Non-browser-based gems create PDFs via low-level drawing commands or partial rendering engines. They are typically faster, lighter on dependencies, and well suited for standardized or data-heavy reports.

Generating PDF with Prawn

Prawn delivers a powerful yet minimalist API for custom layouts. It foregoes HTML rendering in favor of direct PDF commands.

homepage for Prawn

Installation & Setup

Add to Gemfile then run bundle install

gem 'prawn'
bundle install

Creating the invoice:

# prawn_invoices_controller.rb
require 'prawn'

def prawn_invoice
  Prawn::Document.generate("prawn_invoice.pdf") do |pdf|
    pdf.text "Order #12345", size: 20, style: :bold
    pdf.move_down 10
    pdf.text "Customer: Jane Doe"

    pdf.move_down 20
    data = [["Product", "Qty", "Price"],
            ["Rails Hoodie", "1", "$29.99"],
            ["Total", "", "$29.99"]]

    pdf.table(data, header: true)
  end
  render plain: "Invoice generated with Prawn!"
end

Prawn is both fast and highly configurable, making it a top choice among developers seeking direct Canvas API control.

Curious for more details? Explore the Ultimate Prawn PDF Guide.

Generating PDF with PDFKit

PDFKit simplifies HTML-to-PDF creation but doesn’t rely on a full browser environment. It uses wkhtmltopdf—similar to WickedPDF—yet keeps the interface minimal.

homepage for PDFKit Ruby

Setup in Rails

Add to Gemfile then run bundle install

gem 'pdfkit'
gem 'wkhtmltopdf-binary'
bundle install

Create an initializer:

# config/initializers/pdfkit.rb
PDFKit.configure do |config|
  config.wkhtmltopdf = Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
end

HTML Invoices Conversion

def pdfkit_invoice
  html_path = Rails.root.join('app/views/invoices/sample_invoice.html.erb')
  html_content = File.read(html_path)
  kit = PDFKit.new(html_content, page_size: 'A4')
  pdf = kit.to_pdf
  File.open(Rails.root.join('public/pdfkit_invoice.pdf'), 'wb') { |f| f.write(pdf) }
  render plain: "Invoice generated with PDFKit!"
end

PDFKit often outperforms heavier browser-based solutions but provides limited JS support.

For advanced features, check out our Deep Dive into PDFKit.

Generating PDF with HexaPDF

HexaPDF offers a modular design, letting you manipulate individual PDF objects, merge existing files, or optimize PDF sizes.

homepage for HexaPDF

Installation & Example Usage

Add to Gemfile then run bundle install

gem 'hexapdf'
bundle install

Generating the invoice:

require 'hexapdf'

def hexapdf_invoice
  doc = HexaPDF::Document.new
  page = doc.pages.add
  canvas = page.canvas
  canvas.font("Helvetica", size: 14)
  canvas.text("Order #12345", at: [50, 700])
  canvas.text("Customer: Jane Doe", at: [50, 680])

  # If you wish to draw table lines or more structured data:
  canvas.text("Product: Rails Hoodie", at: [50, 660])
  canvas.text("Price: $29.99", at: [50, 640])

  doc.write("hexapdf_invoice.pdf")
  render plain: "Invoice generated with HexaPDF!"
end

With efficient memory usage and built-in optimization, it’s ideal for large-scale or advanced PDF workflows.

Want to learn the finer points? Browse our HexaPDF Advanced Techniques section.

Third-Party PDF Generation APIs: Scalable and Flexible Solutions

Offloading pdf generation to an external service helps large businesses and SaaS platforms handle spiky traffic without tying up server resources.

Generating PDF with PDForge

homepage of pdforge

pdforge is a third-party pdf generation API. You can create beautiful reports with flexible layouts and complex components with an easy-to-use opinionated no-code builder. Let the AI do the heavy lifting by generating your templates, creating custom components or even filling all the variables for you.

You can handle high-volume PDF generation from a single backend call.

Here’s an example of how to generate pdf with pdforge via an API call:

require 'net/http'
require 'json'
require 'uri'

class PdfApiService
  def self.generate_pdf(html_content)
    uri = URI("https://api.pdforge.com/v1/pdf/sync")
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = true
    request = Net::HTTP::Post.new(uri.path, {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer your-api-key"
    })
    request.body = {
      templateId: 'your-template',
      data: { html: html_content }
    }.to_json
    response = http.request(request)
    response.body if response.is_a?(Net::HTTPSuccess)
  end
end

You can create your account, experience our no-code builder and create your first layout template without any upfront payment clicking here.

Comparison of the Best PDF Libraries for Ruby on Rails

Below is a comparison among these best libraries. Determine which meets your needs based on ease of use, performance, feature set, and scalability.



Conclusion

Choosing from the best pdf gem for you depends on both functionality requirements and personal workflow preferences. For streamlined, structured invoices, Prawn remains an excellent non-browser-based choice thanks to its minimal dependencies. If you’re focusing on a browser-based approach with quick setup, WickedPDF is a classic choice for producing well-styled PDFs in a familiar Rails environment.

Meanwhile, choose third-party pdf generation APIs, like pdforge, if you don't want to waste time maintaining pdfs layouts and their infrastructure or if you don't want to keep track of best practices to generate PDFs at scale.

Generating pdfs at scale can be quite complicated!

Generating pdfs at scale can be quite complicated!

We take care of all of this, so you focus on what trully matters on your Product!

We take care of all of this, so you focus on what trully matters on your Product!

Try for free

7-day free trial

Table of contents

Title