Search code examples
ruby-on-railspdfprawn

Prawn::Errors::CannotFit Rails Make new line table


I am working on a web app in ruby on rails that should list presentations that a set of users can sign up for. At the end, the admin should be able to send an email to all users with an automatically generated PDF. I am using prawn to create this pdf.

The controller:

def generate_pdf(teac,pres)
    Prawn::Document.new(:page_size => "A4", :page_layout => :landscape) do
      text "#{teac.name} #{teac.surname}", align: :center
      text "Your Presentations"
      table([
        ["Name","Surname","Title","Room","Time","Date"],
        [pres.collect{ |r| [r.name] },
         pres.collect{ |r| [r.surname] },
         pres.collect{ |r| [r.title] },
         pres.collect{ |r| [r.room] },
         pres.collect{ |r| [Time.at(r.time.to_i).utc.strftime("%H:%M")] },
         pres.collect{ |r| [r.date] }]
        ])
      move_down 20
      pres.each do |pres|
        text "visitors '#{pres.title}':"
        text "#{pres.visitors}"
        move_down 20
      end
    end.render
  end

The problem occurs when the title is so long that the table expands beyond the landscape A4 page. Is there a way to automatically add a new line when the title becomes to long?


Solution

  • If you are okay with setting static column widths, that should to the trick.

    For example:

    table(data, column_widths: [100, 100, 100, 100, 100, 100])