Alternate odd/even page generation

270 views
Skip to first unread message

Gabriel

unread,
Jan 22, 2012, 1:03:07 PM1/22/12
to dompdf-dev
Hi,

I'm looking at setting up a pdf where the back page of each printout
has content that is not part of the main HTML document. This is the
terms and conditions for an invoice on the back of each sheet. The
main content can vary in length but the Ts&Cs are fixed

I'm guessing this isn't possible with the dompdf class as is but am
sure I should be able to modify the class to intercept the page
breaking and insert the Ts&Cs.

If I could be given a few pointers of where to best start looking at
modifying the class I'd be more than happy to contribute any code I
write.

I have two possible idea for how to acheive the end result. Which do
you think is the best approach?

1. Parse two separated DOM inputs simultaneously and generate
alternate pdf pages from the two inputs
2. Pregenerate and cache the back page then splice it in as a new page
each time the NewPage method is called.

How intertwined is the DOM parsing and PDF page rendering?

Gabriel

BrianS

unread,
Feb 7, 2012, 2:59:27 PM2/7/12
to dompd...@googlegroups.com
On Sunday, January 22, 2012 1:03:07 PM UTC-5, Gabriel wrote:
I'm looking at setting up a pdf where the back page of each printout
has content that is not part of the main HTML document. This is the
terms and conditions for an invoice on the back of each sheet. The
main content can vary in length but the Ts&Cs are fixed

So if I understand ... the front side of each page would be a multi-page document and the backside would be the same one-page document over and over. And to print you would want the odd pages to be the front side and the even pages to be the back side.
 
I'm guessing this isn't possible with the dompdf class as is but am
sure I should be able to modify the class to  intercept the page
breaking and insert the Ts&Cs.

If I could be given a few pointers of where to best start looking at
modifying the class I'd be more than happy to contribute any code I
write.

As things are current designed you would not be able to do this with a clean copy of dompdf. We'd be happy to help you out.
 
I have two possible idea for how to acheive the end result. Which do
you think is the best approach?

1. Parse two separated DOM inputs simultaneously and generate
alternate pdf pages from the two inputs
2. Pregenerate and cache the back page then splice it in as a new page
each time the NewPage method is called.

Actually, I was thinking of a third option but it depends on the structure of your Ts&Cs. If the back page is not too complex you could build it in script. Then all you would need to do is modify dompdf to run the script at the beginning/end of each page. The script would build the back page and then start a new page as its last step, whereupon dompdf would process the next page of your document. This would require less modification up front, though if your back page is long and/or complex generating it manually would be a chore. So I would maybe vote for #2 overall.

Considering the second option I can think of a few ways to try this. The difficult aspect in combining two separately rendered documents is that object references would have to be redone. Perhaps if you could render two document in sequence to the same PDF. Here's what I was thinking for the process: 1) render your back page, 2) copy the page contents to a new object, 3) delete the back page from the pdf, 4) render your main document, 5) have a per-page script that inserts the object and starts a new page. So you would need to modify dompdf to: a) process multiple document in sequence, and b) store and run scripts before/after a page has rendered.

This is all kind of off the top of my head, so I don't know if this will really work. Just tossing it out there...

Check out the enhancement request in issue 225 for someone's take on rendering multiple HTML segments in sequence.
 
How intertwined is the DOM parsing and PDF page rendering? 

Fabien would be better able to speak to this, but from my understanding things are fairly intertwined. There are basically two passes: first dompdf parses the document to apply styles and other non-rendering tasks; second pass dompdf builds the PDF from the object hierarchy created in the first step.

Fabien Ménager

unread,
Feb 8, 2012, 2:10:15 AM2/8/12
to dompd...@googlegroups.com
This may be possible to add code into Page_Frame_decorator->next_page() ( http://code.google.com/p/dompdf/source/browse/trunk/dompdf/include/page_frame_decorator.cls.php#112 )
Which would add a page, write all Ts&Cs, but "by hand", not from a document, and then add another page.
As Brian said, dompdf is very intertwined, but understandable if you take the time (a lot of time!).

Gabriel Harrison

unread,
Feb 9, 2012, 10:33:54 AM2/9/12
to dompd...@googlegroups.com
Thanks both of you. Any tips on what to read up on before I dive in to the code.

I was looking at the call back functions over the weekend to see if I
could intercept the pdf object as a new page was triggered. Was also
looking at a more crude solution where I aborted rendering and
restyled the page if it spilled on to a second page.

Gabriel

BrianS

unread,
Feb 24, 2012, 1:34:03 PM2/24/12
to dompd...@googlegroups.com

Any luck with this, Gabriel? The callback functions are more directed at text manipulation so I don't think those would help. But anything you've found would be interesting to know.

I haven't delved into code modification yet, but I have been toying around with how to create an object containing HTML-formatted text. This would make it easier to generate a T&C page. I've pretty much got that figured out, but it does leave me with some other issues to address to make this as painless as possible. First I need to figure out how to tell dompdf to reset the cursor (so to speak) after generating the object. Then I need to figure out how to insert new pages after each content page is generated.
Reply all
Reply to author
Forward
0 new messages