2
votes

I can generate a pdf in prawn. I need to show some text if the generated pdf is multipage. For example, if generated pdf is more than one page, then I have to show "continued ..." in all pages except last page. And if the pdf is just one page then I don't want to show it at all. Is it possible?

2

2 Answers

3
votes

I think it should work:

if (num_pages = pdf.page_count) > 1
  pdf.repeat(lambda { |pg| pg < num_pages }) do
    pdf.draw_text "continued...", :at => [250, 20]
  end
end
1
votes

Prawn's repeat can print text on any selection of pages. The first argument takes a named key, an array, a range or a lambda. In your case, you can define all pages except the last as a range.

page_range = (1..(pdf.page_count - 1))
pdf.repeat(page_range) do
  pdf.draw_text "continued ..."
end