Module: Prawn::Stamp

Included in:
Document
Defined in:
lib/prawn/stamp.rb

Overview

The Prawn::Stamp module is used to create content that will be included multiple times in a document. Using a stamp has three advantages over creating content anew each time it is placed on the page:

i.   faster document creation
ii.  smaller final document
iii. faster display on subsequent displays of the repeated
element because the viewer application can cache the rendered
results

Example:

pdf.create_stamp("my_stamp") {
  pdf.fill_circle_at([10, 15], :radius => 5)
  pdf.text("hello world", :at => [20, 10])
}
pdf.stamp("my_stamp")

Instance Method Summary collapse

Instance Method Details

#create_stamp(name, &block) ⇒ Object

Creates a re-usable stamp named name

raises Prawn::Errors::NameTaken if a stamp already exists in this document with this name raises Prawn::Errors::InvalidName if name.empty?

Example:

pdf.create_stamp("my_stamp") {
  pdf.fill_circle_at([10, 15], :radius => 5)
  pdf.text("hello world", :at => [20, 10])
}


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/prawn/stamp.rb', line 90

def create_stamp(name, &block)
  dictionary = create_stamp_dictionary(name)

  @active_stamp_stream = ""
  @active_stamp_dictionary = dictionary

  update_colors
  yield if block_given?
  update_colors

  dictionary.data[:Length] = @active_stamp_stream.length + 1
  dictionary << @active_stamp_stream

  @active_stamp_stream = nil
  @active_stamp_dictionary = nil
end

#stamp(name) ⇒ Object

Renders the stamp named name to the page raises Prawn::Errors::InvalidName if name.empty? raises Prawn::Errors::UndefinedObjectName if no stamp has been created with this name

Example:

pdf.create_stamp("my_stamp") {
  pdf.fill_circle_at([10, 15], :radius => 5)
  pdf.text("hello world", :at => [20, 10])
}
pdf.stamp("my_stamp")


43
44
45
46
47
# File 'lib/prawn/stamp.rb', line 43

def stamp(name)
  dictionary_name, dictionary = stamp_dictionary(name)
  add_content "/#{dictionary_name} Do"
  page_xobjects.merge!(dictionary_name => dictionary)
end

#stamp_at(name, point) ⇒ Object

Renders the stamp named name at a position offset from the initial coords at which the elements of the stamp was created

Example:

pdf.create_stamp("circle") do
  pdf.fill_circle_at([0, 0], :radius => 25)
end
# draws a circle at 100, 100
pdf.stamp_at("circle", [100, 100])

See stamp() for exceptions that might be raised



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/prawn/stamp.rb', line 62

def stamp_at(name, point)
  # Save the graphics state
  add_content "q"

  # Translate the user space
  x,y = point
  translate_position = "1 0 0 1 %.3f %.3f cm" % [x, y]
  add_content translate_position
  
  # Draw the stamp in the now translated user space
  stamp(name)
  
  # Restore the graphics state to remove the translation
  add_content "Q"
end