Don't break paragraph when new page (Prawn)
Asked Answered
C

4

6

Is there a way to keep text as a same block when a new page (don't break it). If it exists, what is the command?

I mean for example, I have a paragraph of five lines. When a new page starts I don't want two lines in the first page and three lines in the second page. I want the lines to stay together. Another way to do it : put the five lines in the second page.

Cosmos answered 29/7, 2015 at 10:7 Comment(2)
What do you mean with don break it? When text automatically breaks into a new page is because there's no space left on the previous page. Do you want to have the text remain as one block, and move it all to the new page, instead of having half of it in the first page, break and have the other half on the second page?Klarrisa
I mean for example : I have a paragraph of five lines. When a new page start I don't want Two line in the first page and three line in the second page. I want that the line stay together in this case five line in the second page.Recidivate
P
2

You could just do :

pdf.group do

  #Your code

end

Is that what you were looking for ??

Pastorate answered 11/8, 2015 at 17:49 Comment(1)
Doesn't work anymore NotImplementedError (Document#group has been disabled because its implementation lead to corrupted documents whenever a page boundary was crossed. We will try to work on reimplementing it in a future release)Cyruscyst
T
8

After group was removed I did it this way:

I get the height of the text block

height = pdf.height_of_formatted([{ :text => my_text, size: 14, style: :bold }])

And compare the height with the remaining space on the page. If there's no space for the whole text block, I start a new page before printing.

if pdf.cursor < height
  pdf.start_new_page
end

I know, this is more complicated than it was to create a simple group-block, but at least it works and can be altered to all kinds of situations and formattings.

Typewrite answered 4/8, 2017 at 7:52 Comment(2)
Great solution that worked for meStansbury
Worth mentioning measuring a text box in advance: https://mcmap.net/q/1467317/-prawn-pdf-how-to-get-the-height-of-a-text-boxCerulean
C
2

Im a newbie so dont take me seriously. But there seems to be these block text holders in prawn. You can make the borders invisible. So you put the text in one of the boxes and maybe it moves automatically to a new page if the whole box doesnt fit on the last page?

Cazzie answered 11/8, 2015 at 13:28 Comment(0)
P
2

You could just do :

pdf.group do

  #Your code

end

Is that what you were looking for ??

Pastorate answered 11/8, 2015 at 17:49 Comment(1)
Doesn't work anymore NotImplementedError (Document#group has been disabled because its implementation lead to corrupted documents whenever a page boundary was crossed. We will try to work on reimplementing it in a future release)Cyruscyst
C
-1

This work too :

group do
   #Your code
end
Cosmos answered 11/8, 2015 at 14:39 Comment(1)
FYI When you commented this the method was already removed from prawn master.Repairer

© 2022 - 2024 — McMap. All rights reserved.