Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Functionality to Add PDFs from Amazon S3 #49

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions src/PDFMerger/PDFMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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
Expand Down