我用过
Mpdf
对于一个同样的问题。通过执行以下操作,您可以轻松创建新的pdf文档并导入pdf的现有页面:
$sourceFilePath = '/tmp/my-pdf-with-4-pages.pdf';
$destinationFilePath = 'output/my-pdf-with-3-pages.pdf';
$mpdfConfig = [];
$mpdf = new Mpdf($mpdfConfig);
// $mpdf->SetImportUse(); only necesseray for mpdf >=4 && <8
$pageCount = $mpdf->setSourceFile($sourceFilePath); // number of pages in your source pdf file
$pageNumbersToImport = [1,2,3];
$pageNumbersToImportCount = count($pageNumbersToImport);
// importing pages defined in $pageNumbersToImport
for ($pageNumber = 1; $pageNumber <= $pageCount; $pageNumber++) {
if (!in_array($pageNumber, $pageNumbersToImport, true)) {
continue;
}
$templateId = $mpdf->ImportPage($pageNumber); // get page content of page number as template of source pdf file
$mpdf->UseTemplate($templateId); // add page number of source file to current mpdf generated file
if ($pageNumber < $pageNumbersToImportCount) {
$mpdf->AddPage(); // add empty page
}
}
$mpdf->Output($destinationFilePath, 'F'); // write new pdf file, with pages 1-3 of source file