Render Prawn pdf和rqrcode用Ruby on rails显示QR

时间:2014-08-17 19:04:58

标签: ruby-on-rails ruby ruby-on-rails-4 qr-code prawn

使用qrcode创建Ruby on rails web应用程序,以PDF格式显示二维码

我使用

gem 'rqrcode-with-patches', require: 'rqrcode'
gem 'prawn'

我的帖子控制器显示pdf视图

def show
 @qr=RQRCode::QRCode.new(request.url)
 respond_to do |format|
  format.html
  format.pdf do
    pdf = PostPdf.new(@post)
    send_data pdf.render, filename: "post#{@post.id}.pdf",
                          type: "application/pdf",
                          disposition: "inline"
   end
 end
end

post_pdf

class PostPdf < Prawn::Document
 def initialize(post)
  super({top_margin: 30, page_size: 'A4', page_layout: :landscape })
  @post = post
  post_id
  post_title
  qr_code
 end

 def post_id
  move_down 10
  text "Post id: #{@post.id}"
 end

 def post_title
  move_down 10
  text "Post title: #{@post.title}"
 end

 def qr_code
  move_down 10
  @qr=RQRCode::QRCode.new(request.url)
 end
end

我在显示pdf上显示错误,它的节目

  
    

未定义的局部变量或方法`request&#39;对于#

  

1 个答案:

答案 0 :(得分:0)

有一个名为prawn-qrcodes的宝石真的单身使用。

require 'prawn/qrcodes'

Prawn::Document.generate("qr-big.pdf") do
    msg = "Hello world, this is a QR code"
    text msg

    qrcode msg, :position => :center,
                :fit => [bounds.height, bounds.width]
end

我认为这比** rqrcode-with-patches **更适合你,因为它是为 prawn 构建的