-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpdf_wrapper.rb
More file actions
142 lines (121 loc) · 3.67 KB
/
pdf_wrapper.rb
File metadata and controls
142 lines (121 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
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
139
140
141
142
# frozen_string_literal: true
require 'forwardable'
require 'prawn/table'
module PrawnHtml
class PdfWrapper
extend Forwardable
def_delegators :@pdf, :start_new_page, :table
# Wrapper for Prawn PDF Document
#
# @param pdf_document [Prawn::Document] PDF document to wrap
def initialize(pdf_document)
@pdf = pdf_document
end
# Advance the cursor
#
# @param move_down [Float] Quantity to advance (move down)
def advance_cursor(move_down)
return if !move_down || move_down == 0
pdf.move_down(move_down)
end
# Calculate the height of a buffer of items
#
# @param buffer [Array] Buffer of items
# @param options [Hash] Output options
#
# @return [Float] calculated height
def calc_buffer_height(buffer, options)
pdf.height_of_formatted(buffer, options)
end
# Calculate the width of a buffer of items
#
# @param buffer [Array] Buffer of items
#
# @return [Float] calculated width
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
# Height of the page
#
# @return [Float] height
def page_height
pdf.bounds.height
end
# Width of the page
#
# @return [Float] width
def page_width
pdf.bounds.width
end
# Draw a rectangle
#
# @param x [Float] left position of the rectangle
# @param y [Float] top position of the rectangle
# @param width [Float] width of the rectangle
# @param height [Float] height of the rectangle
# @param color [String] fill color
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 line
#
# @param color [String] line color
# @param dash [Integer|Array] integer or array of integer with dash options
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
#
# @param src [String] image source path
# @param options [Hash] hash of options
def image(src, options = {})
return unless src
pdf.image(src, options)
end
# Output to the PDF document
#
# @param buffer [Array] array of text items
# @param options [Hash] hash of options
# @param bounding_box [Array] bounding box arguments, if bounded
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
#
# @param x1 [Float] left position of the line
# @param x2 [Float] right position of the line
# @param y [Float] vertical position of the line
def underline(x1:, x2:, y:)
pdf.stroke do
pdf.line [x1, y], [x2, y]
end
end
private
attr_reader :pdf
def output_buffer(buffer, options, left_indent:)
formatted_text = proc { pdf.formatted_text(buffer, options) }
return formatted_text.call if left_indent == 0
pdf.indent(left_indent, 0, &formatted_text)
end
end
end