This tutorial will guide you on how to generate QR codes using for Ruby on Rails application development.
Let’s have a brief introduction to QR codes. If you are familiar with prawn-qrcode directly go to the implementation section.
What is QR Code?
- QR code is a type of barcode(two-dimensional) for the automotive industry.
- It contains information about the item to which it is attached(i.e. Numbers, names or any secret information).
- It has black squares which are arranged in a square grid and has a white background, which can be read by an imaging device such as a camera.
Why QR code?
- If the information you want to store in a QR code is made of only alphanumeric characters, a single QR code can hold up to 4000 characters.
- The other key feature of QR Codes is that we do not require a chunky hand-held scanner to scan them, our modern cell phones can scan them.
Technology Stack
- Technology: Ruby
- Version: 2.6.3p62
- Git Repo: https://github.com/akshayvadher-BTC/QR-codes
Features
- High capacity encoding of data
- Small printout size
- Readable from any direction("360")
Installation
note: you should have installed ruby in your system.
gem install prawn-qrcode
Implementation
-
After installation of gem, create ruby file let's say sample.rb and add below method into it and run
$ ruby sample_qr.rb
. It’ll generate a PDF named sample.pdf.
If you do not want to use number units(i.e. mm, cm, m, etc.) then you do not need to require prawn/ measurement_extensions.
It will Generate PDF which looks like this.
In Qr code, there are 4 methods which are used most as below,
- Dot (module size): This method used to change blocks(black dots) size.
- Alignment of QR: We have aligned methods to align QR code on the specific side. (i.e top, right, bottom and left).
- Size of string: Depending on the number of characters of string you can change it.
- Page size: As per requirement, You can set the page size of QR code. (i.e. A4, A8). You can also set a margin in it.
We can change the module size by changing dot value and depending on the number of characters in the string you need to increase the size.
You may get error as below.
You are getting this error because you have more string size than you have specified in code. Reach out to learn more about the web development NYC experts for the various ways to improve or build the quality of projects and across your company.
So, if you have fixed document size and larger string than you need to reduce dot and increase size attributes and that’s it.
As you can see, I used many methods. You can use it as per your requirement. Here’s some,
- render_qr_code render’s generated QR code.
- move_up & move_down used to move cursor up & down respectively.
- As we do indentation in our code you can also indent text or QR code using indent method.
- For more details For more details you can hire Ruby on Rails developers from BoTree Technologies for more assistance on generating QR codes using Ruby.
Top comments (1)
Here's gist with Ruby 3.1+ working version: gist.github.com/chaserx/34558f4ef3...