class PDF::QuickRef

QuickRef

A formatting language to create a quick reference sheet. This is a multi-column page in landscape mode that generally has three or four columns. This format may also be used for brochures, but brochure creation requires a bit of management to create properly.

Reference Sheets

A three-column reference sheet is generally in the form of:

Page 1:

column 1 | column 2 | column 3

Page 2:

column 4 | column 5 | column 6

The formatting language provided in QuickRef is based around this text flow. The title of the quick reference sheet is in column 1. The two pages are intended to be printed on both sides of pieces of paper so that columns 1 and 6 are matched. This will use a Z-fold that places columns 5 and 6 face to face and columns 2 and 3 face to face. In the folded reference sheet, columns 1 and 4 will be facing out.

Brochures

In contrast, brochures differ vastly in their design, although the common brochure is also three columns and either follows the same layout as a reference sheet or uses an overlapping fold.

When an overlapping fold is used, the title is typically on column 6 (assuming a left-to-right reading order). A short summary will appear on column 4. Contact information about the maker of the brochure is typically in column 5. Columns 1, 2, and 3 will contain the main body of the brochure. The brochure will be folded so that columns 2 and 3 are face to face. After this, column 1 will face column 4 (exposed by the first fold). In the folded brochure, columns 5 and 6 are facing out.

Usage

qr = PDF::QuickRef.new # 3-column LETTER
qr.title "My QuickRef"
qr.h1 "H1 Text"
qr.lines "Text to put after the header."
qr.save_as "MyQuickRef.pdf"

Constants

VERSION

Attributes

body_font[RW]

The name of the font that will be used for body, lines, and pairs text. The default is ‘Times-Roman’.

body_font_encoding[RW]

The font encoding for body, lines, and pairs text.

body_font_size[RW]

The size body and code text. The default is 7 points.

code_font[RW]

The name of the font that will be used for code, codelines, and codepairs text; this is generally a fixed-pitch font. The default is ‘Courier’.

code_font_encoding[RW]

The font encoding for code, codelines, and codepairs text.

h1_font_size[RW]

The size h1 text. The default is 11 points.

h2_font_size[RW]

The size h2 text. The default is 9 points.

h3_font_size[RW]

The size h3 text. The default is 8 points.

h4_font_size[RW]

The size h4 text. The default is 7 points.

heading_font[RW]

The name of the font that will be used for h1, h2, h3, and h4 text. The default is Times-Roman.

heading_font_encoding[RW]

The font encoding for h1, h2, h3, and h4 text.

pdf[R]

Access to the raw PDF canvas for normal PDF::Writer configuration.

title_font[RW]

The name of the font that will be used for title text. The default font is Times-Roman.

title_font_encoding[RW]

The font encoding for title text.

title_font_size[RW]

The size title text. The default is 14 points.

Public Class Methods

make(*args, &block) click to toggle source

Creates a QuickRef document and then calls instance_eval on the document. This allows for a more natural use of the QuickRef class as a DSL for creating these documents.

Using make

PDF::QuickRef.make do # 3-column LETTER
  title "My QuickRef"
  h1 "H1 Text"
  lines "Text to put after the header."
  save_as "MyQuickRef.pdf"
end
    # File lib/pdf/quickref.rb
329 def self.make(*args, &block)
330   qr = PDF::QuickRef.new(*args)
331   qr.__send__(:instance_eval, &block)
332 end
new(paper = "LETTER", columns = 3, column_separators_visible = true) { |self| ... } click to toggle source

Create the quick reference document. paper is passed unchanged to the PDF::Writer.new; the page is always created landscape. Margins are initialized to 18 points. After some additional initialization is performed, the quick reference document is yielded to an optional block for further configuration. All of this is done before the columns are started.

After the columns are started, lines will be drawn between column positions.

    # File lib/pdf/quickref.rb
 65 def initialize(paper = "LETTER", columns = 3, column_separators_visible = true)
 66   @pdf  = PDF::Writer.new(:paper => paper, :orientation => :landscape)
 67   @pdf.margins_pt 18
 68   @pdf.y = @pdf.absolute_top_margin
 69 
 70   @title_font       = "Times-Roman"
 71   @heading_font     = "Times-Roman"
 72   @body_font        = "Times-Roman"
 73   @code_font        = "Courier"
 74   @title_font_size = 14
 75   @h1_font_size    = 11
 76   @h2_font_size    =  9
 77   @h3_font_size    =  8
 78   @h4_font_size    =  7
 79   @body_font_size  =  6
 80 
 81   @ptab = PDF::SimpleTable.new do |tab|
 82     tab.column_order.replace %w(one two)
 83 
 84     tab.font_size     = @body_font_size
 85     tab.show_lines    = :none
 86     tab.show_headings = false
 87     tab.orientation   = :center
 88     tab.position      = :center
 89   end
 90   @ltab = PDF::SimpleTable.new do |tab|
 91     tab.column_order.replace %w(line)
 92 
 93     tab.font_size     = @body_font_size
 94     tab.show_lines    = :none
 95     tab.show_headings = false
 96     tab.orientation   = :center
 97     tab.position      = :center
 98   end
 99 
100   yield self if block_given?
101 
102   @pdf.start_columns columns
103 
104   @ptab.font_size = @body_font_size
105   @ltab.font_size = @body_font_size
106 
107   @ptab.maximum_width = @pdf.column_width - 10
108   @ltab.maximum_width = @pdf.column_width - 10
109 
110   if column_separators_visible
111     # Put lines between the columns.
112     all = @pdf.open_object
113     @pdf.save_state
114     @pdf.stroke_color! Color::RGB::Black
115     @pdf.stroke_style  PDF::Writer::StrokeStyle::DEFAULT
116     (1 .. (columns - 1)).each do |ii|
117       x = @pdf.left_margin + (@pdf.column_width * ii)
118       x += (@pdf.column_gutter * (ii - 0.5))
119       @pdf.line(x, @pdf.page_height - @pdf.top_margin, x, @pdf.bottom_margin)
120       @pdf.stroke
121     end
122     @pdf.restore_state
123     @pdf.close_object
124     @pdf.add_object(all, :all_pages)
125   end
126 end

Public Instance Methods

body(text) click to toggle source

Writes body text. Paragraphs will be reflowed for optimal placement of text. Text separated by two line separators (e.g., nn, although the separator is platform dependent). The text will be placed with full justification.

    # File lib/pdf/quickref.rb
276 def body(text)
277     # Transform the text a little.
278   paras = text.split(%r(#{$/}{2}))
279   paras.map! { |para| para.split($/).join(" ").squeeze(" ") }
280   text = paras.join("\n\n")
281 
282   @pdf.text "#{text}\n", :font_size => @body_font_size, :justification => :full
283 end
codelines(text) click to toggle source

Creates a one-column zebra-striped table using the code font. Each line of the text is a separate row.

    # File lib/pdf/quickref.rb
203 def codelines(text)
204   data = text.split($/).map { |line| { "line" => line } }
205   @ltab.data.replace data
206   use_code_font
207   @ltab.render_on(@pdf)
208   use_body_font
209   @pdf.text "\n", :font_size => @body_font_size
210 end
codepairs(text) click to toggle source

Creates a two-column zebra-striped table using the code font. Each line of the text is a separate row; the two columns are separated by tab characters.

    # File lib/pdf/quickref.rb
182 def codepairs(text)
183   data = text.split($/).map do |line|
184     one, two = line.split(/\t/)
185     { 'one' => one, 'two' => two }
186   end
187   @ptab.data.replace data
188   use_code_font
189   @ptab.render_on(@pdf)
190   use_body_font
191   @pdf.text "\n", :font_size => @body_font_size
192 end
h1(text) click to toggle source

Writes the text with the heading_font and h1_font_size left justified in the column. The font is set to body_font after the heading is drawn.

    # File lib/pdf/quickref.rb
243 def h1(text)
244   use_heading_font
245   @pdf.text text, :font_size => @h1_font_size
246   use_body_font
247 end
h2(text) click to toggle source

Writes the text with the heading_font and h2_font_size left justified in the column. The font is set to body_font after the heading is drawn.

    # File lib/pdf/quickref.rb
251 def h2(text)
252   use_heading_font
253   @pdf.text "<i>#{text}</i>", :font_size => @h2_font_size
254   use_body_font
255 end
h3(text) click to toggle source

Writes the text with the heading_font and h3_font_size left justified in the column. The font is set to body_font after the heading is drawn.

    # File lib/pdf/quickref.rb
259 def h3(text)
260   use_heading_font
261   @pdf.text "<i>#{text}</i>", :font_size => @h3_font_size
262   use_body_font
263 end
h4(text) click to toggle source

Writes the text with the heading_font and h4_font_size left justified in the column. The font is set to body_font after the heading is drawn.

    # File lib/pdf/quickref.rb
267 def h4(text)
268   use_heading_font
269   @pdf.text "<b><i>#{text}</i></b>", :font_size => @h4_font_size
270   use_body_font
271 end
hline(style = PDF::Writer::StrokeStyle::DEFAULT, color = Color::RGB::Black) click to toggle source

Draws a horizontal line with the specified style and colour.

    # File lib/pdf/quickref.rb
292 def hline(style = PDF::Writer::StrokeStyle::DEFAULT,
293           color = Color::RGB::Black)
294   @pdf.y -= 2.5
295   @pdf.save_state
296   @pdf.stroke_style  style
297   @pdf.stroke_color! color
298   x0 = @pdf.left_margin
299   x1 = @pdf.left_margin + pdf.column_width
300   @pdf.line(x0, @pdf.y, x1, @pdf.y)
301   @pdf.stroke
302   @pdf.restore_state
303   @pdf.y -= 2.5
304 end
lines(text) click to toggle source

Creates a one-column zebra-striped table using the body font. Each line of the text is a separate row.

    # File lib/pdf/quickref.rb
195 def lines(text)
196   data = text.split($/).map { |line| { "line" => line } }
197   @ltab.data.replace data
198   @ltab.render_on(@pdf)
199   @pdf.text "\n", :font_size => @body_font_size
200 end
pairs(text) click to toggle source

Creates a two-column zebra-striped table using the body font. Each line of the text is a separate row; the two columns are separated by tab characters.

    # File lib/pdf/quickref.rb
170 def pairs(text)
171   data = text.split($/).map do |line|
172     one, two = line.split(/\t/)
173     { 'one' => one, 'two' => two }
174   end
175   @ptab.data.replace data
176   @ptab.render_on(@pdf)
177   @pdf.text "\n", :font_size => @body_font_size
178 end
pre(text) click to toggle source

Writes code text. Newlines and spaces will be preserved.

    # File lib/pdf/quickref.rb
285 def pre(text)
286   use_code_font
287   @pdf.text "#{text}\n", :font_size => @body_font_size
288   use_body_font
289 end
render() click to toggle source

Generates the PDF document as a string.

    # File lib/pdf/quickref.rb
312 def render
313   @pdf.render
314 end
Also aliased as: to_s
save_as(filename) click to toggle source

Writes the Quick Reference to disk.

    # File lib/pdf/quickref.rb
307 def save_as(filename)
308   @pdf.save_as(filename)
309 end
title(text) click to toggle source

Writes the text with the title_font and title_font_size centered in the column. After the title has been written, an hline will be drawn under the title. The font is set to body_font after the title is drawn.

    # File lib/pdf/quickref.rb
234 def title(text)
235   use_title_font
236   @pdf.text text, :font_size => @title_font_size, :justification => :center
237   use_body_font
238   hline
239 end
to_s()
Alias for: render
use_body_font() click to toggle source

Change the current font to the body font.

    # File lib/pdf/quickref.rb
222 def use_body_font
223   @pdf.select_font @body_font, @body_font_encoding
224 end
use_code_font() click to toggle source

Change the current font to the code font.

    # File lib/pdf/quickref.rb
226 def use_code_font
227   @pdf.select_font @code_font, @code_font_encoding
228 end
use_heading_font() click to toggle source

Change the current font to the heading font (used normally by h1, h2, h3, and h4|).

    # File lib/pdf/quickref.rb
218 def use_heading_font
219   @pdf.select_font @heading_font, @heading_font_encoding
220 end
use_title_font() click to toggle source

Change the current font to the title font.

    # File lib/pdf/quickref.rb
213 def use_title_font
214   @pdf.select_font @title_font, @title_font_encoding
215 end