From 04e1f6d90f28d6acf19d4e0d9a809a99f491d0bb Mon Sep 17 00:00:00 2001 From: m-hume Date: Thu, 28 Jul 2016 16:42:27 +0100 Subject: [PATCH] Allows for adding of PDFs as strings Add a PDF as a raw string (created by TCPDF?) for inclusion in the merge --- PDFMerger.php | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/PDFMerger.php b/PDFMerger.php index 57d69dc..fd1b974 100644 --- a/PDFMerger.php +++ b/PDFMerger.php @@ -25,6 +25,7 @@ class PDFMerger { private $_files; //['form.pdf'] ["1,2,4, 5-19"] private $_fpdi; + private $_temp_filenames = array(); /** * Merge PDFs. @@ -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 @@ -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 @@ -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.