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',
  bold_font: 'bold_font.ttf',
  stamp: 'stamp.jpg',
  logo: 'example.jpg'
)

Defined Under Namespace

Classes: FontFileNotFound, InvalidInput, LogoFileNotFound, PageSize, 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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of PDFDocument.

Raises:



70
71
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
# File 'lib/invoice_printer/pdf_document.rb', line 70

def initialize(
  document: Document.new,
  labels: {},
  font: nil,
  bold_font: nil,
  stamp: nil,
  logo: nil,
  background: nil,
  page_size: :letter
)
  @document  = document
  @labels    = merge_custom_labels(labels)
  @font      = font
  @bold_font = bold_font
  @stamp     = stamp
  @logo      = 
  @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 = 
    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?(@font) && used?(@bold_font)
    use_font(@font, @bold_font)
  elsif used?(@font)
    use_font(@font, @font)
  end

  build_pdf
end

Instance Attribute Details

#bold_fontObject (readonly)

Returns the value of attribute bold_font.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def bold_font
  @bold_font
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def file_name
  @file_name
end

#fontObject (readonly)

Returns the value of attribute font.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def font
  @font
end

#invoiceObject (readonly)

Returns the value of attribute invoice.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def invoice
  @invoice
end

#labelsObject (readonly)

Returns the value of attribute labels.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def labels
  @labels
end

#logoObject (readonly)

Returns the value of attribute logo.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def 
  @logo
end

#stampObject (readonly)

Returns the value of attribute stamp.



24
25
26
# File 'lib/invoice_printer/pdf_document.rb', line 24

def stamp
  @stamp
end

Class Method Details

.labelsObject



62
63
64
# File 'lib/invoice_printer/pdf_document.rb', line 62

def self.labels
  @@labels ||= DEFAULT_LABELS
end

.labels=(labels) ⇒ Object



66
67
68
# File 'lib/invoice_printer/pdf_document.rb', line 66

def self.labels=(labels)
  @@labels = DEFAULT_LABELS.merge(labels)
end

Instance Method Details

Create PDF file named file_name



118
119
120
# File 'lib/invoice_printer/pdf_document.rb', line 118

def print(file_name = 'invoice.pdf')
  @pdf.render_file file_name
end

#renderObject

Directly render the PDF



123
124
125
# File 'lib/invoice_printer/pdf_document.rb', line 123

def render
  @pdf.render
end