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 ?
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.
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.
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.
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,..)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With