Class: Payday::PdfRenderer
- Inherits:
-
Object
- Object
- Payday::PdfRenderer
- Defined in:
- lib/payday/pdf_renderer.rb
Overview
Renders an invoice to PDF by feeding a presented Hash to the Typst template.
Invoice data crosses into the template only as sys_inputs JSON, where Typst treats every string as literal text. Never interpolate invoice data into the template source.
Constant Summary collapse
- TEMPLATE =
File.('templates/invoice.typ', __dir__)
- FONT_DIR =
File.('../../fonts', __dir__)
- FONTS =
%w[NotoSans-Regular.ttf NotoSans-Bold.ttf].freeze
Class Method Summary collapse
-
.number_to_currency(number, invoice) ⇒ Object
Converts this number to a formatted currency string.
-
.render(invoice, appendix: nil) ⇒ Object
Renders the given invoice as a pdf, returning a string.
-
.render_to_file(invoice, path, appendix: nil) ⇒ Object
Renders the given invoice as a pdf on disk.
Instance Method Summary collapse
-
#initialize(invoice, appendix: nil) ⇒ PdfRenderer
constructor
A new instance of PdfRenderer.
- #render ⇒ Object
Constructor Details
#initialize(invoice, appendix: nil) ⇒ PdfRenderer
Returns a new instance of PdfRenderer.
43 44 45 46 47 |
# File 'lib/payday/pdf_renderer.rb', line 43 def initialize(invoice, appendix: nil) @invoice = invoice @appendix = Appendix.wrap(appendix) @dependencies = {} end |
Class Method Details
.number_to_currency(number, invoice) ⇒ Object
Converts this number to a formatted currency string
27 28 29 30 31 32 33 34 |
# File 'lib/payday/pdf_renderer.rb', line 27 def self.number_to_currency(number, invoice) Money.locale_backend = :currency Money.rounding_mode = BigDecimal::ROUND_HALF_UP currency = Money::Currency.wrap(currency_for(invoice)) number *= currency.subunit_to_unit number = number.round unless Money.default_infinite_precision Money.new(number, currency).format end |
.render(invoice, appendix: nil) ⇒ Object
Renders the given invoice as a pdf, returning a string. See Payday::Appendix for appending pages of your own to it.
22 23 24 |
# File 'lib/payday/pdf_renderer.rb', line 22 def self.render(invoice, appendix: nil) new(invoice, appendix: appendix).render end |
.render_to_file(invoice, path, appendix: nil) ⇒ Object
Renders the given invoice as a pdf on disk
16 17 18 |
# File 'lib/payday/pdf_renderer.rb', line 16 def self.render_to_file(invoice, path, appendix: nil) File.binwrite(path, render(invoice, appendix: appendix)) end |
Instance Method Details
#render ⇒ Object
49 50 51 52 53 54 55 56 |
# File 'lib/payday/pdf_renderer.rb', line 49 def render data = InvoicePresenter.new(@invoice).to_h register_qr_code(data[:qr_code]) data[:logo] = logo Typst(body: body, dependencies: dependencies, fonts: fonts, sys_inputs: sys_inputs(data)) .compile(:pdf).bytes.flatten.pack('C*') end |