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, 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',
  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, stamp: nil, logo: nil, background: nil, page_size: :letter) ⇒ PDFDocument

Returns a new instance of PDFDocument.

Raises:


68
69
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/invoice_printer/pdf_document.rb', line 68

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

  # Version 2.1 deprecation warnings
  warnings = [
    @document.provider_street,
    @document.provider_street_number,
    @document.provider_postcode,
    @document.provider_city,
    @document.provider_city_part,
    @document.provider_extra_address_line,
    @document.purchaser_street,
    @document.purchaser_street_number,
    @document.purchaser_postcode,
    @document.purchaser_city,
    @document.purchaser_city_part,
    @document.purchaser_extra_address_line
  ].delete_if(&:empty?)

  unless warnings.empty?
    warning = <<~WARN
      WARNING: Following values are used in deprecated fields and
      won't be rendered in future versions of InvoicePrinter:

      #{warnings.join(", ")}

      Use new provider_lines and purchaser_lines fields instead of
      the old address fields.
    WARN

    $stderr.puts warning
  end

  build_pdf
end

Instance Attribute Details

#file_nameObject (readonly)

Returns the value of attribute file_name


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

def file_name
  @file_name
end

#fontObject (readonly)

Returns the value of attribute font


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

def font
  @font
end

#invoiceObject (readonly)

Returns the value of attribute invoice


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

def invoice
  @invoice
end

#labelsObject (readonly)

Returns the value of attribute labels


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

def labels
  @labels
end

#logoObject (readonly)

Returns the value of attribute logo


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

def 
  @logo
end

#stampObject (readonly)

Returns the value of attribute stamp


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

def stamp
  @stamp
end

Class Method Details

.labelsObject


60
61
62
# File 'lib/invoice_printer/pdf_document.rb', line 60

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

.labels=(labels) ⇒ Object


64
65
66
# File 'lib/invoice_printer/pdf_document.rb', line 64

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

Instance Method Details

Create PDF file named file_name


134
135
136
# File 'lib/invoice_printer/pdf_document.rb', line 134

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

#renderObject

Directly render the PDF


139
140
141
# File 'lib/invoice_printer/pdf_document.rb', line 139

def render
  @pdf.render
end