0
votes

I am using the prawn gem with Rails 4

I have the following gems:

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

I get the following error:

Prawn::Errors::UnrecognizedTableContent

When I do this:

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

Can anyone help by providing an example of adding a QR code to each row in a table?

UPDATE: I changed my table code to the following (I added .to_s to the qr code:

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

Now, I get the following pdf. See screenshot. How can I get those QR codes inside of a table cell?

enter image description here

1
I never use prawn-qrcode, but they have some examples at github.com/jabbrwcky/prawn-qrcode/tree/master/examples, might be useful for youSaiqul Haq
yea, i've read the documentation. thanks though.don_Bigote

1 Answers

1
votes

You currently use:

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

This results in prawn-table to convert and render the QRCode as string.

By default tables accept he following data as table cells:

  1. String: produces a text cell (the most common usage)
  2. Prawn::Table::Cell
  3. Prawn::Table
  4. Array
  5. Images

Unfortunately I don't have the time right now to really dig into this problem, but judging from the documentation at: http://prawnpdf.org/docs/0.11.1/Prawn/Table/Cell.html, you most likely need to create a subclass of Prawn::Table::Cell, that renders the QR code by implementing the #draw_content() method.

You also might have to adjust/constrain the table cell dimensions to provide miminum size to fit the QRCode by implementing #set_width_constraints()