Class: PrawnHtml::PdfWrapper

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/prawn_html/pdf_wrapper.rb

Instance Method Summary collapse

Constructor Details

#initialize(pdf_document) ⇒ PdfWrapper

Wrapper for Prawn PDF Document



14
15
16
# File 'lib/prawn_html/pdf_wrapper.rb', line 14

def initialize(pdf_document)
  @pdf = pdf_document
end

Instance Method Details

#advance_cursor(move_down) ⇒ Object

Advance the cursor



21
22
23
24
25
# File 'lib/prawn_html/pdf_wrapper.rb', line 21

def advance_cursor(move_down)
  return if !move_down || move_down == 0

  pdf.move_down(move_down)
end

#calc_buffer_height(buffer, options) ⇒ Float

Calculate the height of a buffer of items



33
34
35
# File 'lib/prawn_html/pdf_wrapper.rb', line 33

def calc_buffer_height(buffer, options)
  pdf.height_of_formatted(buffer, options)
end

#calc_buffer_width(buffer) ⇒ Float

Calculate the width of a buffer of items



42
43
44
45
46
47
48
49
50
51
# File 'lib/prawn_html/pdf_wrapper.rb', line 42

def calc_buffer_width(buffer)
  width = 0
  buffer.each do |item|
    font_family = item[:font] || pdf.font.name
    pdf.font(font_family, size: item[:size] || pdf.font_size) do
      width += pdf.width_of(item[:text], inline_format: true)
    end
  end
  width
end

#draw_rectangle(x:, y:, width:, height:, color:) ⇒ Object

Draw a rectangle



74
75
76
77
78
79
# File 'lib/prawn_html/pdf_wrapper.rb', line 74

def draw_rectangle(x:, y:, width:, height:, color:)
  current_fill_color = pdf.fill_color
  pdf.fill_color = color
  pdf.fill_rectangle([y, x], width, height)
  pdf.fill_color = current_fill_color
end

#horizontal_rule(color:, dash:) ⇒ Object

Horizontal line



85
86
87
88
89
90
91
92
# File 'lib/prawn_html/pdf_wrapper.rb', line 85

def horizontal_rule(color:, dash:)
  current_color = pdf.stroke_color
  pdf.dash(dash) if dash
  pdf.stroke_color = color if color
  pdf.stroke_horizontal_rule
  pdf.stroke_color = current_color if color
  pdf.undash if dash
end

#image(src, options = {}) ⇒ Object

Image



98
99
100
101
102
# File 'lib/prawn_html/pdf_wrapper.rb', line 98

def image(src, options = {})
  return unless src

  pdf.image(src, options)
end

#page_heightFloat

Height of the page



56
57
58
# File 'lib/prawn_html/pdf_wrapper.rb', line 56

def page_height
  pdf.bounds.height
end

#page_widthFloat

Width of the page



63
64
65
# File 'lib/prawn_html/pdf_wrapper.rb', line 63

def page_width
  pdf.bounds.width
end

#puts(buffer, options, bounding_box: nil, left_indent: 0) ⇒ Object

Output to the PDF document



109
110
111
112
113
114
115
116
117
# File 'lib/prawn_html/pdf_wrapper.rb', line 109

def puts(buffer, options, bounding_box: nil, left_indent: 0)
  return output_buffer(buffer, options, left_indent: left_indent) unless bounding_box

  current_y = pdf.cursor
  pdf.bounding_box(*bounding_box) do
    output_buffer(buffer, options, left_indent: left_indent)
  end
  pdf.move_cursor_to(current_y)
end

#table(rows, options = {}) ⇒ Object



130
131
132
# File 'lib/prawn_html/pdf_wrapper.rb', line 130

def table(rows, options = {})
  pdf.table(rows, options) # uses prawn-table
end

#underline(x1:, x2:, y:) ⇒ Object

Underline



124
125
126
127
128
# File 'lib/prawn_html/pdf_wrapper.rb', line 124

def underline(x1:, x2:, y:)
  pdf.stroke do
    pdf.line [x1, y], [x2, y]
  end
end