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

option to add image #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
48 changes: 33 additions & 15 deletions src/PDFMerger/PDFMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* such as form fields, links or page annotations (anything not a part of the page content stream).
*
*/

namespace Clegginabox\PDFMerger;

use Exception;
Expand All @@ -44,14 +45,26 @@ public function addPDF($filepath, $pages = 'all', $orientation = null)
$pages = $this->_rewritepages($pages);
}

$this->_files[] = array($filepath, $pages, $orientation);
$this->_files[] = array($filepath, $pages, $orientation, 'pdf');
} else {
throw new Exception("Could not locate PDF on '$filepath'");
}

return $this;
}

public function addImage($filepath)
{
if (file_exists($filepath)) {
$this->_files[] = array($filepath, 'all', 'A', 'image');
} else {
throw new Exception("Could not locate Image on '$filepath'");
}

return $this;
}


/**
* Merges your provided PDFs and outputs to specified location.
* @param $outputmode
Expand All @@ -68,23 +81,29 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf', $ori
$fpdi = new Fpdi();

// merger operations
foreach ($this->_files as $file) {
$filename = $file[0];
foreach ($this->_files as $index => $file) {
$filename = $file[0];
$filepages = $file[1];
$fileType = $file[3];
$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';
if ($fileType === 'pdf') {
$count = $fpdi->setSourceFile($filename);
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 {
$fpdi->addPage();
$fpdi->Image($filename, 0, 0);
}
} else {
foreach ($filepages as $page) {
Expand Down Expand Up @@ -123,8 +142,7 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf', $ori
*/
private function _switchmode($mode)
{
switch(strtolower($mode))
{
switch (strtolower($mode)) {
case 'download':
return 'D';
break;
Expand Down Expand Up @@ -168,11 +186,11 @@ private function _rewritepages($pages)

//add middle pages
while ($x <= $y) {
$newpages[] = (int) $x;
$newpages[] = (int)$x;
$x++;
}
} else {
$newpages[] = (int) $ind[0];
$newpages[] = (int)$ind[0];
}
}

Expand Down