-
-
Save silasrm/3da655045b899a858eae4f4463755f5c to your computer and use it in GitHub Desktop.
Split PDF to individual pages using FPDI and FPDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Split PDF file | |
* | |
* <p>Split all of the pages from a larger PDF files into | |
* single-page PDF files.</p> | |
* | |
* @package FPDF required http://www.fpdf.org/ | |
* @package FPDI required http://www.setasign.de/products/pdf-php-solutions/fpdi/ | |
* @param string $filename The filename of the PDF to split | |
* @param string $end_directory The end directory for split PDF (original PDF's directory by default) | |
* @return void | |
*/ | |
function split_pdf($filename, $end_directory = false) | |
{ | |
require_once('fpdf/fpdf.php'); | |
require_once('fpdi/fpdi.php'); | |
$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); | |
$new_pdf->useTemplate($new_pdf->importPage($i)); | |
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("filename.pdf", 'split/'); | |
?> |
Crazy this error with merged files. Report this case to SetaSign ou FPDF team.
The FPDF team replied
What you see is a consequence of the way FPDI works. When FPDI imports a page, it transforms it into an object (called XObject). So, when you merge 24 PDFs, you end up with a PDF that contains 24 XObjects.
Then, when you import a page from that PDF, I suppose that FPDI imports the 24 XObjects (because it doesn't know which ones are used by the imported page, and which ones are not, so it imports them all).
Olivier
Makes sense.
Humm. Yes. Report this bug to SetaSign and probably make fixed in future.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It seems merged PDFs with FPDF/FPDI have this behaviour. I split your original PDF, I get:
All good. Then I merged all those pages into a single PDF (with FPDF/FPDI), then tried to split that PDF again:
If I open each of the page I see the actual page, all of them are ok. When doing binary comparison of these files I get something like
Only 3 bytes different.