Class PDF::QuickRef
In: lib/pdf/quickref.rb
Parent: Object
TechBook Transaction::Simple SimpleTable Complex Action FontDescriptor Procset Catalog FontEncoding Pages Destination Info Encryption Annotation Contents Outline Page Outlines Font ViewerPreferences Image Hash OHash QuickRef StdDev FontMetrics StrokeStyle ARC4 PolygonPoint ImageInfo lib/pdf/simpletable.rb lib/pdf/writer.rb lib/pdf/techbook.rb lib/pdf/quickref.rb lib/pdf/charts/stddev.rb Charts Math lib/pdf/writer/ohash.rb lib/pdf/writer/fontmetrics.rb lib/pdf/writer/strokestyle.rb lib/pdf/writer/arc4.rb lib/pdf/writer/graphics.rb lib/pdf/writer/object.rb lib/pdf/writer/object/image.rb External lib/pdf/writer/object/font.rb lib/pdf/writer/object/outlines.rb lib/pdf/writer/object/contents.rb lib/pdf/writer/object/annotation.rb lib/pdf/writer/object/destination.rb lib/pdf/writer/object/viewerpreferences.rb lib/pdf/writer/object/info.rb lib/pdf/writer/object/fontencoding.rb lib/pdf/writer/object/page.rb lib/pdf/writer/object/catalog.rb lib/pdf/writer/object/outline.rb lib/pdf/writer/object/encryption.rb lib/pdf/writer/object/procset.rb lib/pdf/writer/object/action.rb lib/pdf/writer/object/pages.rb lib/pdf/writer/object/fontdescriptor.rb Object OffsetReader EN Lang lib/pdf/writer/graphics/imageinfo.rb Graphics Writer PDF dot/m_33_0.png

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"

Methods

body   codelines   codepairs   h1   h2   h3   h4   hline   lines   make   new   pairs   pre   render   save_as   title   to_s   use_body_font   use_code_font   use_heading_font   use_title_font  

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

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

[Source]

     # File lib/pdf/quickref.rb, line 328
328:   def self.make(*args, &block)
329:     qr = PDF::QuickRef.new(*args)
330:     qr.__send__(:instance_eval, &block)
331:   end

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.

[Source]

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

Public Instance methods

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

[Source]

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

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

[Source]

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

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.

[Source]

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

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.

[Source]

     # File lib/pdf/quickref.rb, line 242
242:   def h1(text)
243:     use_heading_font
244:     @pdf.text text, :font_size => @h1_font_size
245:     use_body_font
246:   end

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.

[Source]

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

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.

[Source]

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

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.

[Source]

     # File lib/pdf/quickref.rb, line 266
266:   def h4(text)
267:     use_heading_font
268:     @pdf.text "<b><i>#{text}</i></b>", :font_size => @h4_font_size
269:     use_body_font
270:   end

Draws a horizontal line with the specified style and colour.

[Source]

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

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

[Source]

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

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.

[Source]

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

Writes code text. Newlines and spaces will be preserved.

[Source]

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

Generates the PDF document as a string.

[Source]

     # File lib/pdf/quickref.rb, line 311
311:   def render
312:     @pdf.render
313:   end

Writes the Quick Reference to disk.

[Source]

     # File lib/pdf/quickref.rb, line 306
306:   def save_as(filename)
307:     @pdf.save_as(filename)
308:   end

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.

[Source]

     # File lib/pdf/quickref.rb, line 233
233:   def title(text)
234:     use_title_font
235:     @pdf.text text, :font_size => @title_font_size, :justification => :center
236:     use_body_font
237:     hline
238:   end
to_s()

Alias for render

Change the current font to the body font.

[Source]

     # File lib/pdf/quickref.rb, line 221
221:   def use_body_font
222:     @pdf.select_font @body_font, @body_font_encoding
223:   end

Change the current font to the code font.

[Source]

     # File lib/pdf/quickref.rb, line 225
225:   def use_code_font
226:     @pdf.select_font @code_font, @code_font_encoding
227:   end

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

[Source]

     # File lib/pdf/quickref.rb, line 217
217:   def use_heading_font
218:     @pdf.select_font @heading_font, @heading_font_encoding
219:   end

Change the current font to the title font.

[Source]

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

[Validate]