SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fpdi/fpdf, pdf pages importing, memory exhausted error

    Hi, I have a pdf file that has about 2000 pages. I am importing each page and saving that as a seperate pdf file.

    I am doing a loop to;
    1. create a new pdf .
    2. import a specific page into the initiated pdf doc .
    3. and then save the pdf.

    But at about 10 and odd pages, I get a memory exhausted error - Fatal error: Allowed memory size of 100663296 bytes exhausted (tried to allocate 42 bytes) in .../FPDI/pdf_parser.php on line 699 -

    Below is the script I am using;
    PHP Code:
    <?php
    //sample script for extracting pdf pages

    require_once("fpdf/fpdf.php");
    require_once(
    "FPDI/fpdi.php");

    print 
    '<br />Creating seperate pdf files for each page...'

    //initiate FPDI, initiate a new pdf file
    //p - portrait, mm - milimeter, A4
    $pdf =& new FPDI('p''in''A4'); 

    //add a page to the initiated pdf file
    $pdf->AddPage(); 

    //set the sourcefile, returns the total number of pages in the source pdf file
    $total_pages_src_pdf $pdf->setSourceFile(PATH_PDF_WITH_PAGES);

    for( 
    $i=1$i<=$total_pages_src_pdf;  $i++ ) {

        
    //create a new pdf for consecutive pages that are to be imported
        
    if( $i>) {
            
    $pdf =& new FPDI('p''in''A4');
            
    $pdf->AddPage();
            
    $pdf->setSourceFile(PATH_PDF_WITH_PAGES);
        }

        
    // import a particular page from the source file
        
    $tplIdx $pdf->importPage($i); 

        
    // use the imported page and place it at point 10,10 with a width of 100 mm 
        
    $pdf->useTemplate($tplIdx007.5010.00); 

        
    //left,top,[right]
        
    $pdf->SetMargins(0.500.50); 

        
    $filename_dst 'page_'.$i;
        
    //F - save to a local location
        
    print '<br />'.$i.') saving  '.$filename_dst.'.pdf';        
        
    $pdf->Output(PATH_EXTRACTED_PDF_FILE.'/'.$filename_dst.'.pdf''F');

        
    //close opened files, but they say Output(...) closes the file 
        //$pdf->close();

    //for

    print '<br /><br />...Done.'

    ?>
    Would appreciate any direction. Thanks for reading!.

  2. #2
    SitePoint Evangelist
    Join Date
    Sep 2006
    Posts
    428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't have an answer to your question, but I do have a bit of input.

    I spent the greater portion of last week trying to come up with a solution to producing pdf's from content displayed on a webpage (re-formatted/styled for pdf of course).

    I went through fpdf, htmltopdf and several other php classes and fought with each of them and was never really able to reliably produce the pdf's I needed. I ended up stumbling across wkhtmltopdf. It is a bit more involved to get started/installed with it but after that creating pdf's from html pages is a piece of cake. I would highly recommend it.

  3. #3
    SitePoint Member
    Join Date
    Feb 2010
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    rustybuddy,thanks for sharing that though.

    I tried explicitly calling the function that closes the pdf files and tried deleting the object (at the end of each iteration in the loop) ;

    PHP Code:
    ...
        
    //close opened files
           
    $pdf->_closeParsers();

        
    //deleting the pdf object
        
    $pdf->my_destructor();
        
    //print var_dump($pdf);
        //unset($pdf);

    //for

    print '<br /><br />...Done.'
    Defined the my_destructor() function in - class fpdi - as follows;

    PHP Code:
    function my_destructor() {
        
    settype(&$this'null' );

    $pdf->my_destructor() , doesn't seem delete or set null the object because calling -
    vardump($pdf) , after it shows a lot of content associated with the opened source pdf among other things.

    unset($pdf); doesn't help either.

    Anyone any clue?.

  4. #4
    Barefoot on the Moon! silver trophy
    Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,524
    Mentioned
    52 Post(s)
    Tagged
    1 Thread(s)
    Basically, what's happening is that the maximum_memory limit for PHP on the server has been hit, and you can't go beyond that limit unless you increase it.

    As for the code, you'll have to code it in such a way so that you can process each page individually and *not* accumulate more than a few pages in memory (in other words, in a variable or set of variables)
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •