Jeckky / dompdf

Automatically exported from code.google.com/p/dompdf
0 stars 0 forks source link

Render multiple HTML segments #225

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Render the PDF multiple times with different sections of HTML.

I've implemented this by changing the dompdf class as seen in the attached 
diff. I added the methods I needed to an extended class because I wanted to 
change as little as possible in the base class. I've attached that extended 
class, as well.

For my purposes, I knew where all of my page breaks should be, so I was able to 
split my HTML based on that. Here's what my code looks like:

    $doc = new DOMDocument();
    $doc->loadHTML($html);

    $dompdf = new DOMPDF_ext();

    // Get the style section out of the HTML
    $styles = $doc->getElementsByTagName('style');
    $style = $styles->item(0);

    // Get all the divs with class page (separate pages)
    $xpath = new DOMXPath($doc);
    $pages = $xpath->query('//div[contains(@class, "page")]');

    // insert each page individually
    foreach($pages as $page) {
        $html = $doc->saveXML($style) . $doc->saveXML($page);
        $dompdf->insert_html($html);
    }

    $dompdf->stream($document_name . '.pdf');

Original issue reported on code.google.com by john.col...@eschoolconsultants.com on 1 Feb 2011 at 9:40

Attachments:

GoogleCodeExporter commented 9 years ago
Forgot to mention that this change was made to save memory. With this change, I 
can generate a 300 page PDF and only use 50MB of memory. Without the changes, 
it was easily over 300MB.

The discussion that led to this issue can be found here: 
http://groups.google.com/group/dompdf/browse_thread/thread/5a29cfd6b7918ef6

Original comment by john.col...@eschoolconsultants.com on 1 Feb 2011 at 9:45

GoogleCodeExporter commented 9 years ago
Hello, very simple clever hack!
And it seems very efficient. 
To go further, we could do something similar in order not to need to split the 
document.
The method would process the HTML document by looping over the <body>'s 
children and transform them into dompdf frames only when they have to be 
rendered. This way, we would only have in memory :
 - the HTML DOMDocument
 - a partially rendered PDF
 - a page of frames
 - their styles

Thank you for the patches! 

Original comment by fabien.menager on 2 Feb 2011 at 9:25

GoogleCodeExporter commented 9 years ago
It's great work.

Bu I have a problem with set_paper I can't change the auto page size , could 
you help me?

Original comment by anna.vak...@gmail.com on 22 Nov 2011 at 3:15

GoogleCodeExporter commented 9 years ago
I found the problem, text and image was too big for page size

Original comment by anna.vak...@gmail.com on 22 Nov 2011 at 4:29

GoogleCodeExporter commented 9 years ago
I'm having issues implementing your patch - what version of dompdf was this for?

Original comment by rpat...@novologic.com on 24 Jan 2012 at 7:12

GoogleCodeExporter commented 9 years ago
If you're using this workaround and have problems with UTF-8 support, the 
solution is using <html><head><meta http-equiv="Content-Type" 
content="text/html; charset=UTF-8" /><body> tags in each page.

Original comment by DariusLe...@gmail.com on 12 Apr 2012 at 10:34

GoogleCodeExporter commented 9 years ago

Original comment by eclecticgeek on 24 May 2013 at 3:00