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

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

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

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 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

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.

On Tue, Nov 3, 2009 at 1:17 PM, Marnen Laibow-Koser [email protected]
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

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
[email protected]