HexaPDF Extras

class HexaPDF::Extras::GraphicObject::Zint

Generates a barcode using the ruby-zint library that uses the libzint barcode generation library.

It implements the HexaPDF graphic object interface and can therefore easily be used via the :barcode name:

canvas.draw(:barcode, width: 50, at: [10, 40], value: 'Hello!', symbology: :code128)

Except for a few keyword arguments all are passed through to ruby-zint, so everything that is supported by Zint::Barcode can be used. To make specifying symbologies easier, it is possible to use symbolic names instead of the constants, see configure.

Examples

  • Linear barcode

    canvas.draw(:barcode, width: 60, at: [20, 45], value: '1123456', symbology: :upce)
    canvas.draw(:barcode, width: 60, at: [20, 5], value: 'Hello!', symbology: :code128)
    

  • Stacked barcode

    canvas.draw(:barcode, width: 80, at: [10, 40], symbology: :codablockf,
                value: 'Hello HexaPDF!', option_1: 3)
    

  • Composite barcode

    canvas.draw(:barcode, width: 80, at: [10, 20], symbology: :gs1_128_cc,
                value: '[99]1234-abcd', primary: "[01]03312345678903", option_1: 3)
    

  • 2D barcode

    canvas.draw(:barcode, width: 80, at: [10, 10], symbology: :datamatrix,
                value: 'Hello HexaPDF!', option_3: 100, output_options: 0x0100)
    

Constants

COLOR_CODES

Maps the Zint color codes to HexaPDF color names.

Attributes

at[RW]

The position of the bottom-left corner of the barcode.

Default: [0, 0].

Examples:

canvas.draw(:barcode, height: 50, value: 'test', symbology: :code128)
canvas.draw(:barcode, height: 50, value: 'test', symbology: :code128, at: [20, 50])

font[RW]

The font used when outputting strings.

Any font that is supported by the HexaPDF::Document::Layout module is supported.

Default: ‘Helvetica’.

Examples:

canvas.draw(:barcode, height: 50, font: 'Courier', value: 'test', symbology: :code128)

height[RW]

The height of the barcode.

For details and examples see width.

Default: nil.

width[RW]

The width of resulting barcode.

The resulting size of the barcode depends on whether width and height are set:

  • If neither width nor height are set, the barcode uses the size returned by ruby-zint.

  • If both are set, the barcode is fit exactly into the given rectangle.

  • If either width or height is set, the other dimension is based on the set dimension so that the original aspect ratio is maintained.

Default: nil.

Examples:

  • No dimension set

    canvas.draw(:barcode, value: 'test', symbology: :code128)
    

  • One dimension set

    canvas.draw(:barcode, width: 60, value: 'test', symbology: :code128)
    canvas.draw(:barcode, height: 50, value: 'test', symbology: :code128, at: [0, 50])
    

  • Both dimensions set

    canvas.draw(:barcode, width: 60, height: 60, value: 'test', symbology: :code128)
    

zint_kws[RW]

The keyword arguments that are passed on to Zint::Barcode.new.

Default: {}.

Public Class Methods

configure(**kwargs)

Creates and configures a new Zint drawing support object.

See configure for the allowed keyword arguments.

new()

Creates a Zint graphic object.

Public Instance Methods

configure(at: nil, width: nil, height: nil, font: nil, symbology: nil, **zint_kws)

Configures the Zint graphic object and returns self.

The following arguments are allowed:

:at

The position of the bottom-left corner (see at).

:width

The width of the barcode (see width).

:height

The height of the barcode (see height).

:font

The font to use when outputting strings (see font).

:symbology

The type of barcode. Supports using symbols instead of constants, e.g. :code128 instead of Zint::BARCODE_CODE128.

:zint_kws

Keyword arguments that are passed on to ruby-zint.

Any arguments not specified are not modified and retain their old value, see the attribute methods for the inital default values.

draw(canvas)

Draws the Zint::Barcode object onto the given canvas, with the bottom-left corner at the position specified by at and the size specified by width and height.

form_xobject(document)

Creates a Form XObject for the given HexaPDF::Document that contains the visual representation of the barcode.