I need to generate a QR code and store it in rails. I am using rqrcode gem for generating QR code.
Here is how I am generating QR code as png.
def generate_qr_code(checkin_url)
qrcode = RQRCode::QRCode.new(checkin_url)
png = qrcode.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: 120
)
self.update!(qr_code_image: png.to_s)
end
Issue:
I am facing issues while storing the QR code in qr_code_image.
self.update!(qr_code_image: png.to_s) showing following error:
*** NoMethodError Exception: undefined method `map' for #<String:0x007feb358121d0>
Did you mean? tap
Even IO.write("/tmp/github-qrcode.png", png.to_s) showing
*** Encoding::UndefinedConversionError Exception: "\x89" from ASCII-8BIT to UTF-8
I finally have to store the png image in qr_code_image where I am using
mount_uploaders :qr_code_image, QrCodeUploader
Method png.to_s return a binary content of file
To write a file from a binary contente run:
File.binwrite("/tmp/github-qrcode.png", png.to_s)
If you want save a png.to_s on your database, I think that use a base64 is a good choice.
To save a base64 in your model the code is something like that
u = User.find(1)
u.qr_code_image = Base64.encode64(png.to_s)
u.save
Finaly, to see a qrcode in browser just
<img src="data:image/png;base64,<%= user.qr_code_image %>" />
Related
I am working on Rails 6 API where I need to generate the QR code image and save that image to S3 using active_storage.
I am using rqrcode gem for this which gives me the SVG string. How can I convert the SVG string into the image and store that image to S3 using active_storage?
Following is my code
Controller
qrcode = RQRCode::QRCode.new("#{#order.id}")
svg = qrcode.as_svg(
offset: 0,
color: '000',
shape_rendering: 'crispEdges',
module_size: 6,
standalone: true
)
I should also generate the png image using
png = qrcode.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: 120
)
But while storing I got the following error
ArgumentError (Could not find or build blob: expected attachable, got <ChunkyPNG::Image 120x120 [
Looking for a way to generate the image from the svg string or png file.
Thanks.
note: frontend is react-native
I was able to achieve this by following the answer here.
In short, after you generate the png, you can attach it to your model like so:
#model.qr_code.attach(io: StringIO.new(png.to_s), filename: "filename.png")
p ZBar::Image.from_jpeg(qrcode_tag("hello")).process
def qrcode_tag(text)
qr = ::RQRCode::QRCode.new(text).as_png
image = Magick::Image.from_blob((qr).read).first
image.format = 'JPEG'
return image
end
As I know, RQRCode doesn't support png file type, so I tried to convert a qr code that its file type is png to jpeg then an error that I indicated occurred.
ChunkyPNG::Image does not provide a read method. You probably want to call to_datastream on it. See the documentation of the chunky_png gem: http://www.rubydoc.info/github/wvanbergen/chunky_png/ChunkyPNG/Image
Iam using gem CKEditor to build editor for my website.
I use Paperclip as backend for upload image in CKEditor.
However when upload image and click "Send to server", CKEditor appear a error:
ArgumentError in Ckeditor::PicturesController#create wrong number of arguments (given 1, expected 0)
Extracted source (around line #181):
# <%= link_to(#person.name, person_path) %>
# # => Donald E. Knuth
def parameterize(separator: "-", preserve_case: false)
ActiveSupport::Inflector.parameterize(self, separator: separator, preserve_case: preserve_case)
end
If I use carrierwave as backend for upload image, everything ok. But I only want use Paperclip.
How to fix it ? Thanks.
I'm trying to build a rails app that generates an image by overlaying a text over a default image.
I'm almost there - by using carrierwave + mini magick i have this method working :
def generate_image
message = self.description.upcase
image = MiniMagick::Image.open('public/base.jpg')
image.combine_options do |c|
c.size '400x500'
c.gravity 'NorthWest'
c.fill 'black'
c.strokewidth '2'
c.pointsize '48'
c.interline_spacing '-9'
c.font "#{Rails.root}/public/black.ttf"
c.draw "text 40,40 '#{message}'"
c.antialias
end
self.image = image
end
the problem is that i want my text to fit the size i've specified - but I had no luck !
reading the Image Magick documentation I know that I should use the "capition" method instead of draw - but when I do so it throws out an orrible error :) :
failed with error: mogrify: no encode delegate for this image format `CAPTION' #
error/constitute.c/WriteImage/1167.
any clues ?
thanks !
I'm generating a pdf with prawn. Basically, I generate the document and I fill it with some images.
The problem comes when I download the file and I try to print it. The dimensions are not set to the ones I previously specified.
pdf = Prawn::Document.new(page_size: "A3", margin: PAGE_MARGIN, page_layout: :landscape)
When I try to print it, the default page size is "A4" instead of "A3"
How can I solve this?
I tried to attach some metadata but it didn't work correctly.
Thanks in advance!
In the case where you're generating the document within its own class, this also works to declare the paper size:
class EnvelopePdf < Prawn::Document
def initialize(_item_array, _type_of_item)
super(:page_size => [324, 684], :page_layout => :landscape) # 4.5" by 9.5", which is No 10 envelopes
... application-specific initialization code here ...
print_the_envelopes
end
Using prawn 1.3.0:
require "prawn"
pdf = Prawn::Document.new(:page_size => 'A3')
pdf.text "Hello World!"
pdf.render_file("export.pdf")
in terminal:
pdfinfo export.pdf
outputs:
Creator: Prawn
Producer: Prawn
Tagged: no
Form: none
Pages: 1
Encrypted: no
Page size: 841.89 x 1190.55 pts
Page rot: 0
File size: 842 bytes
Optimized: no
PDF version: 1.3