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

Allows for adding of PDFs as strings #21

Open
wants to merge 1 commit 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
38 changes: 38 additions & 0 deletions PDFMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class PDFMerger
{
private $_files; //['form.pdf'] ["1,2,4, 5-19"]
private $_fpdi;
private $_temp_filenames = array();

/**
* Merge PDFs.
Expand All @@ -36,6 +37,15 @@ public function __construct()
require_once('fpdi/fpdi.php');
}

/**
* Merge PDFs.
* @return void
*/
public function __destruct()
{
$this->cleanTempFiles();
}

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

/**
* Add a PDF as a raw string (created by TCPDF?) for inclusion in the merge. Pages should be formatted: 1,3,6, 12-16.
* @param $raw_pdf_string
* @param $pages
* @return void
*/
public function addRawPDF($raw_pdf_string, $pages = 'all')
{
$temp_name = tempnam(sys_get_temp_dir(), 'PDFMerger');
if (@file_put_contents($temp_name, $raw_pdf_string) === false) {
throw new Exception("Unable to create temporary file");
}
$this->_temp_filenames[] = $temp_name;
return $this->addPDF($temp_name, $pages);
}

/**
* Merges your provided PDFs and outputs to specified location.
* @param $outputmode
Expand Down Expand Up @@ -130,6 +156,18 @@ public function merge($outputmode = 'browser', $outputpath = 'newfile.pdf')


}

/**
* Delete the temporary files created for the merge
* @return void
*/
public function cleanTempFiles()
{
foreach($this->_temp_filenames as $temp_name){
@unlink($temp_name);
}
$this->_temp_filenames = array();
}

/**
* FPDI uses single characters for specifying the output location. Change our more descriptive string into proper format.
Expand Down