From d017724ab57f563d118eb0d2c88cb5202a867bcc Mon Sep 17 00:00:00 2001 From: meritel Date: Mon, 4 Mar 2019 11:03:12 +0100 Subject: [PATCH 1/2] improving performance when __destruct Because Html2Pdf loop through html elements, and creates a lot of sub elements, each time a sub element is cleared, a complete scan of /tmp/ dir was made to delete temp files. These modifs allow to make only ONE scan in /tmp/ dir, at the end of generation, to delete temp files. --- src/Html2Pdf.php | 20 ++++++++++++++++++-- src/MyPdf.php | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/Html2Pdf.php b/src/Html2Pdf.php index 65e04031..38fd544a 100755 --- a/src/Html2Pdf.php +++ b/src/Html2Pdf.php @@ -137,8 +137,9 @@ class Html2Pdf /** * @var Html2Pdf */ - static protected $_subobj = null; // object html2pdf prepared in order to accelerate the creation of sub html2pdf - static protected $_tables = array(); // static table to prepare the nested html tables + static protected $_subobj = null; // object html2pdf prepared in order to accelerate the creation of sub html2pdf + static protected $_tables = array(); // static table to prepare the nested html tables + static protected $_tmpFilesAreCleaned = false; // flag : file cleaning is done /** * list of tag definitions @@ -231,6 +232,21 @@ public function __construct( return $this; } + + /** + * Default destructor. + * Clean cache files once, at the end of the pdf generation + * @public + */ + public function __destruct() { + if($this->_isSubPart || self::$_tmpFilesAreCleaned) return; + // remove all temporary files + $tmpfiles = glob(K_PATH_CACHE.'__tcpdf_*'); + if (!empty($tmpfiles)) { + self::$_tmpFilesAreCleaned = true; + array_map('unlink', $tmpfiles); + } + } /** * Gets the detailed version as array diff --git a/src/MyPdf.php b/src/MyPdf.php index 8411fba2..451f3ec2 100644 --- a/src/MyPdf.php +++ b/src/MyPdf.php @@ -64,6 +64,49 @@ public function __construct( $this->setCellPaddings(0, 0, 0, 0); $this->setCellMargins(0, 0, 0, 0); } + + /** + * Default destructor. + * OVERWRITE TCPDF __destruct + * @public + * @since 1.53.0.TC016 + */ + public function __destruct() { + // cleanup + $this->_destroy(true); + } + + /** + * OVERWRITE TCPDF _destroy + * tcpdf + */ + public function _destroy($destroyall=false, $preserve_objcopy=false) { + // restore internal encoding + if (isset($this->internal_encoding) AND !empty($this->internal_encoding)) { + mb_internal_encoding($this->internal_encoding); + } + $preserve = array( + 'file_id', + 'internal_encoding', + 'state', + 'bufferlen', + 'buffer', + 'cached_files', + 'sign', + 'signature_data', + 'signature_max_length', + 'byterange_string', + 'tsa_timestamp', + 'tsa_data' + ); + foreach (array_keys(get_object_vars($this)) as $val) { + if ($destroyall OR !in_array($val, $preserve)) { + if ((!$preserve_objcopy OR ($val != 'objcopy')) AND ($val != 'file_id') AND isset($this->$val)) { + unset($this->$val); + } + } + } + } /** * Set the parameters for the automatic footer From 38ec9359e8227930ae15342de6ea223746d61814 Mon Sep 17 00:00:00 2001 From: meritel Date: Tue, 5 Mar 2019 17:17:24 +0100 Subject: [PATCH 2/2] delete only instance's tmp files @MyPdf => create a getFileId function to access to the private $tcpdf->file_id @Html2Pdf => use the getFileId() to delete tmp files of this instance of Html2Pdf --- src/Html2Pdf.php | 2 +- src/MyPdf.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/Html2Pdf.php b/src/Html2Pdf.php index 38fd544a..5cd9b097 100755 --- a/src/Html2Pdf.php +++ b/src/Html2Pdf.php @@ -241,7 +241,7 @@ public function __construct( public function __destruct() { if($this->_isSubPart || self::$_tmpFilesAreCleaned) return; // remove all temporary files - $tmpfiles = glob(K_PATH_CACHE.'__tcpdf_*'); + $tmpfiles = glob(K_PATH_CACHE.'__tcpdf_'.$this->pdf->getFileId().'_*'); if (!empty($tmpfiles)) { self::$_tmpFilesAreCleaned = true; array_map('unlink', $tmpfiles); diff --git a/src/MyPdf.php b/src/MyPdf.php index 451f3ec2..50fb2044 100644 --- a/src/MyPdf.php +++ b/src/MyPdf.php @@ -107,6 +107,15 @@ public function _destroy($destroyall=false, $preserve_objcopy=false) { } } } + + /** + * Get the private variable file_id, for cleaning tmp files + * @access public + */ + public function getFileId() + { + return $this->file_id; + } /** * Set the parameters for the automatic footer