Class: InvoicePrinter::PDFDocument
- Inherits:
-
Object
- Object
- InvoicePrinter::PDFDocument
- Defined in:
- lib/invoice_printer/pdf_document.rb
Overview
Prawn PDF representation of InvoicePrinter::Document
Example:
invoice = InvoicePrinter::Document.new(...)
invoice_pdf = InvoicePrinter::PDFDocument.new(
document: invoice,
labels: {},
font: 'font.ttf',
bold_font: 'bold_font.ttf',
stamp: 'stamp.jpg',
logo: 'example.jpg',
qr: 'qr.png'
)
Defined Under Namespace
Classes: FontFileNotFound, InvalidInput, LogoFileNotFound, PageSize, QRFileNotFound, StampFileNotFound
Constant Summary collapse
- DEFAULT_LABELS =
{ name: 'Invoice', provider: 'Provider', purchaser: 'Purchaser', tax_id: 'Identification number', tax_id2: 'Identification number', payment: 'Payment', payment_by_transfer: 'Payment by bank transfer on the account below:', payment_in_cash: 'Payment in cash', account_number: 'Account NO', swift: 'SWIFT', iban: 'IBAN', issue_date: 'Issue date', due_date: 'Due date', variable_symbol: 'Variable symbol', item: 'Item', variable: '', quantity: 'Quantity', unit: 'Unit', price_per_item: 'Price per item', tax: 'Tax', tax2: 'Tax 2', tax3: 'Tax 3', amount: 'Amount', subtotal: 'Subtotal', total: 'Total', sublabels: {} }
- PAGE_SIZES =
{ letter: PageSize.new('LETTER', 612.00, 792.00), a4: PageSize.new('A4', 595.28, 841.89), }
Instance Attribute Summary collapse
-
#bold_font ⇒ Object
readonly
Returns the value of attribute bold_font.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#font ⇒ Object
readonly
Returns the value of attribute font.
-
#invoice ⇒ Object
readonly
Returns the value of attribute invoice.
-
#labels ⇒ Object
readonly
Returns the value of attribute labels.
-
#logo ⇒ Object
readonly
Returns the value of attribute logo.
-
#qr ⇒ Object
readonly
Returns the value of attribute qr.
-
#stamp ⇒ Object
readonly
Returns the value of attribute stamp.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(document: Document.new, labels: {}, font: nil, bold_font: nil, stamp: nil, logo: nil, qr: nil, background: nil, page_size: :letter) ⇒ PDFDocument
constructor
A new instance of PDFDocument.
-
#print(file_name = 'invoice.pdf') ⇒ Object
Create PDF file named
file_name. -
#render ⇒ Object
Directly render the PDF.
Constructor Details
#initialize(document: Document.new, labels: {}, font: nil, bold_font: nil, stamp: nil, logo: nil, qr: nil, background: nil, page_size: :letter) ⇒ PDFDocument
Returns a new instance of PDFDocument.
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 |
# File 'lib/invoice_printer/pdf_document.rb', line 72 def initialize( document: Document.new, labels: {}, font: nil, bold_font: nil, stamp: nil, logo: nil, qr: nil, background: nil, page_size: :letter ) @document = document @labels = merge_custom_labels(labels) @font = font @bold_font = bold_font @stamp = stamp @logo = logo @qr = qr @page_size = page_size ? PAGE_SIZES[page_size.to_sym] : PAGE_SIZES[:letter] @pdf = Prawn::Document.new(background: background, page_size: @page_size.name) raise InvalidInput, 'document is not a type of InvoicePrinter::Document' \ unless @document.is_a?(InvoicePrinter::Document) if used? @logo if File.exist?(@logo) @logo = logo else raise LogoFileNotFound, "Logotype file not found at #{@logo}" end end if used? @stamp if File.exist?(@stamp) @stamp = stamp else raise StampFileNotFound, "Stamp file not found at #{@stamp}" end end if used? @qr if File.exist?(@qr) @qr = qr else raise QRFileNotFound, "QR image file not found at #{@qr}" end end if used?(@font) && used?(@bold_font) use_font(@font, @bold_font) elsif used?(@font) use_font(@font, @font) end build_pdf end |
Instance Attribute Details
#bold_font ⇒ Object (readonly)
Returns the value of attribute bold_font.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def bold_font @bold_font end |
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def file_name @file_name end |
#font ⇒ Object (readonly)
Returns the value of attribute font.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def font @font end |
#invoice ⇒ Object (readonly)
Returns the value of attribute invoice.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def invoice @invoice end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def labels @labels end |
#logo ⇒ Object (readonly)
Returns the value of attribute logo.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def logo @logo end |
#qr ⇒ Object (readonly)
Returns the value of attribute qr.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def qr @qr end |
#stamp ⇒ Object (readonly)
Returns the value of attribute stamp.
26 27 28 |
# File 'lib/invoice_printer/pdf_document.rb', line 26 def stamp @stamp end |
Class Method Details
.labels ⇒ Object
64 65 66 |
# File 'lib/invoice_printer/pdf_document.rb', line 64 def self.labels @@labels ||= DEFAULT_LABELS end |
.labels=(labels) ⇒ Object
68 69 70 |
# File 'lib/invoice_printer/pdf_document.rb', line 68 def self.labels=(labels) @@labels = DEFAULT_LABELS.merge(labels) end |
Instance Method Details
#print(file_name = 'invoice.pdf') ⇒ Object
Create PDF file named file_name
130 131 132 |
# File 'lib/invoice_printer/pdf_document.rb', line 130 def print(file_name = 'invoice.pdf') @pdf.render_file file_name end |
#render ⇒ Object
Directly render the PDF
135 136 137 |
# File 'lib/invoice_printer/pdf_document.rb', line 135 def render @pdf.render end |