creating PDF thru a function inside a loop

492 views
Skip to first unread message

dMc

unread,
May 7, 2009, 9:59:30 PM5/7/09
to dom...@googlegroups.com
Hi Guys, firstly great to see this group running - Dompdf rulz

im trying to create invoices thru a function that sits inside a foreach loop, my prob is that when i run the script it only generates a pdf for the last record pdf in the array,  is there anything obvious that i can do to clear the vars at the end of each loop?

im calling dompdf in the usual manner, and saving each pdf in a directory on the server.  a summary of my code can be seen below...


This is the code for the loop that calls my pdf function -------------------------

    $site_array[] = "11";
    $site_array[] = "1000";
    $site_array[] = "1021";

    require_once("/var/www/..../func.pdf_site_info.php");

    foreach($site_array as $siteKEY => $siteID){

        echo  make_site_pdf($siteID, $_GET["month"], $_GET["yr"]);   

    }   

This is a snippet from the function itself that renders the pdf -------------------------

    ini_set("memory_limit","256M");
    $dompdf = new DOMPDF();
    $dompdf->set_paper('a4', 'portrait');
    $dompdf->load_html($page_HTML);
    $dompdf->render();
    $pdf_string = $dompdf->output();
    $filepath = "/var/www/surfspot-admin/pages/financial/".$siteid."_".$report_month."_".$report_year.".pdf";
    file_put_contents("".$filepath."", $pdf_string);
    echo $filepath;

as you can see above ive tried bumping up the mem allocation for the script thinking that maybe it was using too much memory to run the whole thing but this wasnt the case.


Thanks in advance

~ Dan


BrianS

unread,
May 8, 2009, 11:38:15 AM5/8/09
to dompdf
I do something similar on a site I've developed and don't have any
problems. I don't really see anything here that's problematic, except
that by declaring a new DOMPDF object each call of the loop will
incrementally increase your memory usage. Depending on how complex
your invoices are that can certainly eat up a lot of memory. I would
recommend destroying DOMPDF when you're done with it in the function
with unset($dompdf).

Even so, I don't think you're experiencing a memory error since you're
getting the *last* PDF. If memory were the problem I would think you'd
get the first invoice since you're only getting one. PHP would by
erroring out before any others are generated.

There could be something else going on with the script, but I don't
see the cause in what you posted. Make sure that your file created is
actually unique for each PDF. Is the $filepath echoed for each invoice
and is it different for each one? You might also check to make sure
$site_array isn't being modified somehwere else.

-b

dMc

unread,
May 10, 2009, 7:40:42 PM5/10/09
to dom...@googlegroups.com
Hi Brian thanks for your response,

i was wrong initially it 'is' only spitting out the first item from the array but im not getting any errors displayed,  ive stripped the pdf virtually bare to test whether it is a memory issue and still no joy.  Id also tried unsetting the object like you suggested as well to no avail.

the $site_array isnt being used anywhere else either, do you think you could email me offlist the sample code youre using successfully maybe i can find something in there that im doing wrong with mine.  otherwise for the benefit of the group when i do find a solution to this ill post it through the usergroup when im done.

Thanks

~ Dan
Reply all
Reply to author
Forward
0 new messages