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;
- create a new pdf .
- import a specific page into the initiated pdf doc .
- 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!.