The Artima Developer Community
Sponsored Link

Ruby Code & Style
Creating Printable Documents with Ruby
by Austin Ziegler
October 10, 2005

<<  Page 4 of 4



Tables in PDF::Writer are achieved with the PDF::SimpleTable class, so called because it has a relatively simple table model with more constraints on how the table can be rendered than the table model in HTML or XHTML. The table model itself is simple, but there are a number of configuration options that affect the display of the table. The following code demonstrates part of what is possible with PDF::SimpleTable.

      require 'pdf/writer'
      require 'pdf/simpletable'

      pdf =
      pdf.select_font("Helvetica") do |tab|
        tab.title = "PDF User Unit Conversions"
        tab.column_order.push(*%w(from1 to1 from2 to2))

        tab.columns["from1"] ="from1") { |col|
          col.heading = "From"
        tab.columns["to1"] ="to1") { |col|
          col.heading = "To"
        tab.columns["from2"] ="from2") { |col|
          col.heading = "From"
        tab.columns["to2"] ="to2") { |col|
          col.heading = "To"

        tab.show_lines    = :all
        tab.show_headings = true
        tab.orientation   = :center
        tab.position      = :center

        data = [
          { "from1" =< "1 point", "to1" =< "0.3528 mm",
            "from2" =< "1 point", "to2" =< "1/72”" },
          { "from1" =< "10 mm", "to1" =< "28.35 pts",
            "from2" =< "", "to2" =< "" },
          { "from1" =< "A4", "to1" =< "210 mm × 297 mm",
            "from2" =< "A4", "to2" =< "595.28 pts × 841.89 pts" },
          { "from1" =< "LETTER", "to1" =< "81/2” × 11”",
            "from2" =< "LETTER", "to2" =< "612 pts × 792 pts" },
          ] data
Unit conversion PDF example
Figure 6. Unit Conversion Table PDF Example

Partial Document Generation Technique

One of the limitations of PDF::Writer (that the documents may never become singleton objects or hold references to Proc objects) also provides a strength for document generation. This limitation means that a document may be saved with Marshal::dump at any point during the generation process and it may then be restarted with the pregenerated code in that program or a separate program entirely. PDF::TechBook uses a variant of this to cache the manual so that all that needs to be done is page numbering and table of contents generation.

This technique could be used to generate a partial document that contains a letterhead or logo, like below:

      # letterhead.rb
      require 'pdf/writer'
      pdf =

      pdf.text "Using PDF::Writer"
      pdf.line(pdf.absolute_left_margin, pdf.y + 10,
      pdf.absolute_right_margin, pdf.y + 10).stroke
      pdf.top_margin = pdf.y + 20"letterhead._pc", "wb") { |f| f.write Marshal.dump(pdf) }

Then, at a later date, this pregenerated letter head can be loaded—even by multiple processes at the same time—and added to:

      require "pdf/writer"

      class HelloController < ApplicationController
        # ...
        def pdf
          _pdf ="letterhead._pc", "rb") { |f|

          _pdf.text "Hello, Ruby.", :font_size => 72, :justification => :center

          send_data _pdf.render, :filename => "hello.pdf",
                    :type => "application/pdf"
        # ...


PDF::Writer is a powerful and useful library that can be used in most situations requiring formatted output. While its layout engine isn’t as powerful as a word processor’s, it is sufficient for most purposes. Being pure Ruby, it isn’t as fast as other solutions, either, but it is certainly fast enough. There are techniques that one can use, too, such as the partial document generation technique, to amortize the cost of generating complex documents. Development on PDF::Writer is active, and I am receptive of any bug fixes, enhancements, or extensions you wish to offer.

Talk Back!

Have an opinion about PDF::Writer? Discuss this article in the Articles Forum topic, Creating Printable Documents with Ruby.


[0] Bindings for ClibPDF

[1] ClibPDF

[2] Panda PDF

[3] Bindings to PDFlib

[4] PDFlib

[5] PHP Library “cPDF” by R & OS

[6] PDF::Writer Preview

[7] PDF::Writer 1.1.3

[8] Transaction::Simple

[9] Color-tools

[10] Ruby on Rails

[11] RMagick

[12] Why 72 DPI?

[13] Scalable Vector Graphics

[14] Encapsulated PostScript

[15] Bézier Curves

About the Author

Austin Ziegler has been programming for twenty years, starting on a TRS-80 Model III computer. He discovered Ruby three years ago and has since developed, ported, or extended several different packages, including PDF::Writer, Ruwiki, Text::Format, MIME::Types, and Diff::LCS. He lives in Toronto, Canada.

<<  Page 4 of 4

Sponsored Links

Copyright © 1996-2018 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use