Module: QRCode

Defined in:
lib/prawn/qrcode.rb

Overview

Extension for Prawn::Document for rendering QR codes.

Author

Jens Hausherr ([email protected])

Copyright

Copyright © 2011 Jens Hausherr

License

Apache License, Version 2.0

Constant Summary collapse

DEFAULT_DOTSIZE =

The default size for QR Code modules is 1/72 in

1

Instance Method Summary collapse

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 put into the QR Code

  • *options

    Named optional parameters

    :level

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

    :exent

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

    :pos

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

    :dot

    Size of QR Code module/dot. Calculated from extent or defaulting to 1pt

    :stroke

    boolean value whether to draw bounds around the QR Code. Defaults to true.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/prawn/qrcode.rb', line 39

def print_qr_code(content, *options)
  opt = options.extract_options!
  qr_version = 0
  level = opt[:level] || :m
  extent = opt[:extent]
  dot_size = opt[:dot] || DEFAULT_DOTSIZE
  begin
    qr_version +=1
    qr_code = RQRCode::QRCode.new(content, :size=>qr_version, :level=>level)

    dot_size = extent/(8+qr_code.modules.length) if extent
    render_qr_code(qr_code, :dot=>dot_size, :pos=>opt[:pos], :stroke=>opt[:stroke])
  rescue RQRCode::QRCodeRunTimeError
    if qr_version <40
      retry
    else
      raise
    end
  end
end

#render_qr_code(qr_code, *options) ⇒ Object

Renders a prepared QR Code object.

Parameters:

  • qr_code (RQRCode::QRCode)

    The QR Code to render

  • *options

    Named optional parameters

    :exent

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

    :pos

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

    :dot

    Size of QR Code module/dot. Calculated from extent or defaulting to 1pt

    :stroke

    boolean value whether to draw bounds around the QR Code. Defaults to true.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/prawn/qrcode.rb', line 70

def render_qr_code(qr_code, *options)
  opt = options.extract_options!
  dot = opt[:dot] || DEFAULT_DOTSIZE
  extent= opt[:extent] || (8+qr_code.modules.length) * dot
  stroke = (opt.has_key?(:stroke) && opt[:stroke].nil?) || opt[:stroke]
  pos = opt[:pos] ||[0,cursor]

  bounding_box pos, :width => extent, :height => extent do |box|
    if stroke
      stroke_bounds
    end

    pos_y = 4*dot +qr_code.modules.length * dot

    qr_code.modules.each_index do |row|
      pos_x = 4*dot
      qr_code.modules.each_index do |col|
        move_to [pos_x, pos_y]
        if qr_code.dark?(row, col)
          fill { rectangle([pos_x, pos_y], dot, dot) }
        end
        pos_x = pos_x + dot
      end
      pos_y = pos_y - dot
    end
  end
end