0
votes

I've tried many gems for creating PDF documents, some of them work really well without too much difficulty. My mission is to create a QRcode in a PDF document. I've tried a lot of combinations between QR generators and PDF generators. Although I'm having a lot of trouble actually getting the Qr code into the PDF document. I'm using the rqrcode gem, I like it because it generates QR codes from CSS, not an image.

I've tried using gems specifically built for generating QR codes with Prawn, however I keep getting a message saying that it doesn't recognise the modules method. The closest I've got is by using a similar structure in my html document. But thats only using it without a table, which is useless because the gem works by outputting a table and CSS.

I know this is quite vague, but because of the amount of possible solutions and different attempts I've tried I'm not sure what other information I should give.

Any ideas? Has anyone else managed to create a QR code with the rqrcode gem using a table in Prawn?

How the table looks in the view (HTML, not PDF):

<table class="qr">
 <% qr.modules.each_index do |x| -%>
   <tr>
   <% qr.modules.each_index do |y| -%>
    <% if qr.dark?(x,y) -%>
    <td class="black"/>
    <% else -%>
    <td class="white"/>
    <% end -%>
   <% end -%>
   </tr>
 <% end -%>
</table> 

The closest I've come without it breaking in the .prawn.pdf file (when I try to add a table it breaks). The * and ! is just testing that the modules method works, when I try to change it to cell background it breaks.

@qr.modules.each_index do |x|
   @qr.modules.each_index do |y|
    if @qr.dark?(x,y)
      pdf.text "!"
    else
      pdf.text "*"
    end
   end
 end

Gemfile

gem 'prawn'

gem 'prawn-qrcode'

gem "rqrcode", "~> 0.4.2"

Update

I found some useful code, although now the problem I have is the actual sizing of the QR code. I want to be able to make the QR code very big. Surprisingly enough the parameters in the new code don't actually change the size; the size parameter inside the actual QR code does change the size very slightly, but it's actually changing the version of the QR code, which is not what I want.

1

1 Answers

1
votes

i had a nearly similar problem. I had a to generate a pdf document of barcodes. the barcodes needed to be displayed in a grid in the html view and when user clicks on 'save as pdf', a pdf file of the view be send to user. I solved it as

in controller, before class

require 'rubygems'
require 'barby'
require 'barby/barcode/code_128'
require 'chunky_png'
require 'barby/outputter/png_outputter'
require 'aws/s3'

controller method

def generate_barcode
   ...
   @barcode_numbers = inspection.get_barcode_numbers
   ...
   @barcode_numbers.each do |d|
     barcode = Barby::Code128B.new(d)
     image_path = get_image_path + "code_#{params[:inspection_id]}_#{@spec.id}_#{d.sub('.', '0')}.png"
     File.open(image_path, 'w'){|f|
       f.write barcode.to_png(:height => 50, :margin => 3)
       Barcode.create(:filename => "code_#{params[:inspection_id]}_#{@spec.id}_#{d.sub('.', '0')}.png", :measurement => d)
     }
   end
   html = render_to_string(:action => "generate_barcode.html.haml", :layout => false)
   kit = PDFKit.new(html)
   send_data(kit.to_pdf, :filename => 'barcodes.pdf', :type => 'application/pdf', :disposition => 'inline')
   @all_barcodes.each do |d|
     File.delete("#{Rails.root}/tmp/#{d.filename}")
   end if Rails.env != "development"
   @all_barcodes.destroy_all