Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling content flow with Prawn

Tags:

ruby

prawn

Let's say we want to display a title on the first page that takes up the top half of the page. The bottom half of the page should then fill up with our article text, and the text should continue to flow over into the subsequent pages until it runs out:

enter image description here

This is a pretty basic layout scenario but I don't understand how one would implement it in Prawn.

Here's some example code derived from their online documentation:

pdf = Prawn::Document.new do
  text "The Prince", :align => :center, :size => 48
  text "Niccolò Machiavelli", :align => :center, :size => 20
  move_down 42

  column_box([0, cursor], :columns => 3, :width => bounds.width) do
  text((<<-END.gsub(/\s+/, ' ') + "\n\n") * 20)
   All the States and Governments by which men are or ever have been ruled,
   have been and are either Republics or Princedoms. Princedoms are either
   hereditary, in which the bla bla bla bla .....
   END
  end
end.render

but that will just continue to show the title space for every page:

enter image description here

What's the right way to do this?

like image 866
Yarin Avatar asked Jun 24 '13 18:06

Yarin


2 Answers

Use floats.

float do
        span((bounds.width / 3) - 20, :position => :left) do
                # Row Table Code
        end
end

float do
        span((bounds.width / 3) - 20, :position => :center) do
                # Row Table Code
        end
end

float do
        span((bounds.width / 3) - 20, :position => :right) do
                # Row Table Code
        end
end
like image 85
user3556792 Avatar answered Oct 17 '22 12:10

user3556792


I know this is old, but I thought I'd share that a new option has been added to fix this in v0.14.0.

:reflow_margins is an option that sets column boxes to fill their parent boxes on new page creation.

column_box(reflow_margins: true, columns: 3)
like image 27
Baylor Rae' Avatar answered Oct 17 '22 11:10

Baylor Rae'