Class: Report::Pdf

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/report/pdf.rb

Constant Summary collapse

DEFAULT_FONT =
{
  :normal      => File.expand_path('../pdf/DejaVuSansMono.ttf', __FILE__),
  :italic      => File.expand_path('../pdf/DejaVuSansMono-Oblique.ttf', __FILE__),
  :bold        => File.expand_path('../pdf/DejaVuSansMono-Bold.ttf', __FILE__),
  :bold_italic => File.expand_path('../pdf/DejaVuSansMono-BoldOblique.ttf', __FILE__),
}
DEFAULT_DOCUMENT =
{
  :top_margin => 72,
  :right_margin => 36,
  :bottom_margin => 72,
  :left_margin => 36,
  :page_layout => :landscape,
}
DEFAULT_HEAD =
{}
DEFAULT_BODY =
{
  :width => (10*72),
  :header => true,
  :cell_style => { :inline_format => true },
}
DEFAULT_NUMBER_PAGES =
[
  'Page <page> of <total>',
  {:at => [648, -2], :width => 100, :size => 10}
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

#safe_delete, #tmp_path

Constructor Details

#initialize(report) ⇒ Pdf

Returns a new instance of Pdf.



33
34
35
# File 'lib/report/pdf.rb', line 33

def initialize(report)
  @report = report
end

Instance Attribute Details

#reportObject (readonly)

Returns the value of attribute report.



31
32
33
# File 'lib/report/pdf.rb', line 31

def report
  @report
end

Instance Method Details

#cleanupObject



77
78
79
# File 'lib/report/pdf.rb', line 77

def cleanup
  safe_delete @path if @path
end

#pathObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/report/pdf.rb', line 37

def path
  return @path if defined?(@path)
  require 'prawn'
  tmp_path = tmp_path(:extname => '.pdf')
  Prawn::Document.generate(tmp_path, document) do |pdf|
    
    pdf.font_families.update(font_name => font)
    pdf.font font_name, :size => 10

    first = true
    report.class.tables.each do |table|
      if first
        first = false
      else
        pdf.start_new_page
      end
      if t = make_head(table._head)
        pdf.table t, head
        pdf.move_down 20
      end
      pdf.text table.name, :style => :bold
      if t = make_body(table._body)
        pdf.move_down 10
        pdf.table t, body
      end
    end

    pdf.number_pages(*number_pages)
  end
  
  if stamp
    raise "#{stamp} not readable or does not exist" unless File.readable?(stamp)
    require 'posix/spawn'
    POSIX::Spawn::Child.new 'pdftk', tmp_path, 'stamp', stamp, 'output', "#{tmp_path}.stamped"
    FileUtils.mv "#{tmp_path}.stamped", tmp_path
  end

  @path = tmp_path
end