Spyros
Spyros

Reputation: 48706

How to Wrap By Words in Prawn Table

I would like to create a prawn table where cell text is wrapped by words and not letters. I am already using shrink_to_fit, but it seems that prawn is wrapping based on letters. I would ideally want it to wrap on words alone (single_line is not an option since there can be 2-3 words per cell).

Anyone knows how to do that ?

Upvotes: 8

Views: 3024

Answers (1)

0x4a6f4672
0x4a6f4672

Reputation: 28245

Maybe your table contains words that are too long for a single table cell? As far as I know, table cells in Prawn tables use bounding boxes which should wrap the text automatically. The following example works fine for me:

Prawn::Document.generate 'example.pdf' do
  data = [['Pig','Oink '*10],
          ['Cow','Moo '*10],
          ['Duck','Quack '*10]]
  table data do |table|
    table.column_widths = [50,150]
  end
end

If nothing works, you could try building your own table with multiple text_box calls instead of using the built-in Prawn table method, this is of course a bit cumbersome. text_box draws the requested text into a box. The :overflow parameter controls the behavior when the amount of text exceeds the available space, available options are :truncate, :shrink_to_fit, or :expand.

text_box(txt, :at => [x,y], :width => width, :height => height, :size => size,
              :overflow => :shrink_to_fit,..)

Upvotes: 4

Related Questions