在PHP中拆分PDF文件,读取它并返回结果

Here's how the system should work:

  1. Admin uploads a multi-page PDF file, in which every page contains a unique code for every user.
  2. The system should split the file into separate pages.
  3. User logs in. The system gets his ID as a variable (the unique code which exists in PDF file).
  4. The system shows every user only those pages which contain their ID.

I'm not asking for any code, just the name of some PDF libraries which can do the exact thing I'm looking for :)

Thanks in advance.

You may try this function to split all of the pages from a larger PDF file into single-page PDF files.

Installation

composer require setasign/fpdi-fpdf

Code

use setasign\Fpdi\Fpdi;

/**
 * Split all of the pages from a larger PDF file into single-page PDF files.
 *
 * @param string $filename The filename of the PDF to split
 * @param string $directory The output directory for the new PDF files
 *
 * @return void
 */
function split_pdf(string $filename, string $directory)
{
    $pdf = new Fpdi();
    $pageCount = $pdf->setSourceFile($filename);
    $file = pathinfo($filename, PATHINFO_FILENAME);

    // Split each page into a new PDF
    for ($i = 1; $i <= $pageCount; $i++) {
        $newPdf = new Fpdi();
        $newPdf->addPage();
        $newPdf->setSourceFile($filename);
        $newPdf->useTemplate($newPdf->importPage($i));

        $newFilename = sprintf('%s/%s_%s.pdf', $directory, $file, $i);
        $newPdf->output($newFilename, 'F');
    }
}

Usage

split_pdf('test.pdf', 'split/');