Class: PdfTempura::Renderer

Inherits:
Object
  • Object
show all
Defined in:
lib/pdf_tempura/renderer.rb

Instance Method Summary collapse

Constructor Details

#initialize(template_path, pages, options = {}) ⇒ Renderer

Returns a new instance of Renderer.



7
8
9
10
11
12
# File 'lib/pdf_tempura/renderer.rb', line 7

def initialize(template_path, pages, options = {})
  @template_path = template_path
  @pages = pages
  @options = options
  @template_page_count = options[:template_page_count] || @pages.count
end

Instance Method Details

#renderObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/pdf_tempura/renderer.rb', line 21

def render
  tempfile = Tempfile.new(["render",".pdf"],:encoding => 'ascii-8bit')

  begin
    pdf = Prawn::Document.new(skip_page_creation: true, margin: 0)

    render_into(pdf)

    tempfile.write pdf.render
    tempfile.rewind

    yield tempfile
  ensure
    tempfile.unlink
  end
end

#render_into(pdf) ⇒ Object



14
15
16
17
18
19
# File 'lib/pdf_tempura/renderer.rb', line 14

def render_into(pdf)
  @pages.to_enum.with_index(0).each do |page, index|
    pdf.start_new_page template: @template_path, template_page: ((index % @template_page_count) + 1)
    Render::Page.new(page,@options).render(pdf)
  end
end