From 2c3e8b4c69831089f7aeda0b0e7a3689acc63f12 Mon Sep 17 00:00:00 2001 From: Shawn Tunney Date: Wed, 3 May 2017 10:26:47 -0400 Subject: [PATCH] Add duplex merging --- CHANGELOG.md | 11 ++++++++++- README.md | 5 +++++ src/PDFMerger/PDFMerger.php | 22 +++++++++++++++++++++- 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6576e44..b3ee4b0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,16 @@ All notable changes to `webklex/laravel-pdfmerger` will be documented in this fi Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. -## [UNRELEASED] +## [Unreleased] + +### Added + - Added `duplexMerge` to support duplex-safe merging + +## [1.0.0] (2017-02-17) ### Added - new laravel-pdfmerger package + + +[Unreleased]: https://github.com/Webklex/laravel-pdfmerger +[1.0.0]: https://github.com/Webklex/laravel-pdfmerger/releases/tag/1.0.0 \ No newline at end of file diff --git a/README.md b/README.md index 59b8084..a632692 100644 --- a/README.md +++ b/README.md @@ -63,6 +63,11 @@ $oMerger->addPDF($file, [1, 3]); //Add page one and three only ``` +...merge files together but add blank pages to support duplex printing +```php +$oMerger->duplexMerge(); +``` + ...stream the merged content: ``` php diff --git a/src/PDFMerger/PDFMerger.php b/src/PDFMerger/PDFMerger.php index 188af13..a34750f 100755 --- a/src/PDFMerger/PDFMerger.php +++ b/src/PDFMerger/PDFMerger.php @@ -188,6 +188,22 @@ public function addPDF($filePath, $pages = 'all', $orientation = null) { * @throws \Exception if there are now PDFs to merge */ public function merge($orientation = 'P') { + $this->doMerge($orientation, false); + } + + /** + * Merges your provided PDFs and adds blank pages between documents as needed to allow duplex printing + * @param string $orientation + * + * @return void + * + * @throws \Exception if there are now PDFs to merge + */ + public function duplexMerge($orientation = 'P') { + $this->doMerge($orientation, true); + } + + protected function doMerge($orientation, $duplexSafe) { if ($this->aFiles->count() == 0) { throw new \Exception("No PDFs to merge."); @@ -195,7 +211,7 @@ public function merge($orientation = 'P') { $oFPDI = $this->oFPDI; - $this->aFiles->each(function($file) use($oFPDI, $orientation){ + $this->aFiles->each(function($file) use($oFPDI, $orientation, $duplexSafe){ $file['orientation'] = is_null($file['orientation'])?$orientation:$file['orientation']; $count = $oFPDI->setSourceFile($file['name']); if ($file['pages'] == 'all') { @@ -218,6 +234,10 @@ public function merge($orientation = 'P') { $oFPDI->useTemplate($template); } } + + if ($duplexSafe && $oFPDI->page % 2) { + $oFPDI->AddPage($file['orientation'], [$size['w'], $size['h']]); + } }); } }