Skip to content

Commit

Permalink
fork lynx39/lara-pdf-merger
Browse files Browse the repository at this point in the history
  • Loading branch information
adriallongarriu committed Dec 9, 2020
0 parents commit d17976d
Show file tree
Hide file tree
Showing 208 changed files with 89,913 additions and 0 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# lara-pdf-merger

## Update

Since https://github.com/deltaaskii/lara-pdf-merger is no more available, fork the repo to maintain projects using it.

See https://packagist.org/packages/lynx39/lara-pdf-merger for original package.

---

Original written by http://pdfmerger.codeplex.com/team/view<br/>

### Update

Change parsers and use tcpdf, tcpdi and tcpdi_parser

## Installation

### Laravel 5.x:

Require this package in your composer.json and update composer.

"adriallongarriu/lara-pdf-merger": "dev-master",

After updating composer, add the ServiceProvider to the providers array in config/app.php

LynX39\LaraPdfMerger\PdfMergerServiceProvider::class,

You can optionally use the facade for shorter code. Add this to your facades:

'PdfMerger' => LynX39\LaraPdfMerger\Facades\PdfMerger::class,

## Using

```php

$pdf = new LynX39\LaraPdfMerger\PdfManage;

$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4');
$pdf->addPDF('samplepdfs/two.pdf', '1-2');
$pdf->addPDF('samplepdfs/three.pdf', 'all');

//You can optionally specify a different orientation for each PDF
$pdf->addPDF('samplepdfs/one.pdf', '1, 3, 4', 'L');
$pdf->addPDF('samplepdfs/two.pdf', '1-2', 'P);

$pdf->merge('file', 'samplepdfs/TEST2.pdf', 'P');

// REPLACE 'file' WITH 'browser', 'download', 'string', or 'file' for output options
// Last parameter is for orientation (P for protrait, L for Landscape).
// This will be used for every PDF that doesn't have an orientation specified
```
27 changes: 27 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "adriallongarriu/lara-pdf-merger",
"description": "",
"keywords": ["merger","merge","pdf","manage","paginate", "laravel"],
"license" : [
"MIT"
],
"authors": [
{
"name": "Adrià Llongarriu"
},
{
"name": "Michael Musso",
"email": "[email protected]",
"homepage": "https://github.com/LynX39"
}
],
"require": {
"php" : ">=5.4"
},
"homepage" : "https://github.com/adriallongarriu/lara-pdf-merger",
"autoload": {
"psr-0": {
"LynX39\\LaraPdfMerger": "src/"
}
}
}
11 changes: 11 additions & 0 deletions src/LynX39/LaraPdfMerger/Facades/PdfMerger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php namespace LynX39\LaraPdfMerger\Facades;

use Illuminate\Support\Facades\Facade;

class PdfMerger extends Facade {

protected static function getFacadeAccessor()
{
return 'pdf-merger';
}
}
166 changes: 166 additions & 0 deletions src/LynX39/LaraPdfMerger/PdfManage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php

namespace LynX39\LaraPdfMerger;

use Exception;
use TCPDI;

require_once('tcpdf/tcpdf.php');
require_once('tcpdf/tcpdi.php');

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

/**
* 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', $orientation = null)
{
if (file_exists($filepath)) {
if (strtolower($pages) != 'all') {
$pages = $this->_rewritepages($pages);
}

$this->_files[] = array($filepath, $pages, $orientation);
} 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
* @param $orientation
* @return PDF
*/
public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf', $orientation = 'P')
{
if (!isset($this->_files) || !is_array($this->_files)) {
throw new Exception("No PDFs to merge.");
}

$fpdi = new TCPDI;
$fpdi->setPrintHeader(false);
$fpdi->setPrintFooter(false);

// 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);

$fpdi->AddPage($fileorientation, 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.");
}
$size = $fpdi->getTemplateSize($template);

$fpdi->AddPage($fileorientation, 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;
}

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

return $newpages;
}



}
19 changes: 19 additions & 0 deletions src/LynX39/LaraPdfMerger/PdfMergerServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php namespace LynX39\LaraPdfMerger;

use Illuminate\Support\ServiceProvider;

class PdfMergerServiceProvider extends ServiceProvider {

/**
* Register the application services.
*
* @return void
*/
public function register()
{
$this->app->singleton('pdf-merger', function($app)
{
return new PdfManage();
});
}
}
Loading

0 comments on commit d17976d

Please sign in to comment.