Module: ResumeTools::Renderer::PDF

Defined in:
lib/resumetools/resume/pdf.rb

Constant Summary collapse

FONT_DIR =
File.join(File.dirname(__FILE__), '..', '..', 'fonts')
MARGINS =
[1.0, 1.0, 1.0, 1.0]
FONT_SIZES =
{
  :default => 9,
  :header => 14,
  :contact => 9,
  :section => 11,
  :para => 9,
  :item => 9,
  :period => 10
}
DATE_FORMAT =
"%B, %Y"

Instance Method Summary collapse

Instance Method Details

#render_pdf(opts = {}) ⇒ Object

Render to PDF


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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/resumetools/resume/pdf.rb', line 46

def render_pdf(opts={})
  default_font = opts.delete(:default_font) || "SourceSansPro"

  pdf = Prawn::Document.new(
    :info => {},
    :top_margin => MARGINS[0].in,
    :left_margin => MARGINS[1].in,
    :bottom_margin => MARGINS[2].in,
    :right_margin => MARGINS[3].in
  )

  pdf.font_families.update(
    "VeraSans" => {
      :normal => File.expand_path("Vera.ttf", FONT_DIR),
      :bold => File.expand_path("VeraBd.ttf", FONT_DIR),
      :italic => File.expand_path("VeraIt.ttf", FONT_DIR),
      :bold_italic => File.expand_path("VeraBI.ttf", FONT_DIR)
    },
    "SourceSansPro" => {
      :normal => File.expand_path("SourceSansPro-Regular.ttf", FONT_DIR),
      :bold => File.expand_path("SourceSansPro-Semibold.ttf", FONT_DIR),
      :italic => File.expand_path("SourceSansPro-It.ttf", FONT_DIR),
      :bold_italic => File.expand_path("SourceSansPro-SemiboldIt.ttf", FONT_DIR)
    }
  )

  # Set default font
  pdf.font(default_font, :style => :normal, :size => FONT_SIZES[:default], :kerning => true)

  # Name
  pdf.text self.full_name, :style => :bold, :size => FONT_SIZES[:header], :align => :center

  # Contact info
  self.header_lines.each do |line|
    pdf.text line, :align => :center
  end

  pdf.pad_bottom 20 do
  end

  # Sections
  self.sections.each do |section|
    pdf.pad_top(20) do
      # Section title
      pdf.text section.title, :style => :bold, :size => FONT_SIZES[:section]

      # Section paragraph
      unless section.para.blank?
        pdf.span(pdf.bounds.width - 10, :position => 10) do
          pdf.pad_top(5) { pdf.text section.para, :size => FONT_SIZES[:para] }
        end
      end

      # Section items
      unless section.items.empty?
        pdf.table(section.items.map { |item| [" •", item.text] }, :cell_style => {
          :borders => []
        })
        # , cell_style: {
        #   :font_size => FONT_SIZES[:item],
        #   :border_style => :none,
        #   :border_color => "ffffff",
        #   :vertical_padding => 4,
        #   :horizontal_padding => 0,
        #   :align => { 0 => :left, 1 => :left },
        #   :column_widths => { 0 => 20, 1 => 420 }
        # }
      end

      # Periods
      section.periods.each do |period|
        pdf.pad_top(5) do
          # Period title
          pdf.pad_top(5) { pdf.text period.title, :style => :bold, :size => FONT_SIZES[:period] }

          # Period details
          pdf.pad_top(5) do
            pdf.text(period.line, :size => FONT_SIZES[:default])
          end

          # Period items
          unless period.items.empty?
            pdf.table(period.items.map { |item| [" •", item.text] }, :cell_style => {
              :borders => []
            })
          end
        end
      end
    end
  end

  pdf.render
end