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',
logo: 'example.jpg'
)
Defined Under Namespace
Classes: FontFileNotFound, InvalidInput, LogoFileNotFound
Constant Summary collapse
- DEFAULT_LABELS =
{ name: 'Invoice', provider: 'Provider', purchaser: 'Purchaser', ic: 'Identification number', dic: '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.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(document: Document.new, labels: {}, font: nil, logo: 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, logo: nil) ⇒ PDFDocument
Returns a new instance of PDFDocument.
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 |
# File 'lib/invoice_printer/pdf_document.rb', line 57 def initialize(document: Document.new, labels: {}, font: nil, logo: nil) @document = document @labels = PDFDocument.labels.merge(labels) @pdf = Prawn::Document.new @font = font @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.
21 22 23 |
# File 'lib/invoice_printer/pdf_document.rb', line 21 def file_name @file_name end |
#font ⇒ Object (readonly)
Returns the value of attribute font.
21 22 23 |
# File 'lib/invoice_printer/pdf_document.rb', line 21 def font @font end |
#invoice ⇒ Object (readonly)
Returns the value of attribute invoice.
21 22 23 |
# File 'lib/invoice_printer/pdf_document.rb', line 21 def invoice @invoice end |
#labels ⇒ Object (readonly)
Returns the value of attribute labels.
21 22 23 |
# File 'lib/invoice_printer/pdf_document.rb', line 21 def labels @labels end |
#logo ⇒ Object (readonly)
Returns the value of attribute logo.
21 22 23 |
# File 'lib/invoice_printer/pdf_document.rb', line 21 def logo @logo end |
Class Method Details
.labels ⇒ Object
49 50 51 |
# File 'lib/invoice_printer/pdf_document.rb', line 49 def self.labels @@labels ||= DEFAULT_LABELS end |
.labels=(labels) ⇒ Object
53 54 55 |
# File 'lib/invoice_printer/pdf_document.rb', line 53 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
87 88 89 |
# File 'lib/invoice_printer/pdf_document.rb', line 87 def print(file_name = 'invoice.pdf') @pdf.render_file file_name end |
#render ⇒ Object
Directly render the PDF
92 93 94 |
# File 'lib/invoice_printer/pdf_document.rb', line 92 def render @pdf.render end |