Prawn: float text automatically to a new page

2019-07-28 01:55发布

问题:

I have following simple code to generate a pdf.

def employe_details
        y = cursor
bounding_box([0, y], :width => 16.cm) do
          @employe.each do |pr|
                txt = pr.week.to_s + ' '
                txt += "work hours"
                text_box txt, size: 11, :at => [0.5.cm, cursor]
                move_down 0.4.cm
            end
              .#more similar texts
              .
              .    
end

Problem is, this doesn't create a new page automatically. When the text exceeds first page, rest of the text doesn't show at all or those texts does not show up in a new page.

How to float the texts automatically to a new page when it reaches the end of a page?

Update: Problem with my code seems with this line :at => [0.5.cm, cursor], if I remove the position then it flows to next page, same happens when I use span. If I use position with text in span then it does not flow to next page and if I remove it then it flows to next page. So how can I use something like this

text_box txt, size: 11, :at => [0.5.cm]
text txt, size: 11, :at => [0.5.cm]

Textbox or text without cursor positions, I need to use x-position because every line has different x-positions.

回答1:

bounding_box content will not flow onto the next page. You can use span instead: (emphasis added)

A span is a special purpose bounding box that allows a column of elements to be positioned relative to the margin_box.

This method is typically used for flowing a column of text from one page to the next.

The manual mentions this on page 35:

This example also shows text flowing across pages following the margin box and other bounding boxes.

# ...

move_cursor_to 200
span(350, :position => :center) do
    text "Span is a different kind of bounding box as it lets the text " +
         "flow gracefully onto the next page. It doesn't matter if the text " +
         "started on the middle of the previous page, when it flows to the " +
         "next page it will start at the beginning." + " _ " * 500 +
         "I told you it would start on the beginning of this page."
end

The result is shown on pages 37/38: