Module: Prawn::QRCode

Defined in:
lib/prawn/qrcode.rb,
lib/prawn/qrcode/table.rb,
lib/prawn/qrcode/version.rb,
lib/prawn/qrcode/table/cell.rb

Defined Under Namespace

Modules: Table Classes: QRCodeError, Renderer

Constant Summary collapse

DEFAULT_DOTSIZE =

DEFAULT_DOTSIZE defines the default size for QR Code modules in multiples of 1/72 in

1.to_f
VERSION =
'0.5.2'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dotsize(qr_code, extent, margin = 4) ⇒ Float

dotsize calculates the required dotsize for a QR code to be rendered with the given extent and the module size

Parameters:

  • qr_code (RQRCode::QRCode)

    QR code to render

  • extent (Integer/Float)

    Size of QR code given in pt (1 pt == 1/72 in)

  • margin (Integer) (defaults to: 4)

    Width of margin as number of modules (defaults to 4 modules)

Returns:

  • (Float)

    size of dot in pt (1/72 in)

Since:

  • 0.5.0



61
62
63
# File 'lib/prawn/qrcode.rb', line 61

def self.dotsize(qr_code, extent, margin = 4)
  extent.to_f / (2 * margin + qr_code.modules.length).to_f
end

.min_qrcode(content, qr_version = 0, level: :m, mode: nil) ⇒ RQRCode::QRCode

Creates a QRCode with a minimal size to fit the data with the requested error correction level.

Parameters:

  • content (string)

    The string to render as content of the QR Code

  • qr_version (Integer) (defaults to: 0)

    Optional number of modules to use initially. Will use more if input overflows module size (Default: 0)

  • level (symbol) (defaults to: :m)

    Optional Error correction level to use. One of: (:l, :m, :h, :q), Defaults to :m

  • mode (symbol) (defaults to: nil)

    Optional mode. One of (:number, :alphanumeric, :byte_8bit, :kanji), Defaults to :alphanumeric or :byte_8bit

Returns:

  • (RQRCode::QRCode)

    QR code that can hold the specified data with the desired error correction level

Raises:

  • (RQRCodeCore::QRCodeRunTimeError)

    if the data specified will not fit in the largest QR code (QR version 40) with the given error correction level

Since:

  • 0.5.0



45
46
47
48
49
50
51
# File 'lib/prawn/qrcode.rb', line 45

def self.min_qrcode(content, qr_version = 0, level: :m, mode: nil, **)
  qr_version += 1
  RQRCode::QRCode.new(content, size: qr_version, level: level, mode: mode)
rescue RQRCodeCore::QRCodeRunTimeError
  retry if qr_version < 40
  raise
end

Instance Method Details

Prints a QR Code to the PDF document. The QR Code creation happens on the fly.

Parameters:

  • content (string)

    The string to render as content of the QR Code

  • level (symbol) (defaults to: :m)

    Error correction level to use. One of: (:l, :m, :h, :q), Defaults to :m

  • mode (symbol) (defaults to: nil)

    Optional mode. One of (:number, :alphanumeric, :byte_8bit, :kanji), Defaults to :alphanumeric or :byte_8bit

  • pos (Array) (defaults to: [0, cursor])

    Two-element array containing the position at which the QR-Code should be rendered. Defaults to [0,cursor]

  • options (Hash)

    additional options that are passed on to Prawn::QRCode::Renderer

See Also:



75
76
77
78
# File 'lib/prawn/qrcode.rb', line 75

def print_qr_code(content, level: :m, mode: nil, pos: [0, cursor], **options)
  qr_code = Prawn::QRCode.min_qrcode(content, level: level, mode: mode)
  render_qr_code(qr_code, pos: pos, **options)
end

#render_qr_code(qr_code, **options) ⇒ Object

Renders a prepared QR code (RQRCode::QRCode) int the pdf.

Parameters:

  • qr_code (RQRCode::QRCode)

    The QR code (an RQRCode::QRCode) to render

  • options (Hash)

    additional options that are passed on to Prawn::QRCode::Renderer

See Also:

Since:

  • 0.5.0



88
89
90
91
# File 'lib/prawn/qrcode.rb', line 88

def render_qr_code(qr_code, **options)
  renderer = Renderer.new(qr_code, **options)
  renderer.render(self)
end