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

Parameters:

  • pdf_document (Prawn::Document)

    PDF document to wrap



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

Parameters:

  • move_down (Float)

    Quantity to advance (move down)



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

Parameters:

  • buffer (Array)

    Buffer of items

  • options (Hash)

    Output options

Returns:

  • (Float)

    calculated height



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

Parameters:

  • buffer (Array)

    Buffer of items

Returns:

  • (Float)

    calculated width



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

Parameters:

  • x (Float)

    left position of the rectangle

  • y (Float)

    top position of the rectangle

  • width (Float)

    width of the rectangle

  • height (Float)

    height of the rectangle

  • color (String)

    fill color



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

Parameters:

  • color (String)

    line color

  • dash (Integer|Array)

    integer or array of integer with dash options



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

Parameters:

  • src (String)

    image source path

  • options (Hash) (defaults to: {})

    hash of options



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

Returns:

  • (Float)

    height



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

Returns:

  • (Float)

    width



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

Parameters:

  • buffer (Array)

    array of text items

  • options (Hash)

    hash of options

  • bounding_box (Array) (defaults to: nil)

    bounding box arguments, if bounded



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

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

Underline

Parameters:

  • x1 (Float)

    left position of the line

  • x2 (Float)

    right position of the line

  • y (Float)

    vertical position of the line



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