Skip to content

Instantly share code, notes, and snippets.

@maccath
Created October 30, 2012 16:09
Show Gist options
  • Star 39 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save maccath/3981205 to your computer and use it in GitHub Desktop.
Save maccath/3981205 to your computer and use it in GitHub Desktop.
Split PDF to individual pages using FPDI and FPDF
<?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/');
?>
@flipflopsimsommer
Copy link

thx, 👍 awsome.
don't forget

   $pdf->close();

alter your loop.

@tsujitpathak
Copy link

why some version of pdf are not being splitted ?

@sunnypatial
Copy link

Hi Sir,

Thanks for this script.
But i have a query here. Can we split all pdf files by calling 1 function.
For eg:
Currently we are using $new_pdf->importPage($i)
We are passing page number in the importPage method. Can we pass all pages only one time. So that there will be no need to use for loop in here.

@theangerofman
Copy link

maccath, thank you for a great script.

@kmeziere
Copy link

Awesome.

My time and I are saying you THANK YOU.

@ddval
Copy link

ddval commented Mar 1, 2016

Hi Sir.

My files always stop at random pages and not fall over from it.

Can someone help me?

Thanks.

@obakary
Copy link

obakary commented Jun 21, 2016

Thank you for this script. It allowed me to save time.

@abeeku
Copy link

abeeku commented Sep 10, 2016

Would this work for .epub files

@janzankowski
Copy link

Note that free version of FPDI only works with PDF versions up to 1.4 - see https://www.setasign.com/products/fpdi-pdf-parser/details/

@ilkermutlu
Copy link

@janzankowski here's some messy solution I've come with to that: https://gist.github.com/ilkermutlu/c38a72ecb60a1a9d65983b2528d5e69e

@phpshubham
Copy link

how to split for some range of pdf

@CrazyGoGo
Copy link

CrazyGoGo commented Mar 30, 2020

Splitted pages are truncated, only the top left of the original document appears

@dimas-afpurn
Copy link

how to set in codeigniter?

@wovosoft
Copy link

Set Adjust page size : true

$new_pdf->useTemplate($new_pdf->importPage($i), adjustPageSize: true);

@KhanhNS
Copy link

KhanhNS commented Dec 28, 2023

FPDI can only handle PDF document up to PDF ver 1.4. Be careful when sett source pdf file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment