Class: InvoicePrinter::PDFDocument

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(document: Document.new, labels: {}, font: nil, stamp: nil, logo: nil, background: nil) ⇒ PDFDocument

Returns a new instance of PDFDocument.

Raises:



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 = 

  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 = 
    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_nameObject (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

#fontObject (readonly)

Returns the value of attribute font.



22
23
24
# File 'lib/invoice_printer/pdf_document.rb', line 22

def font
  @font
end

#invoiceObject (readonly)

Returns the value of attribute invoice.



22
23
24
# File 'lib/invoice_printer/pdf_document.rb', line 22

def invoice
  @invoice
end

#labelsObject (readonly)

Returns the value of attribute labels.



22
23
24
# File 'lib/invoice_printer/pdf_document.rb', line 22

def labels
  @labels
end

#logoObject (readonly)

Returns the value of attribute logo.



22
23
24
# File 'lib/invoice_printer/pdf_document.rb', line 22

def 
  @logo
end

#stampObject (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

.labelsObject



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

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

#renderObject

Directly render the PDF



94
95
96
# File 'lib/invoice_printer/pdf_document.rb', line 94

def render
  @pdf.render
end