Module: ZplRender::Output::Pdf

Defined in:
lib/zpl_render/output/pdf.rb

Class Method Summary collapse

Class Method Details

.encode(canvases, dpmm:) ⇒ Object

Encode one page per label Canvas. Each page has the label's exact physical size (canvas dots / dpmm), and the label raster is placed 1:1 so barcode modules stay sharp when printed.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/zpl_render/output/pdf.rb', line 14

def encode(canvases, dpmm:)
  raise ArgumentError, "no labels to render" if canvases.empty?

  first = canvases.first
  doc = Prawn::Document.new(
    page_size: page_size(first, dpmm),
    margin: 0,
    skip_page_creation: false,
    info: { Creator: "zpl_render #{ZplRender::VERSION}" }
  )
  canvases.each_with_index do |canvas, i|
    doc.start_new_page(size: page_size(canvas, dpmm), margin: 0) if i.positive?
    png = Png.encode(canvas)
    doc.image StringIO.new(png), at: [0, doc.bounds.top],
                                 width: doc.bounds.width, height: doc.bounds.height
  end
  doc.render
end

.page_size(canvas, dpmm) ⇒ Object



33
34
35
36
# File 'lib/zpl_render/output/pdf.rb', line 33

def page_size(canvas, dpmm)
  # dots -> mm -> points (72 pt / inch, 25.4 mm / inch)
  [canvas.width / dpmm.to_f / 25.4 * 72.0, canvas.height / dpmm.to_f / 25.4 * 72.0]
end