# PDF::Writer - measure size of a section before rendering it?

**URL:** <https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017>\
**Category:** Ruby\
**Created:** [September 8, 2008, 10:48am UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017 "2008-09-08T10:48:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 8, 2008, 10:48am UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017/1 "2008-09-08T10:48:24Z")

</div>

I’m using the PDF::Writer plugin and have got everything that i wanted  
working except for one thing: I have sections that i don’t want to  
split over a page. I tried a couple of ways of dealing with this, each  
of which has problems. Can anyone help?

1. Save page number and y position. Render the section, then test new  
page against old. If we’re on a new page, jump back to the saved  
position, insert a page break, then jump to the end. PROBLEM: there  
doesn’t seem to be a way to move the current position to a different  
page - only to different places on the same page.

2. This seems more sensible: Create the section, but don’t render it to  
the page - render it to some temporary place in memory - a virtual page,  
effectively (storing the virtual position first and afterwards to get  
the size of the section). If the section size is more than the distance  
from the position on the real page to the margin, put a page break on  
the real page. Then render to the real page - either by copying the  
contents of the virtual page or by re-rendering that section from  
scratch. PROBLEM: I can’t work out how to do this. doh.

Can anyone help, either with these approaches or a better way?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [September 8, 2008, 11:54am UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017/2 "2008-09-08T11:54:03Z")

</div>

I’m a dumbass…for approach 2, i just need to make a new PDF::Writer  
object and test my section size on that. What would be nicer though, is  
instead of redoing everything on my real pdf after testing it on the  
temp one, i could just copy the contents across. Does anyone know a way  
to do that?

thanks  
max

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 3, 2009, 6:22am UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017/3 "2009-11-03T06:22:50Z")

</div>

Max W. wrote:

> I’m a dumbass…for approach 2, i just need to make a new PDF::Writer  
> object and test my section size on that. What would be nicer though, is  
> instead of redoing everything on my real pdf after testing it on the  
> temp one, i could just copy the contents across. Does anyone know a way  
> to do that?
> 
> thanks  
> max

Hey man, this is what I did. If you can extend PDF::Writer that class or  
just open that class up, then you can add this function. If the markup  
comes out horrible, look at [gist:224821 · GitHub](http://gist.github.com/224821)

```
def wrap_it(&block)
    new_pdf = ConsoloPdf.new
    beginning_y = new_pdf.y

    yield new_pdf

    new_pdf_height = beginning_y - new_pdf.y

    self.start_new_page if self.y < new_pdf_height

    yield self
  end
```

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 3, 2009, 10:08am UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017/4 "2009-11-03T10:08:03Z")

</div>

Thanks a lot for that.

I’ve actually switched to using acts\_as\_flying\_saucer for pdf generation

- it does very good conversion from html to pdf, so no more  
hand-building as was the case with pdf writer.

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 3, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017/5 "2009-11-03T19:34:02Z")

</div>

On Tue, Nov 3, 2009 at 1:17 PM, Marnen Laibow-Koser [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Max W. wrote:
> 
> > Thanks a lot for that.
> > 
> > I’ve actually switched to using acts\_as\_flying\_saucer for pdf generation
> > 
> > - it does very good conversion from html to pdf, so no more  
> > hand-building as was the case with pdf writer.
> 
> I didn’t know there was a Flying Saucer plugin. I’ll have to look at it  
> – I really like Prawn, but doing my PDF files in HTML would be nice.

Yeah, if you want easy HTML to PDF, Flying Saucer is definitely the way  
to go.  
Prawn is more for if you want absolute control over how your document  
is rendered.

-greg

---

<div class="post-metadata">

**Author:** ![cmaxvv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/85f322/32.png) [@cmaxvv](https://www.ruby-forum.com/u/cmaxvv)\
**Post date:** [November 3, 2009, 3:09pm UTC](https://www.ruby-forum.com/t/pdf-writer-measure-size-of-a-section-before-rendering-it/148017/6 "2009-11-03T15:09:15Z")

</div>

Max W. wrote:

> Thanks a lot for that.
> 
> I’ve actually switched to using acts\_as\_flying\_saucer for pdf generation
> 
> - it does very good conversion from html to pdf, so no more  
> hand-building as was the case with pdf writer.

I didn’t know there was a Flying Saucer plugin. I’ll have to look at it  
– I really like Prawn, but doing my PDF files in HTML would be nice.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
