module Prawn::View

This mixin allows you to create modular Prawn code without the need to create subclasses of {Prawn::Document}.

“‘ruby class Greeter

include Prawn::View

# Optional override: allows you to set document options or even use
# a custom document class
def document
  @document ||= Prawn::Document.new(page_size: 'A4')
end

def initialize(name)
  @name = name
end

def say_hello
  text "Hello, #{@name}!"
end

def say_goodbye
  font("Courier") do
    text "Goodbye, #{@name}!"
  end
end

end

greeter = Greeter.new(“Gregory”)

greeter.say_hello greeter.say_goodbye

greeter.save_as(“greetings.pdf”) “‘

The short story about why you should use this mixin rather than creating subclasses of ‘Prawn::Document` is that it helps prevent accidental conflicts between your code and Prawn’s code.

Here’s the slightly longer story…

By using composition rather than inheritance under the hood, this mixin allows you to keep your state separate from ‘Prawn::Document`’s state, and also will prevent unexpected method name collisions due to late binding effects.

This mixin is mostly meant for extending Prawn’s functionality with your own additions, but you can also use it to replace or wrap existing Prawn methods. Calling ‘super` will still work as expected, and alternatively you can explicitly call `document.some_method` to delegate to Prawn where needed.