Skip to content
Open
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
66 changes: 42 additions & 24 deletions src/PDFMerger/PDFMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@

use Exception;
use setasign\Fpdi\Fpdi;
use setasign\Fpdi\FpdiException;

class PDFMerger
{
private $_files; //['form.pdf'] ["1,2,4, 5-19"]
private $_fpdi;
private $ignoreFpdiException = FALSE;

/**
* Add a PDF for inclusion in the merge with a valid file path. Pages should be formatted: 1,3,6, 12-16.
Expand All @@ -52,6 +54,14 @@ public function addPDF($filepath, $pages = 'all', $orientation = null)
return $this;
}

/**
* Ignore ignoreFpdiException exceptions (e.g. if files are encrypted) during merge.
* @param bool $ignore
*/
public function ignoreFpdiException($ignore = TRUE) {
$this->ignoreFpdiException = $ignore;
}

/**
* Merges your provided PDFs and outputs to specified location.
* @param $outputmode
Expand All @@ -69,34 +79,42 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf', $ori

// merger operations
foreach ($this->_files as $file) {
$filename = $file[0];
$filepages = $file[1];
$fileorientation = (!is_null($file[2])) ? $file[2] : $orientation;

$count = $fpdi->setSourceFile($filename);

//add the pages
if ($filepages == 'all') {
for ($i=1; $i<=$count; $i++) {
$template = $fpdi->importPage($i);
$size = $fpdi->getTemplateSize($template);
if ($fileorientation === 'A') {
$fileorientation = ($size['width'] > $size['height']) ? 'L' : 'P';
try {
$filename = $file[0];
$filepages = $file[1];
$fileorientation = (!is_null($file[2])) ? $file[2] : $orientation;

$count = $fpdi->setSourceFile($filename);

//add the pages
if ($filepages == 'all') {
for ($i=1; $i<=$count; $i++) {
$template = $fpdi->importPage($i);
$size = $fpdi->getTemplateSize($template);
if ($fileorientation === 'A') {
$fileorientation = ($size['width'] > $size['height']) ? 'L' : 'P';
}
$fpdi->AddPage($fileorientation, array($size['width'], $size['height']));
$fpdi->useTemplate($template);
}
$fpdi->AddPage($fileorientation, array($size['width'], $size['height']));
$fpdi->useTemplate($template);
}
} else {
foreach ($filepages as $page) {
if (!$template = $fpdi->importPage($page)) {
throw new Exception("Could not load page '$page' in PDF '$filename'. Check that the page exists.");
} else {
foreach ($filepages as $page) {
if (!$template = $fpdi->importPage($page)) {
throw new Exception("Could not load page '$page' in PDF '$filename'. Check that the page exists.");
}
$size = $fpdi->getTemplateSize($template);

$fpdi->AddPage($fileorientation, array($size['width'], $size['height']));
$fpdi->useTemplate($template);
}
$size = $fpdi->getTemplateSize($template);

$fpdi->AddPage($fileorientation, array($size['width'], $size['height']));
$fpdi->useTemplate($template);
}
}
catch (FpdiException $e) {
if ($this->ignoreFpdiException) {
continue;
}
throw $e;
}
}

//output operations
Expand Down