11<?php
22
33/**
4- * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2018
4+ * @copyright Copyright © Kartik Visweswaran, Krajee.com, 2014 - 2020
55 * @package yii2-mpdf
66 * @version 1.0.6
77 */
@@ -198,32 +198,6 @@ class Pdf extends Component
198198 */
199199 protected $ _pdfAttachments ;
200200
201- /**
202- * Defines a Mpdf temporary path if not set.
203- *
204- * @param string $prop the Mpdf constant to define
205- * @param string $dir the directory to create
206- *
207- * @throws InvalidConfigException
208- */
209- protected static function definePath ($ prop , $ dir )
210- {
211- if (defined ($ prop )) {
212- $ propDir = constant ($ prop );
213- if (is_writable ($ propDir )) {
214- return ;
215- }
216- }
217- $ status = true ;
218- if (!is_dir ($ dir )) {
219- $ status = mkdir ($ dir , 0777 , true );
220- }
221- if (!$ status ) {
222- throw new InvalidConfigException ("Could not create the folder ' {$ dir }' in ' \$tempPath' set. " );
223- }
224- define ($ prop , $ dir );
225- }
226-
227201 /**
228202 * @inheritdoc
229203 */
@@ -236,25 +210,26 @@ public function init()
236210
237211 /**
238212 * Initialize folder paths to allow [[Mpdf]] to write temporary data.
239- *
240- * @throws InvalidConfigException
241213 */
242214 public function initTempPaths ()
243215 {
244216 if (empty ($ this ->tempPath )) {
245217 $ this ->tempPath = Yii::getAlias ('@runtime/mpdf ' );
246218 }
247- $ s = DIRECTORY_SEPARATOR ;
248- $ prefix = $ this ->tempPath . $ s ;
249- static ::definePath ('_MPDF_TEMP_PATH ' , "{$ prefix }tmp {$ s }" );
250- static ::definePath ('_MPDF_TTFONTDATAPATH ' , "{$ prefix }ttfontdata {$ s }" );
219+ if (!file_exists ($ this ->tempPath )) {
220+ mkdir ($ this ->tempPath );
221+ }
251222 }
252223
253224 /**
254225 * Renders and returns the PDF output. Uses the class level property settings.
255226 *
256227 * @return mixed
257228 * @throws InvalidConfigException
229+ * @throws \Mpdf\MpdfException
230+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
231+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
232+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
258233 */
259234 public function render ()
260235 {
@@ -300,26 +275,26 @@ public function setApi()
300275 * Fetches the content of the CSS file if supplied
301276 *
302277 * @return string
278+ * @throws InvalidConfigException
303279 */
304280 public function getCss ()
305281 {
306282 if (!empty ($ this ->_css )) {
307283 return $ this ->_css ;
308284 }
309- $ this ->_css = '' ;
285+ $ this ->_css = '' ;
310286 if (!empty ($ this ->cssFile )) {
311287 $ cssFiles = is_array ($ this ->cssFile ) ? $ this ->cssFile : [$ this ->cssFile ];
312288 foreach ($ cssFiles as $ cssFile ) {
313289 $ cssFile = Yii::getAlias ($ cssFile );
314290 if (!empty ($ cssFile ) && file_exists ($ cssFile )) {
315291 $ this ->_css .= file_get_contents ($ cssFile );
316- }
317- else {
292+ } else {
318293 throw new InvalidConfigException ("CSS File not found: ' {$ cssFile }'. " );
319294 }
320295 }
321296 }
322- $ this ->_css .= $ this ->cssInline ;
297+ $ this ->_css .= $ this ->cssInline ;
323298 return $ this ->_css ;
324299 }
325300
@@ -374,6 +349,10 @@ public function execute($method, $params = [])
374349 *
375350 * @return mixed
376351 * @throws InvalidConfigException
352+ * @throws \Mpdf\MpdfException
353+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
354+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
355+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
377356 */
378357 public function output ($ content = '' , $ file = '' , $ dest = self ::DEST_BROWSER )
379358 {
@@ -392,7 +371,6 @@ public function output($content = '', $file = '', $dest = self::DEST_BROWSER)
392371 $ api ->WriteHTML ($ content );
393372 }
394373 if ($ pdfAttachments ) {
395- $ api ->SetImportUse ();
396374 $ api ->SetHeader (null );
397375 $ api ->SetFooter (null );
398376 foreach ($ pdfAttachments as $ attachment ) {
@@ -430,7 +408,7 @@ public function output($content = '', $file = '', $dest = self::DEST_BROWSER)
430408 /**
431409 * Parse the format automatically based on the orientation
432410 */
433- protected function parseFormat ()
411+ public function parseFormat ()
434412 {
435413 $ landscape = self ::ORIENT_LANDSCAPE ;
436414 $ tag = '- ' . $ landscape ;
@@ -444,9 +422,18 @@ protected function parseFormat()
444422 *
445423 * @param Mpdf $api the Mpdf API instance
446424 * @param string $attachment the attachment name
425+ * @throws \setasign\Fpdi\PdfParser\CrossReference\CrossReferenceException
426+ * @throws \setasign\Fpdi\PdfParser\PdfParserException
427+ * @throws \setasign\Fpdi\PdfParser\Type\PdfTypeException
447428 */
448- private function writePdfAttachment ($ api , $ attachment )
429+ public function writePdfAttachment ($ api = null , $ attachment = null )
449430 {
431+ if ($ attachment === null ) {
432+ return ;
433+ }
434+ if ($ api === null ) {
435+ $ api = $ this ->getApi ();
436+ }
450437 $ pageCount = $ api ->SetSourceFile ($ attachment );
451438 for ($ i = 1 ; $ i <= $ pageCount ; $ i ++) {
452439 $ api ->AddPage ();
0 commit comments