1

i have a pdf upload script with php, my question is when user uploads a multipage pdf file , i want this to split in to individual pdf's. for instance if the pdf has 3 pages, the result should be 1.pdf , 2.pdf, 3.pdf etc.

for example convert -density 300 filename.pdf filename.png works fine creating a png file , but i want the same in to pdf files.

Display name
  • 420
  • 5
  • 20

1 Answers1

2

You can use a FPDF and FPDI to do this.

require_once('fpdf/fpdf.php');
require_once('fpdi/fpdi.php');

// get the page count
$pdf = new FPDI();
$pageCount = $pdf->setSourceFile($pdfFilePath);

// iterate through all pages
for($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++)
{
    // create blank document
    $pdf = new FPDI();

    // import a page
    $pdf->setSourceFile($pdfFilePath);
    $templateId = $pdf->importPage($pageNumber);

    // get the size of the imported page
    $size = $pdf->getTemplateSize($templateId);

    // create a page (landscape or portrait depending on the page being imported)
    if($size['w'] > $size['h'])
    {
        $pdf->AddPage('L', array($size['w'], $size['h']));
    }
    else
    {   $pdf->AddPage('P', array($size['w'], $size['h']));
    }

    // use the imported page
    $pdf->useTemplate($templateId);

    // write the PDF file
    $pdf->Output(('/path/to/save/'.$pageNumber.'pdf'), 'F');
}
Peter
  • 138
  • 3