How to Generate QR Codes using Prawn-QRcode in Ruby?
Tags: ruby, qrcode, rails, coding
Published: 2019-12-10 09:39:23

---
description: Wondering how to generate a QR code using Ruby? Here's a simple guide that provides a detailed description of the process
---

This tutorial will guide you on how to generate [QR codes](https://en.wikipedia.org/wiki/QR_code) using for [Ruby on Rails application development] (https://www.botreetechnologies.com/ruby-on-rails-development).
Let’s have a brief introduction to QR codes. If you are familiar with [prawn-qrcode](https://github.com/jabbrwcky/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](https://gorails.com/setup/ubuntu/18.04#ruby-rvm) in your system.*
<code>gem install prawn-qrcode</code>

**Implementation**

* After installation of gem, create ruby file let's say sample.rb and add below method into it and run

     <code>$ ruby sample_qr.rb</code>.

* 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**.

{% gist https://gist.github.com/akshayvadher-BTC/9aa78cad1088d98b111d6cf5817006a3 %}

It will Generate PDF which looks like this.

![pdf of generated QR codes](https://www.botreetechnologies.com/blog/wp-content/uploads/2019/12/prawn-qrcode-in-ruby-1.png)

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.

![error](https://www.botreetechnologies.com/blog/wp-content/uploads/2019/12/error.png)

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](https://www.botreetechnologies.com/usa/web-development-company-new-york) 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](https://www.botreetechnologies.com/hire-ruby-on-rails-developers) from BoTree Technologies for more assistance on generating QR codes using Ruby.

### Read Also: [Must have Visual Studio Code extensions!](https://www.botreetechnologies.com/blog/must-have-visual-studio-code-extensions)