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

Replacement for PHP base Exception class #32

Open
wants to merge 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,6 @@ pip-log.txt

# Mac crap
.DS_Store

# JetBrains PHPStorm
.idea
7 changes: 7 additions & 0 deletions Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* This file is placed here for compatibility with Zendframework 2's ModuleManager.
* It allows usage of this module even without composer.
* The original Module.php is in 'src/DoctrineDataFixtureModule' in order to respect PSR-0
*/
require_once __DIR__ . '/src/PDFMerger/Module.php';
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "vaza18/pdfmerger",
"name": "matiasiglesias/pdfmerger",
"description": "PDFMerger for PHP (PHP5 Compatible) by Jarrod Nettles",
"require":{
"php": ">=5.2.0"
Expand Down
24 changes: 24 additions & 0 deletions src/PDFMerger/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace matiasiglesias;

use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;

/**
* Class Module
* @package matiasiglesias
* @author Matias Iglesias <[email protected]>
*/
class Module
{
/**
* @param MvcEvent $e
*/
public function onBootstrap(MvcEvent $e)
{
$eventManager = $e->getApplication()->getEventManager();
$moduleRouteListener = new ModuleRouteListener();
$moduleRouteListener->attach($eventManager);
}

}
53 changes: 29 additions & 24 deletions PDFMerger.php → src/PDFMerger/PDFMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
* - essentially, it cannot import dynamic content such as form fields, links
* or page annotations (anything not a part of the page content stream).
*/

namespace matiasiglesias;

class PDFMerger
{
private $_files; //['form.pdf'] ["1,2,4, 5-19"]
Expand All @@ -47,12 +50,13 @@ public function __construct()
require_once('tcpdf/tcpdi.php');
}

/**
* Add a PDF for inclusion in the merge with a valid file path. Pages should be formatted: 1,3,6, 12-16.
* @param $filepath
* @param $pages
* @return void
*/
/**
* Add a PDF for inclusion in the merge with a valid file path. Pages should be formatted: 1,3,6, 12-16.
* @param $filepath
* @param string $pages
* @return PDFMerger
* @throws ExceptionÆ
*/
public function addPDF($filepath, $pages = 'all')
{
if(file_exists($filepath))
Expand All @@ -66,21 +70,22 @@ public function addPDF($filepath, $pages = 'all')
}
else
{
throw new exception("Could not locate PDF on '$filepath'");
throw new Exception("Could not locate PDF on '$filepath'");
}

return $this;
}

/**
* Merges your provided PDFs and outputs to specified location.
* @param $outputmode
* @param $outputname
* @return PDF
*/
/**
* Merges your provided PDFs and outputs to specified location.
* @param $outputmode
* @param $outputpath
* @return PDF | boolean
* @throws Exception
*/
public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf')
{
if(!isset($this->_files) || !is_array($this->_files)): throw new exception("No PDFs to merge."); endif;
if(!isset($this->_files) || !is_array($this->_files)): throw new Exception("No PDFs to merge."); endif;

$fpdi = new TCPDI;
$fpdi->SetPrintHeader(false);
Expand Down Expand Up @@ -111,7 +116,7 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf')
{
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."); endif;
if(!$template = $fpdi->importPage($page)): throw new Exception("Could not load page '$page' in PDF '$filename'. Check that the page exists."); endif;
$size = $fpdi->getTemplateSize($template);
$orientation = ($size['h'] > $size['w']) ? 'P' : 'L';

Expand Down Expand Up @@ -141,8 +146,7 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf')
}
else
{
throw new exception("Error outputting PDF to '$outputmode'.");
return false;
throw new Exception("Error outputting PDF to '$outputmode'.");
}
}

Expand All @@ -152,7 +156,7 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf')
/**
* FPDI uses single characters for specifying the output location. Change our more descriptive string into proper format.
* @param $mode
* @return Character
* @return string
*/
private function _switchmode($mode)
{
Expand All @@ -176,11 +180,12 @@ private function _switchmode($mode)
}
}

/**
* Takes our provided pages in the form of 1,3,4,16-50 and creates an array of all pages
* @param $pages
* @return unknown_type
*/
/**
* Takes our provided pages in the form of 1,3,4,16-50 and creates an array of all pages
* @param $pages
* @return unknown_type
* @throws Exception
*/
private function _rewritepages($pages)
{
$pages = str_replace(' ', '', $pages);
Expand All @@ -196,7 +201,7 @@ private function _rewritepages($pages)
$x = $ind[0]; //start page
$y = $ind[1]; //end page

if($x > $y): throw new exception("Starting page, '$x' is greater than ending page '$y'."); return false; endif;
if($x > $y): throw new Exception("Starting page, '$x' is greater than ending page '$y'."); return false; endif;

//add middle pages
while($x <= $y): $newpages[] = (int) $x; $x++; endwhile;
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading