Generate QR Codes with Ruby
Ruby's rqrcode gem makes QR code generation simple and elegant. Perfect for Rails applications, scripts, and automation workflows.
Installation
Install the rqrcode gem using Bundler or gem install.
gem 'rqrcode'gem install rqrcodeGenerate QR Codes with rqrcode
Code examples using the rqrcode gem in Ruby.
QR Code as SVG
require 'rqrcode'
qr = RQRCode::QRCode.new('https://qrcode.fun')
svg = qr.as_svg(
offset: 0,
color: '1A2B3C',
shape_rendering: 'crispEdges',
module_size: 6,
standalone: true
)
File.write('qrcode.svg', svg)QR Code as PNG
require 'rqrcode'
qr = RQRCode::QRCode.new('https://qrcode.fun')
png = qr.as_png(
bit_depth: 1,
border_modules: 4,
color_mode: ChunkyPNG::COLOR_GRAYSCALE,
color: 'black',
file: nil,
fill: 'white',
module_px_size: 6,
resize_exactly_to: false,
resize_gte_to: false,
size: 300
)
IO.binwrite('qrcode.png', png.to_s)Rails Controller
class QrCodesController < ApplicationController
def show
qr = RQRCode::QRCode.new(params[:data] || 'https://qrcode.fun')
svg = qr.as_svg(module_size: 6, standalone: true)
render inline: svg, content_type: 'image/svg+xml'
end
endGenerate QR Codes via API in Ruby
Call the QRCode.fun API from Ruby using Net::HTTP or Faraday for styled QR codes.
require 'net/http'
require 'json'
uri = URI('https://qrcode.fun/api/generate-qr-styled')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Post.new(uri)
request['Content-Type'] = 'application/json'
request.body = {
data: 'https://qrcode.fun',
width: 300,
height: 300,
type: 'png',
margin: 10,
dotsOptions: { color: '#1A2B3C', type: 'rounded' },
cornersSquareOptions: { color: '#8564C3', type: 'extra-rounded' },
backgroundOptions: { color: '#FFFFFF' }
}.to_json
response = http.request(request)
result = JSON.parse(response.body)
puts result['data'][0..49]Live QR Code Preview
Try generating a QR code with Ruby right now.
Native Library vs API
Compare using rqrcode versus the QRCode.fun API.
| Feature | rqrcode Gem | QRCode.fun API |
|---|---|---|
| Setup complexity | gem install + ChunkyPNG for images | Single HTTP request |
| Customization | Colors, module size (SVG/PNG) | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | bundle update | Always up-to-date |
| Output formats | SVG, PNG, ANSI terminal | PNG, SVG |
Ruby QR Code Use Cases
Common scenarios for QR codes in Ruby applications.
Rails Applications
Generate QR codes in Rails controllers for user profiles, event tickets, and two-factor authentication setup.
Background Jobs
Create QR codes asynchronously with Sidekiq or Active Job for batch label generation and email campaigns.
API Endpoints
Build API endpoints that return QR codes as images or SVG for mobile apps and third-party integrations.
Automation Scripts
Write Ruby scripts for generating QR codes in bulk for inventory, asset tagging, and event management.
Ruby QR Code Ecosystem Deep Dive
Ruby's elegant syntax and Rails ecosystem make QR code generation simple and productive for web applications.
Rails Controllers, Views & Active Storage
In Rails, generate QR codes in controllers using rqrcode and serve them as inline SVG or PNG downloads. Use Active Storage to persist generated QR codes as model attachments — attach a QR code to a Ticket or Product record. Rails' fragment caching with cache keys based on QR content avoids redundant generation. In Hotwire/Turbo applications, generate QR codes server-side and deliver them via Turbo Frames for dynamic updates without full page reloads.
Sidekiq Background Processing
For bulk QR code generation, offload work to Sidekiq workers. Create a GenerateQrCodeJob that produces QR codes for batch scenarios like event ticketing, product labeling, or mail campaigns. Use Sidekiq's batch feature to track completion of large QR generation runs. Redis caching with a content-based key (Digest::SHA256) prevents duplicate generation across workers and serves cached results for repeated requests.
Gem Ecosystem: rqrcode, ChunkyPNG & MiniMagick
Ruby's gem ecosystem provides flexible QR code rendering pipelines. rqrcode generates the QR matrix, then ChunkyPNG renders it to PNG without ImageMagick dependency — ideal for containerized deployments. MiniMagick enables advanced post-processing: adding logos, rounded corners, and gradient backgrounds. For SVG output, rqrcode's built-in SVG renderer creates resolution-independent QR codes perfect for responsive web designs and print materials.
Frequently Asked Questions
Common questions about generating QR codes with Ruby.
Start generating QR codes with Ruby
Use our free generator or integrate the API into your Ruby and Rails applications.