【问题标题】:Render Prawn pdf and rqrcode display QR with Ruby on rails使用 Ruby on rails 渲染 Prawn pdf 和 rqrcode 显示 QR
【发布时间】:2014-08-17 19:04:58
【问题描述】:

使用 qrcode 创建 Ruby on Rails Web 应用程序,在 pdf 中显示 QR 码

我正在使用

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 时遇到错误,它是显示

未定义的局部变量或方法“请求”#

【问题讨论】:

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


    【解决方案1】:

    有一个名为 prawn-qrcodes 的 gem,它真的很单用。

    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

    构建的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-25
      • 2018-06-25
      • 2013-06-24
      相关资源
      最近更新 更多