Skip to content

Instantly share code, notes, and snippets.

@3dd13
Created February 18, 2011 07:49
Show Gist options
  • Save 3dd13/833392 to your computer and use it in GitHub Desktop.
Save 3dd13/833392 to your computer and use it in GitHub Desktop.
overflow / page break examples of Prawn Pdf generation.
# gem "prawn", "0.8.4"
# Sometime there is no enough space for the last table row when it reaches the end of page
Prawn::Document.generate("text_group_overflow_question.pdf") do |pdf|
add_page_break_if_overflow(pdf) do |pdf|
# generating table here
# ...
end
end
def add_page_break_if_overflow(pdf, &block)
current_page = pdf.page_count
roll = pdf.transaction do
yield(pdf)
pdf.rollback if pdf.page_count > current_page
end
if roll == false
pdf.start_new_page
yield(pdf)
end
end
# gem "prawn", "0.11.1.pre"
Prawn::Document.generate("text_group_overflow_question.pdf") do
10.times do
text "I am part of a paragraph"
end
end
# gem "prawn", "0.11.1.pre"
Prawn::Document.generate("text_group_overflow_question.pdf") do
group do
10.times do
text "I am part of a paragraph"
end
end
end
@maurcarvalho
Copy link

Transaction is deprecated in the last versions (I'm using 1.0.0). Do you known to to solve this problem without transaction?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment