Nesting pdfs

68 views
Skip to first unread message

Marcus Wyatt

unread,
Mar 1, 2019, 4:00:07 AM3/1/19
to Prawn
SO question

I'm currently working on transitioning our WickedPdf pdfs to use Prawn and Prawn::Table because the page breaks are more reliable. I'm doing so by using a PORO to make and render the pdf.
I'm wondering if I can "nest" pdfs. By that I mean, can I have 1 PORO that is a pdf on its own but can also be inside another pdf (that has its own PORO)? Or is the only way to use some kind of helper that I call when I want to include the section that may or may not be nested.
I hope that makes sense.

Alexander Mankuta

unread,
Mar 1, 2019, 4:27:14 AM3/1/19
to Prawn
Hi Marcus,

This is not strictly Prawn question but OK.

You can try this trick. Let's say you have two classes that represent documents (not Prawn::Document). You can arrange them like this:

class Doc1
  def initialize(prawn_doc = nil)
    @prawn_doc = prawn_doc || Prawn::Document.new
  end
end

Then, whenever you want to include another document into the current document you pass its constructor Prawn document you already have.

class Doc1
  def render
    # Do what this doc need to do to render a beautiful document

    AnotherDoc.new(@prawn_doc).render

    # Do more awesome drawing
  end
end

And when you don't include that another document you just skip the constructor argument and let it create its own prawn document.

AnotherDoc.new.render

Hope this helps.

--
Regards,
Alex

Marcus Wyatt

unread,
Mar 1, 2019, 7:58:14 AM3/1/19
to Prawn
I think this will be the way I need to do it but there's still a problem. Since I'm using bounding_box and table to draw my data, the nested pdf renders over the containing pdf. 
When I render the nested pdf inside the containing pdf's render call, I only see the nested pdf.
When I render the nested pdf inside the method where the content gets filled for the containing pdf, I get overlapping text.

Here's a screenshot of the header. ("Policy snapshot" and "Log report")
Screen Shot 2019-03-01 at 7.54.22 AM.png

Alexander Mankuta

unread,
Mar 1, 2019, 8:02:31 AM3/1/19
to Prawn
I'm afraid Prawn doesn't provide any reliable isolation in this regard. You'd need to make sure all your drawing operations properly use bounding boxes.

--
Regards,
Alex
Reply all
Reply to author
Forward
0 new messages