Module: Prawn::Document::Internals

Included in:
Prawn::Document
Defined in:
lib/prawn/document/internals.rb

Overview

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.

Instance Method Summary collapse

Instance Method Details

#add_content(str) ⇒ Object

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


48
49
50
# File 'lib/prawn/document/internals.rb', line 48

def add_content(str)
  page.content << str << "\n"
end

#before_render(&block) ⇒ Object

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



68
69
70
# File 'lib/prawn/document/internals.rb', line 68

def before_render(&block)
  @before_render_callbacks << block
end

#go_to_page(k) ⇒ Object

:nodoc:



72
73
74
75
76
# File 'lib/prawn/document/internals.rb', line 72

def go_to_page(k) # :nodoc:
  jump_to = @store.pages.data[:Kids][k]
  @current_page = jump_to.identifier
  @page_content = jump_to.data[:Contents].identifier
end

#namesObject

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.



56
57
58
# File 'lib/prawn/document/internals.rb', line 56

def names
  @store.root.data[:Names] ||= ref!(:Type => :Names)
end

#names?Boolean

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

Returns:

  • (Boolean)


62
63
64
# File 'lib/prawn/document/internals.rb', line 62

def names?
  @store.root.data[:Names]
end

#on_page_create(&block) ⇒ Object

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



80
81
82
83
84
85
86
# File 'lib/prawn/document/internals.rb', line 80

def on_page_create(&block)
   if block_given?
      @on_page_create_callback = block
   else
      @on_page_create_callback = nil
   end
end

#ref(data) ⇒ Object

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



24
25
26
# File 'lib/prawn/document/internals.rb', line 24

def ref(data)
  ref!(data).identifier
end

#ref!(data) ⇒ Object

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



36
37
38
# File 'lib/prawn/document/internals.rb', line 36

def ref!(data)
  @store.ref(data)
end