Free Ruby on Rails Prawn PDF Templates & DSL Generator

Pure-Ruby Prawn::Document vector classes for Rails and Sidekiq — zero wkhtmltopdf or Chromium binaries. Produced the #1 smallest PDF of all 22 libraries in our 22-page stress test (79.0 KB in 537 ms & 34.0 MB RAM; 2.3 KB in 7.5 ms single-page).

Install: gem install prawn -v 2.5.0 | Runtime: Ruby (Ruby 3) | Version: 2.5.0 | Popularity: 1.65M/mo installs

Works with: Ruby on Rails, Sinatra, Sidekiq, Hanami

How Prawn (Ruby / Rails) Templates Work in CodePDF

Translates CodePDF's visual canvas into idiomatic Ruby Prawn::Document DSL calls (text_box, fill_rounded_rectangle, stroke_line) flipping top-left points to Prawn's bottom-left origin.

  • Pure Ruby gem with zero wkhtmltopdf or Chromium binary headaches on Heroku, Render, or Kamal
  • Automatically flips Y-coordinates and computes text_box bounds and dynamic Array#each row shifts
  • Ready to call from Rails controllers with send_data pdf.render, type: 'application/pdf'

Quick Start: Generate PDFs with Prawn

Install Prawn (Ruby / Rails) (Ruby 3), then run the code CodePDF exports for any template:

gem install prawn -v 2.5.0

  1. Run gem install prawn -v 2.5.0 (or add gem 'prawn' to your Gemfile and bundle).
  2. Save the generated file as document.rb.
  3. Run ruby document.rb to produce document.pdf.
  4. prawn uses a bottom-left origin in points; the generated code flips the canvas layout automatically.
  5. Register custom TTF fonts with prawn's font families before rendering non-standard typefaces.

Example Prawn Output (Standard B2B Commercial Invoice)

Compiled directly from the Standard B2B Commercial Invoice template (Ruby 3):

require "prawn"

# This code is generated using codepdf.dev

def wrap_text_lines(pdf, text, max_width, size)
    str = text.to_s.gsub("\r\n", "\n").gsub("\r", "\n")
    return [str] if max_width <= 0
    out = []
    str.split("\n", -1).each do |para|
        if para.empty?
            out << ""
            next
        end
        if pdf.width_of(para, size: size) <= max_width
            out << para
            next
        end
        lead = para[/\A[ \t]*/] || ""
        words = para.strip.split(/\s+/)
        if words.empty?
            out << ""
            next
        end
        words[0] = lead + words[0] unless lead.empty?
        cur = ""
        words.each do |w|
            if pdf.width_of(w, size: size) > max_width
                unless cur.empty?
                    out << cur
                    cur = ""
// … (564 more lines in full generated file)

Custom Fonts in Prawn

Register TTF weights inside pdf.font_families.update(...) before calling pdf.font.

  1. Store .ttf files under ./fonts/ accessible to Ruby.
  2. Call pdf.font_families.update('{{FONT_FAMILY}}' => { normal: ..., bold: ... }).
  3. Set the active family with pdf.font '{{FONT_FAMILY}}'.

Official documentation: Prawn docs

Prawn Benchmark Results

Measured on CodePDF’s 26-component, 22-page All-Components Dynamic Multi-Page Flow Stress Test (same document across all 22 libraries).

Prawn (Ruby / Rails) stress-test results and rank out of 22 PDF libraries
MetricPrawnRank (of 22)
Wall-clock time537 ms15th fastest
CPU time0.65 s11th lowest
Peak RAM34 MB5th lowest
PDF file size79 KB1st smallest
Engine type
Coordinate Canvas
Under the hood
Pure-Ruby Prawn::Document vector DSL (zero wkhtmltopdf / browser binaries)
Runs in
Ruby 3 (Rails, Sinatra, Sidekiq, Hanami)
Verdict
#1 smallest PDF across all 22 libraries (79.0 KB for 22 pages; 2.3 KB / 7.5 ms single-page) in 537 ms & 34.0 MB RAM — pure Ruby.

Under the Hood & Benchmark Highlights

  • Surprise winner on output size: produced the #1 smallest PDF across ALL 22 libraries in our 11-page 26-component stress test — just 41.9 KB (10.8× smaller than Puppeteer and 24× smaller than WeasyPrint)!
  • Also ranked #3 fastest wall-clock (348 ms) and #4 lowest RAM (31.9 MB) — a dramatic drop-in improvement for Rails apps migrating off archived wkhtmltopdf / wicked_pdf.
  • Built-in fonts are Windows-1252 only, so register a TTF font family for full UTF-8.

Good to Know (Prawn Caveats)

  • Built-in fonts are Windows-1252 only: Prawn raises Prawn::Errors::IncompatibleStringEncoding on UTF-8 characters outside Windows-1252 unless you register a TrueType font family via pdf.font_families.update().
  • Bottom-left coordinate system: Prawn draws from the bottom-left origin in points; CodePDF flips canvas coordinates and emits cursor offsets for dynamic loops without needing prawn-table.
  • Triple-licensed (Ruby License / GPL-2.0 / GPL-3.0): Proprietary commercial apps should elect the Ruby License terms.

Popular Prawn PDF Templates

All 100 CodePDF templates export to Prawn (Ruby / Rails). Browse them by category: Career (5) · Business (6) · Education (7) · Marketing (6) · Human Resources (8) · Finance (7) · Legal (8) · Healthcare (10) · Logistics (6) · Real Estate (6) · Hospitality (4) · Operations (5) · Technology (5) · Banking (9) · Non-Profit (4) · Personal (3) · Compliance & Enterprise (1), or see the full template gallery.

Prawn Template FAQs

Is Prawn a good replacement for wicked_pdf in Ruby on Rails?

Yes! Because wkhtmltopdf is archived upstream, Rails teams use Prawn for pure-Ruby PDF generation with zero system binaries — and CodePDF removes the manual coordinate layout effort.

How do I stream a CodePDF Prawn template from a Rails controller?

Render the document in memory with pdf.render and return send_data pdf_bytes, filename: 'invoice.pdf', type: 'application/pdf', disposition: 'inline'.

Does exported Prawn code require prawn-table?

No — CodePDF draws crisp vector tables and dynamic row loops using core Prawn primitives with zero extra gems required.

Compare Prawn with Other PDF Libraries

Prawn is the only Ruby engine CodePDF targets. See the full 22-library benchmark rankings · Compare all supported PDF libraries