Module: Prawn::Stamp

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

Overview

This 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:

  • Faster document creation.
  • Smaller final document.
  • Faster display on subsequent displays of the repeated element because the viewer application can cache the rendered results.

Examples:

pdf.create_stamp("my_stamp") {
  pdf.fill_circle([10, 15], 5)
  pdf.draw_text("hello world", at: [20, 10])
}
pdf.stamp("my_stamp")

Stable API collapse

Instance Method Details

#create_stamp(name) { ... } ⇒ void

This method returns an undefined value.

Creates a re-usable stamp.

Examples:

pdf.create_stamp("my_stamp") {
  pdf.fill_circle([10, 15], 5)
  pdf.draw_text("hello world", at: [20, 10])
}

Parameters:

  • name (String)

    Stamp name.

Yields:

  • Stamp content.

Raises:

Source Code
lib/prawn/stamp.rb, line 74
74
def create_stamp(name, &block)
75
  dictionary = create_stamp_dictionary(name)
76
77
  state.page.stamp_stream(dictionary, &block)
78
end

#stamp(name) ⇒ void

This method returns an undefined value.

Renders the stamp.

Examples:

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

Parameters:

  • name (String)

Raises:

Source Code
lib/prawn/stamp.rb, line 35
35
def stamp(name)
36
  dictionary_name, dictionary = stamp_dictionary(name)
37
  renderer.add_content("/#{dictionary_name} Do")
38
  update_annotation_references(dictionary.data[:Annots])
39
  state.page.xobjects.merge!(dictionary_name => dictionary)
40
end

#stamp_at(name, point) ⇒ void

This method returns an undefined value.

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

Examples:

pdf.create_stamp("circle") do
  pdf.fill_circle([0, 0], 25)
end
# draws a circle at 100, 100
pdf.stamp_at("circle", [100, 100])

Parameters:

  • name (String)
  • point (Array(Number, Number))

See Also:

  • for exceptions that might be raised.
Source Code
lib/prawn/stamp.rb, line 56
56
def stamp_at(name, point)
57
  translate(point[0], point[1]) { stamp(name) }
58
end