Reputation: 1693
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 :)
Upvotes: 0
Views: 186
Reputation: 5058
FPDF uses a single resource dictionary, which means that all resources such as images, fonts or other imported pages (via FPDI) are located at a single place. A page refer to this dictionary as it's source of resources no matter if the resources are used on the specific page or not.
FPDI simply copies the resource dictionary when importing a page, including all defined resources. It does not analyze the page content to decide which resources can be ignored or not.
It is impossible to solve this problem with FPDI (as long as anybody would write an extension for this).
This problem is a common problem for any tool that combines or split PDF documents. We (Setasign - also authors of FPDI) also have this problem with another merger/split tool but we were able to write a script, that optimizes the resources. Maybe this solution may help you. Just see here. This solution is not free.
Upvotes: 1