如何使用 Prawn 和 Rails 4 将 QR 码添加到 table 行

How do I add a QR code to table rows with Prawn and Rails 4

大虾gem我用的是Rails4

我有以下 gems:

gem 'prawn'
gem 'prawn-table'
gem 'prawn-qrcode'

我收到以下错误:

Prawn::Errors::UnrecognizedTableContent

当我这样做时:

require 'prawn/qrcode'

class ReportPdf < Prawn::Document
  def initialize(organizations)
    super()
    @organizations = organizations
    table_content
  end

  def table_content
    table organization_rows do
      row(0).font_style = :bold
      self.header = true
      self.row_colors = ['DDDDDD', 'FFFFFF']
      self.column_widths = [40, 300, 200]
    end
  end

  def organization_rows
    [['#', 'Name', 'id']] +
      @organizations.map do |organization|
        [render_qr_code(RQRCode::QRCode.new(organization.id.to_s)), organization.name, organization.id]
      end
  end

end

任何人都可以提供一个向 table 中的每一行添加二维码的示例吗?

更新: 我将 table 代码更改为以下内容(我将 .to_s 添加到二维码:

def organization_rows
  [['#', 'Name', 'id']] +
  @organizations.map do |organization|
   [render_qr_code(RQRCode::QRCode.new(organization.id.to_s)).to_s,
   organization.name,
   organization.id]
  end
end

现在,我得到以下 pdf。见截图。如何在 table 单元格中获取这些二维码?

您目前使用:

def organization_rows
  [['#', 'Name', 'id']] +
  @organizations.map do |organization|
   [render_qr_code(RQRCode::QRCode.new(organization.id.to_s)).to_s,
   organization.name,
   organization.id]
  end
end

这导致 prawn-table 将 QRCode 转换并呈现​​为字符串。

默认情况下 tables 接受以下数据作为 table 个单元格:

  1. 字符串:生成文本单元格(最常见的用法)
  2. Prawn::Table::Cell
  3. 大虾::Table
  4. 数组
  5. 图片

不幸的是,我现在没有时间真正深入研究这个问题,但从文档判断:http://prawnpdf.org/docs/0.11.1/Prawn/Table/Cell.html, 您很可能需要创建 Prawn::Table::Cell 的子类,它通过实现 #draw_content() 方法来呈现 QR 码。

您可能还需要 adjust/constrain table 单元格尺寸,以通过实施 #set_width_constraints()

提供适合 QRCode 的最小尺寸