2015-09-17 63 views
3

我試圖生成具有不同QRcodes一個PDF文檔,並使用下面的寶石的Rails:在蝦PDF生成QR碼

gem 'prawn' 
gem 'prawn-qrcode' 

問題是,我似乎無法打印出與QR碼以下方法:

require 'prawn/qrcode' 

class QrcodePdf < Prawn::Document 
    def initialize (deal) 
    super() 
    @deal = deal 
    title 
    @deal.venues.each do |venue| 
     text "QR Code for: #{venue.name}" 
     qrcode = RQRCode::QRCode.new(@deal.id.to_s + "_" + venue.id.to_s + "_" + @deal.created_at.to_s) 
     render_qr_code(qrcode) 
    end 
    end 

    def title 
    text "Title of deal: #{@deal.title}", size: 16, style: :bold 
    end 
end 

任何幫助將不勝感激!謝謝!

編輯:附加信息

對不起,我忘了指出的PDF實際上是被編譯。但QRCode部分是空白的。 所以它只是在循環中打印QR碼的文本。

此外,我已經打印出@ deal.id.to_s中的字符串......它確實包含我想要的數據,所以我不確定是哪裏出了問題。

我也在使用部分中提到https://github.com/jabbrwcky/prawn-qrcode

+0

您應該添加你做了什麼錯誤。發佈說明「這不起作用」的代碼對我們試圖幫助您沒有幫助。 – Deekor

+0

Hi @Deekor,對不起,我對問題進行了必要的更改 – user5296896

+0

如何將QR碼放在表格中? –

回答

4

這可能是與prawn-qrcode錯誤,但render_qr_code不起作用。至少Ruby 2.2.3(我沒有測試過,如果是舊版本)。

你仍然可以使用print_qr_code方法,它的功能:

pdf = Prawn::Document.new(:page_size => "A4") do 
    print_qr_code("some-text", extent: 96, stroke: false) 
end 
+1

啊我明白了。難怪它不起作用。您的解決方案完美運作感謝您的幫助,非常感謝! – user5296896