From 7f343fd1c3f58263c3374c773b85aaea16a30956 Mon Sep 17 00:00:00 2001 From: Maximilian Kresse <545671+MaximilianKresse@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:28:11 +0200 Subject: [PATCH 1/2] Added support for importing links --- composer.json | 2 +- src/PDFMerger/PDFMerger.php | 13 ++++++++----- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 87a41b5..deffbc6 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "php": ">=5.5.9", "illuminate/support": ">=5.0", "setasign/fpdf": "1.8.*", - "setasign/fpdi": "^2.0" + "setasign/fpdi": "^2.4" }, "require-dev": { "phpunit/phpunit": "4.*", diff --git a/src/PDFMerger/PDFMerger.php b/src/PDFMerger/PDFMerger.php index 05401c9..92dcf0c 100755 --- a/src/PDFMerger/PDFMerger.php +++ b/src/PDFMerger/PDFMerger.php @@ -18,6 +18,7 @@ use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Http\Response; +use setasign\Fpdi\PdfReader\PageBoundaries; class PDFMerger { @@ -120,7 +121,7 @@ public function download(){ * @return string */ public function save($filePath = null){ - return $this->oFilesystem->put($filePath?$filePath:$this->fileName, $this->output()); + return $this->oFilesystem->put($filePath?:$this->fileName, $this->output()); } /** @@ -227,7 +228,7 @@ protected function doMerge($orientation, $duplexSafe) { if ($file['pages'] == 'all') { for ($i = 1; $i <= $count; $i++) { - $template = $oFPDI->importPage($i); + $template = $oFPDI->importPage($i, PageBoundaries::CROP_BOX, true, true); $size = $oFPDI->getTemplateSize($template); $autoOrientation = isset($file['orientation']) ? $file['orientation'] : $size['orientation']; @@ -236,8 +237,10 @@ protected function doMerge($orientation, $duplexSafe) { } } else { foreach ($file['pages'] as $page) { - if (!$template = $oFPDI->importPage($page)) { - throw new \Exception("Could not load page '$page' in PDF '" . $file['name'] . "'. Check that the page exists."); + if (!$template = $oFPDI->importPage($page, PageBoundaries::CROP_BOX, true, true)) { + throw new \Exception( + "Could not load page '$page' in PDF '" . $file['name'] . "'. Check that the page exists." + ); } $size = $oFPDI->getTemplateSize($template); $autoOrientation = isset($file['orientation']) ? $file['orientation'] : $size['orientation']; @@ -247,7 +250,7 @@ protected function doMerge($orientation, $duplexSafe) { } } - if ($duplexSafe && $oFPDI->page % 2) { + if ($duplexSafe && $oFPDI->PageNo() % 2) { $oFPDI->AddPage($file['orientation'], [$size['width'], $size['height']]); } }); From fb0ce9c4a5a52feab5068e66529d4b702ce23994 Mon Sep 17 00:00:00 2001 From: Maximilian Kresse <545671+MaximilianKresse@users.noreply.github.com> Date: Thu, 4 Jul 2024 15:30:08 +0200 Subject: [PATCH 2/2] Added support for parserParams The parserParams can be used by the FPDI PDF-Parser to handle encrypted files (more to this here: https://manuals.setasign.com/fpdi-pdf-parser-manual/v2/protected-pdf-documents/). --- src/PDFMerger/PDFMerger.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/PDFMerger/PDFMerger.php b/src/PDFMerger/PDFMerger.php index 92dcf0c..fe0a1e7 100755 --- a/src/PDFMerger/PDFMerger.php +++ b/src/PDFMerger/PDFMerger.php @@ -146,32 +146,38 @@ public function setFileName($fileName){ /** * Set the final filename + * * @param string $string * @param mixed $pages * @param mixed $orientation + * @param array $parserParams Individual parameters passed to the parser instance. Can be used for the + * FPDI PDF-Parser to handle encrypted pdfs. * * @return string */ - public function addString($string, $pages = 'all', $orientation = null){ + public function addString($string, $pages = 'all', $orientation = null, $parserParams = []){ $filePath = storage_path('tmp/'.Str::random(16).'.pdf'); $this->oFilesystem->put($filePath, $string); $this->tmpFiles->push($filePath); - return $this->addPDF($filePath, $pages, $orientation); + return $this->addPDF($filePath, $pages, $orientation, $parserParams); } /** * Add a PDF for inclusion in the merge with a valid file path. Pages should be formatted: 1,3,6, 12-16. + * * @param string $filePath * @param string $pages * @param string $orientation + * @param array $parserParams Individual parameters passed to the parser instance. Can be used for the + * FPDI PDF-Parser to handle encrypted pdfs. * * @return self * * @throws \Exception if the given pages aren't correct */ - public function addPDF($filePath, $pages = 'all', $orientation = null) { + public function addPDF($filePath, $pages = 'all', $orientation = null, $parserParams = []) { if (file_exists($filePath)) { if (!is_array($pages) && strtolower($pages) != 'all') { throw new \Exception($filePath."'s pages could not be validated"); @@ -180,7 +186,8 @@ public function addPDF($filePath, $pages = 'all', $orientation = null) { $this->aFiles->push([ 'name' => $filePath, 'pages' => $pages, - 'orientation' => $orientation + 'orientation' => $orientation, + 'parserParams' => $parserParams ]); } else { throw new \Exception("Could not locate PDF on '$filePath'"); @@ -223,7 +230,10 @@ protected function doMerge($orientation, $duplexSafe) { $this->aFiles->each(function($file) use($oFPDI, $orientation, $duplexSafe){ $file['orientation'] = is_null($file['orientation'])?$orientation:$file['orientation']; - $count = $oFPDI->setSourceFile(StreamReader::createByString(file_get_contents($file['name']))); + $count = $oFPDI->setSourceFileWithParserParams( + StreamReader::createByString(file_get_contents($file['name'])), + $file['parserParams'] + ); if ($file['pages'] == 'all') {