Class: Payday::PdfRenderer
- Inherits:
-
Object
- Object
- Payday::PdfRenderer
- Defined in:
- lib/payday/pdf_renderer.rb
Overview
The PDF renderer. We use this internally in Payday to render pdfs, but really you should just need to call {Payday{Payday::Invoiceable{Payday::Invoiceable#render_pdf} to render pdfs yourself.
Class Method Summary collapse
-
.bill_to_ship_to(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength.
- .bold_cell(pdf, text, options = {}) ⇒ Object
- .cell(pdf, text, options = {}) ⇒ Object
-
.company_banner(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength.
-
.invoice_details(invoice, pdf) ⇒ Object
rubocop:todo Metrics/PerceivedComplexity rubocop:todo Metrics/MethodLength rubocop:todo Metrics/AbcSize.
- .invoice_or_default(invoice, property) ⇒ Object
-
.line_items_table(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength.
- .max_cell_width(cell_proxy) ⇒ Object
-
.notes(invoice, pdf) ⇒ Object
rubocop:enable Metrics/MethodLength.
-
.number_to_currency(number, invoice) ⇒ Object
Converts this number to a formatted currency string.
- .page_numbers(pdf) ⇒ Object
-
.pdf(invoice) ⇒ Object
rubocop:todo Metrics/MethodLength, Metrics/AbcSize.
-
.render(invoice) ⇒ Object
Renders the given invoice as a pdf, returning a string.
- .render_qr_code(invoice, pdf) ⇒ Object
-
.render_to_file(invoice, path) ⇒ Object
Renders the given invoice as a pdf on disk.
-
.stamp(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength.
-
.totals_lines(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength.
Class Method Details
.bill_to_ship_to(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength
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 |
# File 'lib/payday/pdf_renderer.rb', line 107 def self.bill_to_ship_to(invoice, pdf) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength bill_to_cell_style = {borders: [], padding: [2, 0]} bill_to_ship_to_bottom = 0 # render bill to pdf.float do pdf.table([[bold_cell(pdf, I18n.t('payday.invoice.bill_to', default: 'Bill To'))], [invoice.bill_to]], column_widths: [200], cell_style: bill_to_cell_style) bill_to_ship_to_bottom = pdf.cursor end # render ship to if defined?(invoice.ship_to) && !invoice.ship_to.nil? table = pdf.make_table([[bold_cell(pdf, I18n.t('payday.invoice.ship_to', default: 'Ship To'))], [invoice.ship_to]], column_widths: [200], cell_style: bill_to_cell_style) pdf.bounding_box([pdf.bounds.width - table.width, pdf.cursor], width: table.width, height: table.height + 2) do table.draw end end # make sure we start at the lower of the bill_to or ship_to details bill_to_ship_to_bottom = pdf.cursor if pdf.cursor < bill_to_ship_to_bottom pdf.move_cursor_to(bill_to_ship_to_bottom - 20) end |
.bold_cell(pdf, text, options = {}) ⇒ Object
318 319 320 |
# File 'lib/payday/pdf_renderer.rb', line 318 def self.bold_cell(pdf, text, = {}) cell(pdf, "<b>#{text}</b>", .merge(inline_format: true)) end |
.cell(pdf, text, options = {}) ⇒ Object
314 315 316 |
# File 'lib/payday/pdf_renderer.rb', line 314 def self.cell(pdf, text, = {}) Prawn::Table::Cell::Text.make(pdf, text, ) end |
.company_banner(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength
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 |
# File 'lib/payday/pdf_renderer.rb', line 69 def self.(invoice, pdf) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength # render the logo image = invoice_or_default(invoice, :invoice_logo) height = nil width = nil # Handle images defined with a hash of options if image.is_a?(Hash) data = image image = data[:filename] width, height = data[:size].split('x').map(&:to_f) end if File.extname(image) == '.svg' logo_info = pdf.svg(File.read(image), at: pdf.bounds.top_left, width: width, height: height) logo_height = logo_info[:height] else logo_info = pdf.image(image, at: pdf.bounds.top_left, width: width, height: height) logo_height = logo_info.scaled_height end # render the company details table_data = [] table_data << [bold_cell(pdf, invoice_or_default(invoice, :company_name).strip, size: 12)] invoice_or_default(invoice, :company_details).lines.each { |line| table_data << [line] } table = pdf.make_table(table_data, cell_style: {borders: [], padding: 0}) pdf.bounding_box([pdf.bounds.width - table.width, pdf.bounds.top], width: table.width, height: table.height + 5) do table.draw end pdf.move_cursor_to(pdf.bounds.top - logo_height - 20) end |
.invoice_details(invoice, pdf) ⇒ Object
rubocop:todo Metrics/PerceivedComplexity rubocop:todo Metrics/MethodLength rubocop:todo Metrics/AbcSize
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/payday/pdf_renderer.rb', line 137 def self.invoice_details(invoice, pdf) # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity # invoice details table_data = [] # invoice number if defined?(invoice.invoice_number) && invoice.invoice_number table_data << if invoice.paid? [bold_cell(pdf, I18n.t('payday.invoice.receipt_no', default: 'Receipt #:')), bold_cell(pdf, invoice.invoice_number.to_s, align: :right)] else [bold_cell(pdf, I18n.t('payday.invoice.invoice_no', default: 'Invoice #:')), bold_cell(pdf, invoice.invoice_number.to_s, align: :right)] end end # Due on if defined?(invoice.due_at) && invoice.due_at due_date = if invoice.due_at.is_a?(Date) || invoice.due_at.is_a?(Time) invoice.due_at.strftime(Payday::Config.default.date_format) else invoice.due_at.to_s end table_data << [bold_cell(pdf, I18n.t('payday.invoice.due_date', default: 'Due Date:')), bold_cell(pdf, due_date, align: :right)] end # Paid on if defined?(invoice.paid_at) && invoice.paid_at paid_date = if invoice.paid_at.is_a?(Date) || invoice.due_at.is_a?(Time) invoice.paid_at.strftime(Payday::Config.default.date_format) else invoice.paid_at.to_s end table_data << [bold_cell(pdf, I18n.t('payday.invoice.paid_date', default: 'Paid Date:')), bold_cell(pdf, paid_date, align: :right)] end # loop through invoice_details and include them invoice.each_detail do |key, value| table_data << [bold_cell(pdf, key), bold_cell(pdf, value, align: :right)] end return unless table_data.length.positive? pdf.table(table_data, cell_style: {borders: [], padding: [1, 10, 1, 1]}) end |
.invoice_or_default(invoice, property) ⇒ Object
306 307 308 309 310 311 312 |
# File 'lib/payday/pdf_renderer.rb', line 306 def self.invoice_or_default(invoice, property) if invoice.respond_to?(property) && invoice.send(property) invoice.send(property) else Payday::Config.default.send(property) end end |
.line_items_table(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/payday/pdf_renderer.rb', line 191 def self.line_items_table(invoice, pdf) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength table_data = [] table_data << [bold_cell(pdf, I18n.t('payday.line_item.description', default: 'Description'), borders: []), bold_cell(pdf, I18n.t('payday.line_item.unit_price', default: 'Unit Price'), align: :center, borders: []), bold_cell(pdf, I18n.t('payday.line_item.quantity', default: 'Quantity'), align: :center, borders: []), bold_cell(pdf, I18n.t('payday.line_item.amount', default: 'Amount'), align: :center, borders: [])] invoice.line_items.each do |line| table_data << if line.predefined_amount [line.description, '', '', number_to_currency(line.predefined_amount, invoice)] else [line.description, line.display_price || number_to_currency(line.price, invoice), line.display_quantity || BigDecimal(line.quantity.to_s).to_s('F'), number_to_currency(line.amount, invoice)] end end pdf.move_cursor_to(pdf.cursor - 20) pdf.table(table_data, width: pdf.bounds.width, header: true, cell_style: {border_width: 0.5, border_left_color: 'FFFFFF', border_right_color: 'FFFFFF', border_top_color: 'F6F9FC', border_bottom_color: 'BCC6D0', padding: [5, 10], inline_format: true}, row_colors: %w[F6F9FC ffffff]) do # left align the number columns columns(1..3).rows(1..(row_length - 1)).style(align: :right) # set the column widths correctly natural = natural_column_widths natural[0] = width - natural[1] - natural[2] - natural[3] end end |
.max_cell_width(cell_proxy) ⇒ Object
332 333 334 335 336 337 338 339 |
# File 'lib/payday/pdf_renderer.rb', line 332 def self.max_cell_width(cell_proxy) max = 0 cell_proxy.each do |cell| max = cell.natural_content_width if cell.natural_content_width > max end max end |
.notes(invoice, pdf) ⇒ Object
rubocop:enable Metrics/MethodLength
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/payday/pdf_renderer.rb', line 272 def self.notes(invoice, pdf) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength return unless defined?(invoice.notes) && invoice.notes pdf.move_cursor_to(pdf.cursor - 30) pdf.font('NotoSans') do pdf.text(I18n.t('payday.invoice.notes', default: 'Notes'), style: :bold) end pdf.line_width = 0.5 pdf.stroke_color = 'cccccc' pdf.stroke_line([0, pdf.cursor - 3, pdf.bounds.width, pdf.cursor - 3]) pdf.move_cursor_to(pdf.cursor - 10) pdf.text(invoice.notes.to_s, inline_format: true) render_qr_code(invoice, pdf) end |
.number_to_currency(number, invoice) ⇒ Object
Converts this number to a formatted currency string
323 324 325 326 327 328 329 330 |
# File 'lib/payday/pdf_renderer.rb', line 323 def self.number_to_currency(number, invoice) Money.locale_backend = :currency Money.rounding_mode = BigDecimal::ROUND_HALF_UP currency = Money::Currency.wrap(invoice_or_default(invoice, :currency)) number *= currency.subunit_to_unit number = number.round unless Money.default_infinite_precision Money.new(number, currency).format end |
.page_numbers(pdf) ⇒ Object
300 301 302 303 304 |
# File 'lib/payday/pdf_renderer.rb', line 300 def self.page_numbers(pdf) return unless pdf.page_count > 1 pdf.number_pages('<page> / <total>', at: [pdf.bounds.right - 25, -10]) end |
.pdf(invoice) ⇒ Object
rubocop:todo Metrics/MethodLength, Metrics/AbcSize
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 |
# File 'lib/payday/pdf_renderer.rb', line 19 def self.pdf(invoice) # rubocop:todo Metrics/MethodLength, Metrics/AbcSize pdf = Prawn::Document.new(page_size: invoice_or_default(invoice, :page_size)) font_dir = File.join(File.dirname(__dir__), '..', 'fonts') pdf.font_families.update( 'NotoSans' => {normal: File.join(font_dir, 'NotoSans-Regular.ttf'), bold: File.join(font_dir, 'NotoSans-Bold.ttf')} ) # set up some default styling pdf.font_size(10) pdf.font 'NotoSans' stamp(invoice, pdf) (invoice, pdf) bill_to_ship_to(invoice, pdf) invoice_details(invoice, pdf) line_items_table(invoice, pdf) totals_lines(invoice, pdf) notes(invoice, pdf) render_qr_code(invoice, pdf) unless defined?(invoice.notes) && invoice.notes page_numbers(pdf) pdf end |
.render(invoice) ⇒ Object
Renders the given invoice as a pdf, returning a string
15 16 17 |
# File 'lib/payday/pdf_renderer.rb', line 15 def self.render(invoice) pdf(invoice).render end |
.render_qr_code(invoice, pdf) ⇒ Object
288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/payday/pdf_renderer.rb', line 288 def self.render_qr_code(invoice, pdf) return unless defined?(invoice.qr_code) && invoice.qr_code.to_s.strip.present? require 'rqrcode' pdf.move_cursor_to(pdf.cursor - 10) qr = RQRCode::QRCode.new(invoice.qr_code.to_s) png = qr.as_png(size: 200) pdf.image(StringIO.new(png.to_s), width: 100, position: :left) end |
.render_to_file(invoice, path) ⇒ Object
Renders the given invoice as a pdf on disk
10 11 12 |
# File 'lib/payday/pdf_renderer.rb', line 10 def self.render_to_file(invoice, path) pdf(invoice).render_file(path) end |
.stamp(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/payday/pdf_renderer.rb', line 46 def self.stamp(invoice, pdf) # rubocop:todo Metrics/MethodLength stamp = nil if invoice.refunded? stamp = I18n.t 'payday.status.refunded', default: 'REFUNDED' elsif invoice.paid? stamp = I18n.t 'payday.status.paid', default: 'PAID' elsif invoice.overdue? stamp = I18n.t 'payday.status.overdue', default: 'OVERDUE' end if stamp pdf.bounding_box([150, pdf.cursor - 50], width: pdf.bounds.width - 300) do pdf.font('NotoSans') do pdf.fill_color 'cc0000' pdf.text stamp, align: :center, size: 25, rotate: 15, style: :bold end end end pdf.fill_color '000000' end |
.totals_lines(invoice, pdf) ⇒ Object
rubocop:todo Metrics/MethodLength
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/payday/pdf_renderer.rb', line 227 def self.totals_lines(invoice, pdf) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength table_data = [] table_data << [ bold_cell(pdf, I18n.t('payday.invoice.subtotal', default: 'Subtotal:')), cell(pdf, number_to_currency(invoice.subtotal, invoice), align: :right) ] tax_description = if invoice.tax_description.nil? I18n.t('payday.invoice.tax', default: 'Tax:') else invoice.tax_description end table_data << [ bold_cell(pdf, tax_description), cell(pdf, number_to_currency(invoice.tax, invoice), align: :right) ] if invoice.shipping_rate.positive? shipping_description = if invoice.shipping_description.nil? I18n.t('payday.invoice.shipping', default: 'Shipping:') else invoice.shipping_description end table_data << [ bold_cell(pdf, shipping_description), cell(pdf, number_to_currency(invoice.shipping, invoice), align: :right) ] end table_data << [ bold_cell(pdf, I18n.t('payday.invoice.total', default: 'Total:'), size: 12), cell(pdf, number_to_currency(invoice.total, invoice), size: 12, align: :right) ] table = pdf.make_table(table_data, cell_style: {borders: []}) pdf.bounding_box([pdf.bounds.width - table.width, pdf.cursor], width: table.width, height: table.height + 2) do table.draw end end |