forked from rguedes/PDFMerger
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Fúlvio Carvalhido
committed
Mar 25, 2014
0 parents
commit 8377f7f
Showing
126 changed files
with
14,630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto | ||
|
||
# Custom for Visual Studio | ||
*.cs diff=csharp | ||
*.sln merge=union | ||
*.csproj merge=union | ||
*.vbproj merge=union | ||
*.fsproj merge=union | ||
*.dbproj merge=union | ||
|
||
# Standard to msysgit | ||
*.doc diff=astextplain | ||
*.DOC diff=astextplain | ||
*.docx diff=astextplain | ||
*.DOCX diff=astextplain | ||
*.dot diff=astextplain | ||
*.DOT diff=astextplain | ||
*.pdf diff=astextplain | ||
*.PDF diff=astextplain | ||
*.rtf diff=astextplain | ||
*.RTF diff=astextplain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
################# | ||
## Eclipse | ||
################# | ||
|
||
*.pydevproject | ||
.project | ||
.metadata | ||
bin/ | ||
tmp/ | ||
*.tmp | ||
*.bak | ||
*.swp | ||
*~.nib | ||
local.properties | ||
.classpath | ||
.settings/ | ||
.loadpath | ||
|
||
# External tool builders | ||
.externalToolBuilders/ | ||
|
||
# Locally stored "Eclipse launch configurations" | ||
*.launch | ||
|
||
# CDT-specific | ||
.cproject | ||
|
||
# PDT-specific | ||
.buildpath | ||
|
||
|
||
################# | ||
## Visual Studio | ||
################# | ||
|
||
## Ignore Visual Studio temporary files, build results, and | ||
## files generated by popular Visual Studio add-ons. | ||
|
||
# User-specific files | ||
*.suo | ||
*.user | ||
*.sln.docstates | ||
|
||
# Build results | ||
[Dd]ebug/ | ||
[Rr]elease/ | ||
*_i.c | ||
*_p.c | ||
*.ilk | ||
*.meta | ||
*.obj | ||
*.pch | ||
*.pdb | ||
*.pgc | ||
*.pgd | ||
*.rsp | ||
*.sbr | ||
*.tlb | ||
*.tli | ||
*.tlh | ||
*.tmp | ||
*.vspscc | ||
.builds | ||
*.dotCover | ||
|
||
## TODO: If you have NuGet Package Restore enabled, uncomment this | ||
#packages/ | ||
|
||
# Visual C++ cache files | ||
ipch/ | ||
*.aps | ||
*.ncb | ||
*.opensdf | ||
*.sdf | ||
|
||
# Visual Studio profiler | ||
*.psess | ||
*.vsp | ||
|
||
# ReSharper is a .NET coding add-in | ||
_ReSharper* | ||
|
||
# Installshield output folder | ||
[Ee]xpress | ||
|
||
# DocProject is a documentation generator add-in | ||
DocProject/buildhelp/ | ||
DocProject/Help/*.HxT | ||
DocProject/Help/*.HxC | ||
DocProject/Help/*.hhc | ||
DocProject/Help/*.hhk | ||
DocProject/Help/*.hhp | ||
DocProject/Help/Html2 | ||
DocProject/Help/html | ||
|
||
# Click-Once directory | ||
publish | ||
|
||
# Others | ||
[Bb]in | ||
[Oo]bj | ||
sql | ||
TestResults | ||
*.Cache | ||
ClientBin | ||
stylecop.* | ||
~$* | ||
*.dbmdl | ||
Generated_Code #added for RIA/Silverlight projects | ||
|
||
# Backup & report files from converting an old project file to a newer | ||
# Visual Studio version. Backup files are not needed, because we have git ;-) | ||
_UpgradeReport_Files/ | ||
Backup*/ | ||
UpgradeLog*.XML | ||
|
||
|
||
|
||
############ | ||
## Windows | ||
############ | ||
|
||
# Windows image file caches | ||
Thumbs.db | ||
|
||
# Folder config file | ||
Desktop.ini | ||
|
||
|
||
############# | ||
## Python | ||
############# | ||
|
||
*.py[co] | ||
|
||
# Packages | ||
*.egg | ||
*.egg-info | ||
dist | ||
build | ||
eggs | ||
parts | ||
bin | ||
var | ||
sdist | ||
develop-eggs | ||
.installed.cfg | ||
|
||
# Installer logs | ||
pip-log.txt | ||
|
||
# Unit test / coverage reports | ||
.coverage | ||
.tox | ||
|
||
#Translations | ||
*.mo | ||
|
||
#Mr Developer | ||
.mr.developer.cfg | ||
|
||
# Mac crap | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
<?php | ||
/** | ||
* PDFMerger created by Jarrod Nettles December 2009 | ||
* [email protected] | ||
* | ||
* v1.0 | ||
* | ||
* Class for easily merging PDFs (or specific pages of PDFs) together into one. Output to a file, browser, download, or return as a string. | ||
* Unfortunately, this class does not preserve many of the enhancements your original PDF might contain. It treats | ||
* your PDF page as an image and then concatenates them all together. | ||
* | ||
* Note that your PDFs are merged in the order that you provide them using the addPDF function, same as the pages. | ||
* If you put pages 12-14 before 1-5 then 12-15 will be placed first in the output. | ||
* | ||
* | ||
* Uses FPDI 1.3.1 from Setasign | ||
* Uses FPDF 1.6 by Olivier Plathey with FPDF_TPL extension 1.1.3 by Setasign | ||
* | ||
* Both of these packages are free and open source software, bundled with this class for ease of use. | ||
* They are not modified in any way. PDFMerger has all the limitations of the FPDI package - essentially, it cannot import dynamic content | ||
* such as form fields, links or page annotations (anything not a part of the page content stream). | ||
* | ||
*/ | ||
class PDFMerger | ||
{ | ||
private $_files; //['form.pdf'] ["1,2,4, 5-19"] | ||
private $_fpdi; | ||
|
||
/** | ||
* Merge PDFs. | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
require_once('fpdf/fpdf.php'); | ||
require_once('fpdi/fpdi.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 | ||
*/ | ||
public function addPDF($filepath, $pages = 'all') | ||
{ | ||
if(file_exists($filepath)) | ||
{ | ||
if(strtolower($pages) != 'all') | ||
{ | ||
$pages = $this->_rewritepages($pages); | ||
} | ||
|
||
$this->_files[] = array($filepath, $pages); | ||
} | ||
else | ||
{ | ||
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 | ||
*/ | ||
public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf') | ||
{ | ||
if(!isset($this->_files) || !is_array($this->_files)): throw new exception("No PDFs to merge."); endif; | ||
|
||
$fpdi = new FPDI; | ||
|
||
//merger operations | ||
foreach($this->_files as $file) | ||
{ | ||
$filename = $file[0]; | ||
$filepages = $file[1]; | ||
|
||
$count = $fpdi->setSourceFile($filename); | ||
|
||
//add the pages | ||
if($filepages == 'all') | ||
{ | ||
for($i=1; $i<=$count; $i++) | ||
{ | ||
$template = $fpdi->importPage($i); | ||
$size = $fpdi->getTemplateSize($template); | ||
|
||
$fpdi->AddPage('P', array($size['w'], $size['h'])); | ||
$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."); endif; | ||
$size = $fpdi->getTemplateSize($template); | ||
|
||
$fpdi->AddPage('P', array($size['w'], $size['h'])); | ||
$fpdi->useTemplate($template); | ||
} | ||
} | ||
} | ||
|
||
//output operations | ||
$mode = $this->_switchmode($outputmode); | ||
|
||
if($mode == 'S') | ||
{ | ||
return $fpdi->Output($outputpath, 'S'); | ||
} | ||
else | ||
{ | ||
if($fpdi->Output($outputpath, $mode) == '') | ||
{ | ||
return true; | ||
} | ||
else | ||
{ | ||
throw new exception("Error outputting PDF to '$outputmode'."); | ||
return false; | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
* FPDI uses single characters for specifying the output location. Change our more descriptive string into proper format. | ||
* @param $mode | ||
* @return Character | ||
*/ | ||
private function _switchmode($mode) | ||
{ | ||
switch(strtolower($mode)) | ||
{ | ||
case 'download': | ||
return 'D'; | ||
break; | ||
case 'browser': | ||
return 'I'; | ||
break; | ||
case 'file': | ||
return 'F'; | ||
break; | ||
case 'string': | ||
return 'S'; | ||
break; | ||
default: | ||
return 'I'; | ||
break; | ||
} | ||
} | ||
|
||
/** | ||
* 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 | ||
*/ | ||
private function _rewritepages($pages) | ||
{ | ||
$pages = str_replace(' ', '', $pages); | ||
$part = explode(',', $pages); | ||
|
||
//parse hyphens | ||
foreach($part as $i) | ||
{ | ||
$ind = explode('-', $i); | ||
|
||
if(count($ind) == 2) | ||
{ | ||
$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; | ||
|
||
//add middle pages | ||
while($x <= $y): $newpages[] = (int) $x; $x++; endwhile; | ||
} | ||
else | ||
{ | ||
$newpages[] = (int) $ind[0]; | ||
} | ||
} | ||
|
||
return $newpages; | ||
} | ||
|
||
} |
Oops, something went wrong.