Module: ZplRenderer

Defined in:
lib/zpl_renderer.rb,
lib/zpl_renderer/binary.rb,
lib/zpl_renderer/engine.rb,
lib/zpl_renderer/errors.rb,
lib/zpl_renderer/decoder.rb,
lib/zpl_renderer/options.rb,
lib/zpl_renderer/version.rb,
lib/zpl_renderer/renderer.rb,
lib/zpl_renderer/sanitizer.rb,
lib/zpl_renderer/validation.rb

Overview

Offline ZPL to PNG/PDF converter for Ruby and Rails applications.

png = ZplRenderer.render(zpl, format: :png, width: 4, height: 6, dpi: 203)
ZplRenderer.render_file(zpl, "label.pdf", format: :pdf, dpi: 300)

Defined Under Namespace

Modules: Binary Classes: BinaryMissingError, ConfigurationError, Decoder, Engine, Error, InvalidBarcodeError, Options, OutputTooLargeError, RenderError, Renderer, Sanitizer, TimeoutError, UnsupportedCommandError, Validation, ValidationError

Constant Summary collapse

VERSION =
"0.1.2"

Class Method Summary collapse

Class Method Details

.content_type(format) ⇒ Object

Content-Type helper for Rails send_data responses.



68
69
70
# File 'lib/zpl_renderer.rb', line 68

def content_type(format)
  Options.new(format: format).content_type
end

.decode_png(png_bytes) ⇒ Array<ZplRenderer::Decoder::Hit>

Decode barcodes/QR codes from a rendered PNG using an independent ZXing engine.

Returns:



75
76
77
# File 'lib/zpl_renderer.rb', line 75

def decode_png(png_bytes)
  Decoder.new.decode_png(png_bytes)
end

.render(zpl, **kwargs) ⇒ String

Render ZPL to binary PNG or PDF bytes.

Parameters:

  • zpl (String)

    raw ZPL label data

  • format (Symbol)

    :png or :pdf

  • width (Numeric)

    label width (inches by default)

  • height (Numeric)

    label height (inches by default)

  • dpi (Integer)

    203, 300, or 600

  • dpmm (Integer, nil)

    8, 12, or 24 (overrides dpi when set)

  • index (Integer)

    label index when multiple ^XA...^XZ blocks exist

  • timeout (Numeric)

    render timeout in seconds

  • max_output_bytes (Integer)

    hard cap on output size

  • strict (Boolean)

    raise on unsupported commands

  • unit (Symbol)

    :inches or :mm

  • clip_to_print_width (Boolean)

    retain clipping from a narrow ^PW

Returns:

  • (String)

    binary PNG or PDF data



35
36
37
# File 'lib/zpl_renderer.rb', line 35

def render(zpl, **kwargs)
  default_renderer.render(zpl, **kwargs)
end

.render_file(zpl, path, **kwargs) ⇒ String

Render ZPL and write the result to path.

Returns:

  • (String)

    the output path



42
43
44
# File 'lib/zpl_renderer.rb', line 42

def render_file(zpl, path, **kwargs)
  default_renderer.render_file(zpl, path, **kwargs)
end

.sanitize(zpl, canvas_dots: nil) ⇒ ZplRenderer::Sanitizer::Result

Sanitize carrier ZPL quirks (e.g. ^FO464,-- and narrow ^PW).

Parameters:

  • canvas_dots (Integer, nil) (defaults to: nil)

    widen ^PW up to this canvas width

Returns:



63
64
65
# File 'lib/zpl_renderer.rb', line 63

def sanitize(zpl, canvas_dots: nil)
  Sanitizer.sanitize(zpl, canvas_dots: canvas_dots)
end

.validate(zpl, **kwargs) ⇒ ZplRenderer::Validation::Result

Validate ZPL without rendering.



49
50
51
52
53
54
55
56
57
# File 'lib/zpl_renderer.rb', line 49

def validate(zpl, **kwargs)
  options = Options.new(**kwargs)
  sanitized = Sanitizer.sanitize(zpl, canvas_dots: options.sanitize_canvas_dots)
  result = Validation.new(sanitized.zpl, options).call
  sanitized.repairs.each do |repair|
    result.warnings << "sanitized #{repair[:from]} -> #{repair[:to]} (#{repair[:reason]})"
  end
  result
end