2
votes

I want to specify columns at various points in a pdf document.

Example: three columns for this section, one column for that section, two columns for this other section.

I don't want rows to be tied to a physical location on a pdf document because I have dynamic text with various lengths, so I cannot predict how much content is within a certain area. What happens is that if there is too much text for a [column, row] box it simply overflows to the subsequent row, colliding with text there.

It appears that the layout grid in prawn forces me to specify the number of rows.
It would be nice if I could dynamically define rows like with bootstrap. That way content in a row does not start until the previous row takes up all the space it needs.

Here is how I understand you must define a layout grid in Prawn:

    pdf.define_grid(columns: 12, rows: 12, gutter: 10)

So say I do something like this:

pdf.grid([0,0]).bounding_box do
  pdf.text "Aenean eu leo quam. Pellentesque ornare sem lacinia quam venenatis vestibulum. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Maecenas faucibus mollis interdum."
end

pdf.grid([1,0]).bounding_box do
  pdf.text "some text"
end

Clearly the text in the [0,0] bounding box is going to collide with the text in the [1,0] bounding box because there is too much content in [0,0], so it overflows into rows below it.

Because bootstrap is not stuck to a physical columns/rows layout grid, Bootstrap allows the content in [0,0] to take up as much space as needed and then begins [1,0] content afterwards.

The issue is that prawn's layout grid appears to apply directly to the physical location of a pdf document, but I don't want it to do that.

Hopefully that makes sense. Is something like this possible in Prawn? Otherwise I foresee myself having to forgo columns altogether for my pdfs, which would render a ton of empty space.

Prawn PDF manual, layout info on page 74

3

3 Answers

1
votes

With prawn table, it could like like this:

require 'prawn'
require 'prawn/table'

Prawn::Document.generate("junk4.pdf") do

  font "Helvetica"

  text "Test Table", :style => :bold_italic
  stroke_horizontal_rule

  move_down 20

  table([ ["short", "short", "loooooooooooooooooooong "*30],
          ["short", "loooooooooooooooooooong "*15, "short"],
          ["loooooooooooooooooooong "*10, "short", "short"] ])

end
0
votes

I think the grid is for tables. Column_box looks more applicable to your problem:

column_box([0, cursor], :columns => 2, :width => bounds.width) do
  text("a bunch of text ...")
end
0
votes

I hope I understand you correctly.

I'd use Prawn table for this as suggested in the first answer. For that add a gem 'prawn-table' and use code like this:

pdf = Prawn::Document.new

# First section with two columns
pdf.table([['column_text_1', 'column_text_2']])

# Second section with three columns
pdf.table([['column_text_1', 'column_text_2', 'column_text_3']])

If you need something more advanced you could refer to the gem documentation or leave a comment here.