File: prawn.rb

package info (click to toggle)
ruby-tilt 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 632 kB
  • sloc: ruby: 4,975; makefile: 7
file content (39 lines) | stat: -rw-r--r-- 730 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# frozen_string_literal: true

# = Prawn
#
# Prawn template implementation.
#
# === See also
#
# * http://prawnpdf.org
#
# === Related module
#
# * Tilt::PrawnTemplate

require_relative 'template'
require 'prawn'

module Tilt
  class PrawnTemplate < Template
    self.default_mime_type = 'application/pdf'

    def prepare
      @options[:page_size] = 'A4' unless @options.has_key?(:page_size)
      @options[:page_layout] = :portrait unless @options.has_key?(:page_layout)
    end

    def evaluate(scope, locals, &block)
      pdf = ::Prawn::Document.new(@options)
      locals = locals.dup
      locals[:pdf] = pdf
      super
      pdf.render
    end

    def precompiled_template(locals)
      @data.to_str
    end
  end
end