Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Wrap By Words in Prawn Table

Tags:

ruby

prawn

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 ?

like image 675
Spyros Avatar asked Apr 11 '12 07:04

Spyros


People also ask

How do I wrap text around a table in HTML?

Use the border-collapse property set to "collapse" and table-layout property set to "fixed" on the <table> element. Also, specify the width of the table. Then, set the word-wrap property to its "break-word" value for <td> elements and add border and width to them.

How do you wrap the contents of a table?

To wrap text in a column in a table, right-click to view the column dropdown menu and select the "Wrap text" option: You can wrap text in multiple columns at once by selecting multiple columns, right-clicking, and then selecting the option to wrap text. If you want to wrap your column headers check out this article.

Does word-wrap work in a table?

Wrap Text Around a Table in Word. Right-click on the table and select “Table Properties.” In the Table tab, select the “Around” option. Adjust the wrapping by dragging and dropping the table, or by clicking “Positioning” in Table Properties.


1 Answers

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,..)
like image 169
0x4a6f4672 Avatar answered Sep 23 '22 18:09

0x4a6f4672