Skip to content

Commit c9a888f

Browse files
authored
Merge pull request #38 from niklaswolf/master
Add possibility to append pdf attachements
2 parents d2513eb + 3450d52 commit c9a888f

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

Pdf.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ class Pdf extends Component
170170
*/
171171
protected $_css;
172172

173+
/**
174+
*
175+
* @var array Array of file-pathes that should be attached to the generated PDF
176+
*/
177+
protected $_pdfAttachements;
178+
173179
/**
174180
* @inherit doc
175181
*/
@@ -280,7 +286,21 @@ public function getCss()
280286
$css .= $this->cssInline;
281287
return $css;
282288
}
283-
289+
290+
/**
291+
* @return array Array of attachements
292+
*/
293+
public function getPdfAttachements (){
294+
return $this->_pdfAttachements;
295+
}
296+
297+
/**
298+
* add an PDF to attach to the generated PDF
299+
* @param string $filePath
300+
*/
301+
public function addPdfAttachement ($filePath){
302+
$this->_pdfAttachements[] = $filePath;
303+
}
284304
/**
285305
* Configures mPDF options
286306
* @param array the mPDF configuration options entered as a `$key => value`
@@ -331,15 +351,40 @@ public function output($content = '', $file = '', $dest = self::DEST_BROWSER)
331351
{
332352
$api = $this->api;
333353
$css = $this->css;
354+
$pdfAttachements = $this->getPdfAttachements();
334355
if (!empty($css)) {
335356
$api->WriteHTML($css, 1);
336357
$api->WriteHTML($content, 2);
337358
} else {
338359
$api->WriteHTML($content);
360+
}
361+
362+
if($pdfAttachements){
363+
$api->SetImportUse();
364+
$api->SetHeader(null);
365+
$api->SetFooter(null);
366+
foreach ($pdfAttachements as $attachement){
367+
$this->writePdfAttachement($api, $attachement);
368+
}
339369
}
370+
340371
return $api->Output($file, $dest);
341372
}
342-
373+
374+
/**
375+
* appends the given attachement to the generated PDF
376+
* @param mPDF $api
377+
* @param String $attachement
378+
*/
379+
private function writePdfAttachement ($api, $attachement){
380+
$pageCount = $api->SetSourceFile($attachement);
381+
for($i=1; $i<=$pageCount; $i++){
382+
$api->AddPage();
383+
$templateId = $api->ImportPage($i);
384+
$api->UseTemplate($templateId);
385+
}
386+
}
387+
343388
/**
344389
* Parse the format automatically based on the orientation
345390
*/

0 commit comments

Comments
 (0)