Skip to content

Commit

Permalink
Merge pull request #2 from maddhatter/duplex
Browse files Browse the repository at this point in the history
Duplex-Safe Merging
  • Loading branch information
Webklex authored May 4, 2017
2 parents cc87a04 + 2c3e8b4 commit 448f5ee
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion src/PDFMerger/PDFMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,14 +188,30 @@ 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.");
}

$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') {
Expand 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']]);
}
});
}
}

0 comments on commit 448f5ee

Please sign in to comment.