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
//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>1 ) {
		$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($tplIdx, 0, 0, 7.50, 10.00); 

	//left,top,[right]
	$pdf->SetMargins(0.50, 0.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!.

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.

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) ;

...
	//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;

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?.

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)