Prawn::Document::Internals

This module exposes a few low-level PDF features for those who want to extend Prawn’s core functionality. If you are not comfortable with low level PDF functionality as defined by Adobe’s specification, chances are you won’t need anything you find here.

Public Instance Methods

add_content(str) click to toggle source

Appends a raw string to the current page content.

                              
 # Raw line drawing example:           
 x1,y1,x2,y2 = 100,500,300,550
 pdf.add_content("%.3f %.3f m" % [ x1, y1 ])  # move 
 pdf.add_content("%.3f %.3f l" % [ x2, y2 ])  # draw path
 pdf.add_content("S") # stroke                    
    # File lib/prawn/document/internals.rb, line 48
48:       def add_content(str)
49:         page.content << str << "\n"
50:       end
before_render(&block) click to toggle source

Defines a block to be called just before the document is rendered.

    # File lib/prawn/document/internals.rb, line 68
68:       def before_render(&block)
69:         @before_render_callbacks << block
70:       end
names() click to toggle source

The Name dictionary (PDF spec 3.6.3) for this document. It is lazily initialized, so that documents that do not need a name dictionary do not incur the additional overhead.

    # File lib/prawn/document/internals.rb, line 56
56:       def names
57:         @store.root.data[:Names] ||= ref!(:Type => :Names)
58:       end
names?() click to toggle source

Returns true if the Names dictionary is in use for this document.

    # File lib/prawn/document/internals.rb, line 62
62:       def names?
63:         @store.root.data[:Names]
64:       end
on_page_create(&block) click to toggle source

Defines a block to be called just before a new page is started.

    # File lib/prawn/document/internals.rb, line 80
80:       def on_page_create(&block)
81:          if block_given?
82:             @on_page_create_callback = block
83:          else
84:             @on_page_create_callback = nil
85:          end
86:       end
ref(data) click to toggle source

Creates a new Prawn::Reference and adds it to the Document’s object list. The data argument is anything that Prawn::PdfObject() can convert.

Returns the identifier which points to the reference in the ObjectStore

    # File lib/prawn/document/internals.rb, line 24
24:       def ref(data)
25:         ref!(data).identifier
26:       end
ref!(data) click to toggle source

Like ref, but returns the actual reference instead of its identifier.

While you can use this to build up nested references within the object tree, it is recommended to persist only identifiers, and them provide helper methods to look up the actual references in the ObjectStore if needed. If you take this approach, Prawn::Document::Snapshot will probably work with your extension

    # File lib/prawn/document/internals.rb, line 36
36:       def ref!(data)
37:         @store.ref(data)
38:       end

Private Instance Methods

finalize_all_page_contents() click to toggle source
    # File lib/prawn/document/internals.rb, line 90
90:       def finalize_all_page_contents
91:         (1..page_count).each do |i|
92:           go_to_page i
93:           repeaters.each { |r| r.run(i) }
94:           restore_graphics_state
95:           page.content.compress_stream if compression_enabled?
96:           page.content.data[:Length] = page.content.stream.size
97:         end
98:       end
min_version(min) click to toggle source

raise the PDF version of the file we’re going to generate. A private method, designed for internal use when the user adds a feature to their document that requires a particular version.

     # File lib/prawn/document/internals.rb, line 104
104:       def min_version(min)
105:         @version = min if min > @version
106:       end
render_body(output) click to toggle source

Write out the PDF Body, as per spec 3.4.2

     # File lib/prawn/document/internals.rb, line 122
122:       def render_body(output)
123:         @store.compact if @optimize_objects
124:         @store.each do |ref|
125:           ref.offset = output.size
126:           output << ref.object
127:         end
128:       end
render_header(output) click to toggle source

Write out the PDF Header, as per spec 3.4.1

     # File lib/prawn/document/internals.rb, line 110
110:       def render_header(output)
111:         @before_render_callbacks.each{ |c| c.call(self) }
112: 
113:         # pdf version
114:         output << "%PDF-#{@version}\n"
115: 
116:         # 4 binary chars, as recommended by the spec
117:         output << "%\xFF\xFF\xFF\xFF\n"
118:       end
render_trailer(output) click to toggle source

Write out the PDF Trailer, as per spec 3.4.4

     # File lib/prawn/document/internals.rb, line 145
145:       def render_trailer(output)
146:         trailer_hash = {:Size => @store.size + 1, 
147:                         :Root => @store.root,
148:                         :Info => @store.info}
149:         trailer_hash.merge!(@trailer) if @trailer
150: 
151:         output << "trailer\n"
152:         output << Prawn::PdfObject(trailer_hash) << "\n"
153:         output << "startxref\n" 
154:         output << @xref_offset << "\n"
155:         output << "%%EOF" << "\n"
156:       end
render_xref(output) click to toggle source

Write out the PDF Cross Reference Table, as per spec 3.4.3

     # File lib/prawn/document/internals.rb, line 132
132:       def render_xref(output)
133:         @xref_offset = output.size
134:         output << "xref\n"
135:         output << "0 #{@store.size + 1}\n"
136:         output << "0000000000 65535 f \n"
137:         @store.each do |ref|
138:           output.printf("%010d", ref.offset)
139:           output << " 00000 n \n"
140:         end
141:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.