From 93913b8d089b7f6c6698b16e8169bd94f86e2187 Mon Sep 17 00:00:00 2001 From: nipunTharuksha Date: Thu, 2 May 2024 00:24:57 +0530 Subject: [PATCH 1/2] Added Functionality to Add PDFs from Amazon S3 --- src/PDFMerger/PDFMerger.php | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/PDFMerger/PDFMerger.php b/src/PDFMerger/PDFMerger.php index 89628ae..4e92d7e 100755 --- a/src/PDFMerger/PDFMerger.php +++ b/src/PDFMerger/PDFMerger.php @@ -12,12 +12,14 @@ namespace Webklex\PDFMerger; +use Illuminate\Support\Carbon; use setasign\Fpdi\Fpdi as FPDI; use setasign\Fpdi\PdfParser\StreamReader; use Illuminate\Filesystem\Filesystem; use Illuminate\Support\Collection; use Illuminate\Support\Str; use Illuminate\Http\Response; +use Illuminate\Support\Facades\Storage; class PDFMerger { @@ -159,6 +161,38 @@ public function addString($string, $pages = 'all', $orientation = null){ return $this->addPDF($filePath, $pages, $orientation); } + /** + * Add a PDF from S3 + * + * @param string $filePath + * @param string $pages + * @param string|null $orientation + * @param int $tempURLMinutes + * + * @return self + * + * @throws \Exception if the given pages aren't correct or the file cannot be located + */ + public function addPDFFromS3($filePath, $pages = 'all', $orientation = null, $tempURLMinutes = 5) { + if (Storage::disk('s3')->exists($filePath)) { + if (!is_array($pages) && strtolower($pages) != 'all') { + throw new \Exception("Invalid format for pages: '$pages'"); + } + + $temporaryUrl = Storage::disk('s3')->temporaryUrl($filePath, Carbon::now()->addMinutes($tempURLMinutes)); + + $this->aFiles->push([ + 'name' => $temporaryUrl, + 'pages' => $pages, + 'orientation' => $orientation + ]); + } else { + throw new \Exception("Could not locate PDF on '$filePath'"); + } + + return $this; + } + /** * 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 From 6ad630419e70bfef8ef55051bbbbcfef89806854 Mon Sep 17 00:00:00 2001 From: nipunTharuksha Date: Thu, 2 May 2024 00:30:59 +0530 Subject: [PATCH 2/2] Read me file updated --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 89e2802..d4aef5e 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,13 @@ $oMerger->addString(file_get_contents('/path/to/project/vendors/webklex/laravel- ``` +...add files from s3: + +``` php +$oMerger->addPDFFromS3('/path/examples/pdf_two.pdf'), [1]); + +``` + ...select the pages you want to merge: ``` php