In Rails with the Prawn gem how to set the table width to 100%?
Asked Answered
C

1

12

Im using the Prawn gem to generate a pdf table. I need to set the width of the table to 100%. How can I do this?

Here's my slmun_pdf.rb

class SlmunPdf < Prawn::Document
  def initialize(slmunDbs, view, allcount)

    if slmunDbs.table_name == "schools"
      super(top_margin: 50)
      if slmunDbs.size != allcount
        @warn = " (Not all Schools)"
      else
        @all = " All #{slmunDbs.size} Schools"
      end
      text "Showing#{@all}", size: 18, style: :bold, align: :center, color: "636363"
      text "#{@warn}", size: 11, align: :center, color: "858585"
      @schools = slmunDbs
      @view = view
      school_list

    end


  end

  def school_list
    move_down 20
    table school_list_rows, :cell_style => { :font => "Helvetica", :size => 9, :border_width => 0.5, :borders => [:top, :bottom], :border_color => "B0B0B0", :text_color => "737373"} do
        self.row(0).align = :center
        row(0).background_color = 'A0B046'      
        row(0).text_color = "FFFFFF"
        self.row_colors = ["DDDDDD", "FFFFFF"]
        self.header = true
    end
  end

  def school_list_rows
    [["Name", "Contact", "Country", "Pre Registration", "Full Registration", "Assigned Countries", "Total Delegates"  ]] +
    @schools.map do |school|
      countries = ""
      len = school.countries.count
      school.countries.each_with_index do |country, index|
        countries << "#{country.country }"
         if index+1 != len
          countries << ","
         end
      end 
      [school.name, school.contact, school.country, @view.yesno(school.prereg), @view.yesno(school.fullreg), countries, school.delegates.size ]
    end
  end

end

The existing documentations didn't help. I might be using a different method? I learnt this from rails cast!

Chetchetah answered 19/6, 2014 at 8:23 Comment(8)
prawnpdf.org/docs/0.11.1/Prawn/Document/PageGeometry.html You can do it in pixels, depending on your page widthPatrilineal
@RubyRacer Please help I do not use "Prawn::Document.new" in my appChetchetah
No, but you can use cell width using these numbersPatrilineal
@RubyRacer Can you please tell me how? I pasted my code aboveChetchetah
prawn pdf manual, page 91 prawnpdf.org/manual.pdfPatrilineal
@RubyRacer According to that, If I haven't set a column width it will expand. But i haven't set a column width and its not expandingChetchetah
It will expand to the content... Does the content cover full line?Patrilineal
@RubyRacer No the content doesn't cover the full lineChetchetah
L
24

You can use the width method of the Prawn::Document::BoundingBox class:

require 'prawn'
require 'prawn/table'

Prawn::Document.generate("hello.pdf") do
  table_content = [["This table"], ["covers the"], ["whole page width"]]
  table table_content, width: bounds.width
end
Landgraviate answered 27/3, 2015 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.