Class: Barcodes::Renderer::Pdf

Inherits:
Object
  • Object
show all
Defined in:
lib/barcodes/renderer/pdf.rb

Overview

This class handles PDF rendering support.

Direct Known Subclasses

Image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(barcode = nil) ⇒ Pdf

Creates a new Barcodes::Renderer::Pdf instance



18
19
20
# File 'lib/barcodes/renderer/pdf.rb', line 18

def initialize(barcode=nil)
  @barcode = barcode
end

Instance Attribute Details

#barcodeObject

The barcode instance



15
16
17
# File 'lib/barcodes/renderer/pdf.rb', line 15

def barcode
  @barcode
end

Instance Method Details

#draw(pdf) ⇒ Object

Draws the barcode to the provided Prawn::Document



36
37
38
39
40
41
42
43
44
# File 'lib/barcodes/renderer/pdf.rb', line 36

def draw(pdf)
  if self.barcode.class == Barcodes::Symbology::Ean8 || self.barcode.class == Barcodes::Symbology::Ean13 || self.barcode.class == Barcodes::Symbology::UpcA
    self._draw_ean_upc(self.barcode, pdf)
  elsif self.barcode.class == Barcodes::Symbology::Planet || self.barcode.class == Barcodes::Symbology::Postnet
    self._draw_planet_postnet(self.barcode, pdf)
  else
    self._draw_standard(self.barcode, pdf)
  end
end

#pdfObject

Returns the instantiated Prawn::Document object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/barcodes/renderer/pdf.rb', line 47

def pdf
  width = (@barcode.width * 0.001) * 72.0
  height = (@barcode.height * 0.001) * 72.0
  
  document_options = {
    :page_size => [width, height],
    :left_margin => 0,
    :right_margin => 0,
    :top_margin => 0,
    :bottom_margin => 0
  }
  
  Prawn::Document.new document_options
end

#render(filename = nil) ⇒ Object

Render the barcode as PDF with optional filename



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/barcodes/renderer/pdf.rb', line 23

def render(filename=nil)
  pdf = self.pdf
  
  self.draw(pdf)
  
  unless filename.nil?
    pdf.render_file filename
  else
    pdf.render
  end
end