I have a 150Mb pdf (55 pages, containing text and images) generated with FPDF.
I would like to split this PDF into single pages PDF.
I use FPDI, but I have a major issue, each single page PDF is 150Mb (juste like the original pdf).
Here is my code :
use setasign\Fpdi\Fpdi;
require('fpdf181/fpdf.php');
require('fpdi/autoload.php');
function split_pdf($filename, $end_directory = false)
{
$end_directory = $end_directory ? $end_directory : './';
$new_path = preg_replace('/[\/]+/', '/', $end_directory.'/'.substr($filename, 0, strrpos($filename, '/')));
if (!is_dir($new_path))
{
// Will make directories under end directory that don't exist
// Provided that end directory exists and has the right permissions
mkdir($new_path, 0777, true);
}
$pdf = new FPDI();
$pagecount = $pdf->setSourceFile($filename); // How many pages?
// Split each page into a new PDF
for ($i = 1; $i <= $pagecount; $i++) {
$new_pdf = new FPDI();
$new_pdf->AddPage();
$new_pdf->setSourceFile($filename);
$templateIndex = $new_pdf->importPage($i);
$new_pdf->useTemplate($templateIndex, null, null, 0, 0, true);
try {
$new_filename = $end_directory.str_replace('.pdf', '', $filename).'_'.$i.".pdf";
$new_pdf->Output($new_filename, "F");
echo "Page ".$i." split into ".$new_filename."<br />\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
}
// Create and check permissions on end directory!
split_pdf("contract.pdf", 'split/');
My original PDF only embeds PNGs and Helvetica text.
Thanks in advance for any help :)