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 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