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',
stamp: 'stamp.jpg',
logo: 'example.jpg'
)
Defined Under Namespace
Classes: FontFileNotFound, InvalidInput, LogoFileNotFound
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', item: 'Item', quantity: 'Quantity', unit: 'Unit', price_per_item: 'Price per item', tax: 'Tax', tax2: 'Tax 2', tax3: 'Tax 3', amount: 'Amount', subtotal: 'Subtotal', total: 'Total' }
Instance Attribute Summary collapse
-
#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.
-
#stamp ⇒ Object
readonly
Returns the value of attribute stamp.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(document: Document.new, labels: {}, font: nil, stamp: nil, logo: nil, background: nil) ⇒ 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, stamp: nil, logo: nil, background: nil) ⇒ PDFDocument
Returns a new instance of PDFDocument.
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 |
# File 'lib/invoice_printer/pdf_document.rb', line 58 def initialize(document: Document.new, labels: {}, font: nil, stamp: nil, logo: nil, background: nil) @document = document @labels = PDFDocument.labels.merge(labels) @pdf = Prawn::Document.new(background: background) @font = font @stamp = stamp @logo = logo raise InvalidInput, 'document is not a type of InvoicePrinter::Document' \ unless @document.is_a?(InvoicePrinter::Document) if @logo && !@logo.empty? if File.exist?(@logo) @logo = logo else raise LogoFileNotFound, "Logotype file not found at #{@logo}" end end if @font && !@font.empty? if File.exist?(@font) set_fonts(@font) if @font else raise FontFileNotFound, "Font file not found at #{@font}" end end build_pdf end |
Instance Attribute Details
#file_name ⇒ Object (readonly)
Returns the value of attribute file_name.
22 23 24 |
# File 'lib/invoice_printer/pdf_document.rb', line 22 def file_name @file_name end |
#font ⇒ Object (readonly)
Returns the value of attribute font.
22 23 24 |
# File 'lib/invoice_printer/pdf_document.rb', line 22 def font @font end |
#invoice ⇒ Object (readonly)
Returns the value of attribute invoice.
22 23 24 |
# File 'lib/invoice_printer/pdf_document.rb', line 22 def invoice @invoice end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
22 23 24 |
# File 'lib/invoice_printer/pdf_document.rb', line 22 def labels @labels end |
#logo ⇒ Object (readonly)
Returns the value of attribute logo.
22 23 24 |
# File 'lib/invoice_printer/pdf_document.rb', line 22 def logo @logo end |
#stamp ⇒ Object (readonly)
Returns the value of attribute stamp.
22 23 24 |
# File 'lib/invoice_printer/pdf_document.rb', line 22 def stamp @stamp end |
Class Method Details
.labels ⇒ Object
50 51 52 |
# File 'lib/invoice_printer/pdf_document.rb', line 50 def self.labels @@labels ||= DEFAULT_LABELS end |
.labels=(labels) ⇒ Object
54 55 56 |
# File 'lib/invoice_printer/pdf_document.rb', line 54 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
89 90 91 |
# File 'lib/invoice_printer/pdf_document.rb', line 89 def print(file_name = 'invoice.pdf') @pdf.render_file file_name end |
#render ⇒ Object
Directly render the PDF
94 95 96 |
# File 'lib/invoice_printer/pdf_document.rb', line 94 def render @pdf.render end |