Sha256: 90b1bd0a1d2e93d1c87c037902ba292003f74a94b599af606c22ecc2a08298db

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

require 'prawn'
require 'prawn/table'

module PrawnRails

  # This derives from Prawn::Document in order to override defaults. 
  # Note that the Prawn::Document behaviour itself shouldn't be changed.
  class Document < Prawn::Document
    def self.extensions
      Prawn::Document.extensions
    end

    def initialize(opts = {})
      if PrawnRails.config.respond_to?(:to_h)
        default = PrawnRails.config.to_h.merge(opts)
      else
        default = PrawnRails.config.marshal_dump.merge(opts)
      end
      super(default)
    end

    # Typically text expects a string. But Rails views have this interesting concept that they implicitly call `to_s` on all the variables before rendering. So, passing an integer to text fails:
    #
    # pdf.text 10       #=> fails because 10 is not a string
    # pdf.text 10.to_s  #=> works
    #
    # To circumvent this situation, we call to_s on value, and delegate action to actual Prawn::Document.
    def text(value, options = {})
      super(value.to_s, options)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
prawn-rails-1.2.0 lib/prawn-rails/document.rb