PDFTemplate.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\pdf;
12 
15 
16 /**
17  * PDFTemplate is used to output pdf files based on a given pdf template.
18  * PDFTemplate uses FPDI/FPDF. PDFPage instances are used to render data onto
19  * the template pages.
20  *
21  * The following example shows the usage:
22  *
23  * @code
24  * // example Controller method to show a pdf download dialog
25  * // Page1 extends PDFPage and defines what is rendered onto the template
26  * function doExecute()
27  * {
28  * $template = new PDFTemplate(new MyPDF());
29  * // set the template
30  * $template->setTemplate('include/pdf/wcmf.pdf');
31  * // render Page1 on every second template page
32  * $template->setPages([new Page1(), null], true);
33  *
34  * // output the final page
35  * $downloadfile = 'wcmf.pdf';
36  * $response->setFile(new MemoryDocument($template->output($downloadfile, 'S'), 'application/pdf', true, $downloadfile));
37  * }
38  * @endcode
39  *
40  * @author ingo herwig <ingo@wemove.com>
41  */
42 class PDFTemplate {
43 
44  private $pdf = null;
45  private $tpl = null;
46  private $pages = [];
47  private $cycle = false;
48  private $data = null;
49 
50  /**
51  * Constructor
52  * @param $pdf The PDF instance to render onto, defaults to PDF created with default constructor
53  */
54  public function __construct($pdf) {
55  if (!isset($pdf) || (!($pdf instanceof PDF))) {
56  $this->pdf = new PDF();
57  }
58  else {
59  $this->pdf = $pdf;
60  }
61  }
62 
63  /**
64  * Set the template filename
65  * @param $filename The name of the file
66  */
67  public function setTemplate($filename) {
68  $this->tpl = $filename;
69  }
70 
71  /**
72  * Set the PDFPage instances used to write the content to the template.
73  * The page instances will be used one after another: The 1 instance writes to the first
74  * template page, the second to the second and so on. Use the cycle parameter to cycle the
75  * instances (e.g. if the same data should be written to every template page, put one
76  * instance into the pages array and set cycle to true)
77  * @param $pages An array of PDFPage instances
78  * @param $cycle Boolean whether to cycle the PDFPage instances or not (default: _false_)
79  * @param $data An optional data object, that will passed to the PDFPage::render method (default: _null_)
80  */
81  public function setPages($pages, $cycle=false, $data=null) {
82  $this->pages = $pages;
83  $this->cycle = $cycle;
84  $this->data = &$data;
85  }
86 
87  /**
88  * Output the pdf. Delegates to FPDF::Output()
89  * @see http://www.fpdf.de/funktionsreferenz/Output/
90  * @param $name The name of the pdf file
91  * @param $dest The pdf destination ('I': browser inline, 'D': browser download, 'F': filesystem, 'S': string)
92  * @return The document string in case of dest = 'S', nothing else
93  */
94  public function output($name='', $dest='') {
95 
96  if ($this->tpl == null) {
97  throw new \RuntimeException("No PDF template provided. Use PDFTemplate::setTemplate.");
98  }
99 
100  $pageIndex = 0;
101  $numPages = $this->pdf->setSourceFile($this->tpl);
102  for ($i=1; $i<=$numPages; $i++) {
103  // add each page
104  $tplIndex = $this->pdf->ImportPage($i);
105  $size = $this->pdf->getTemplatesize($tplIndex);
106  $this->pdf->AddPage($size['h'] > $size['w'] ? 'P' : 'L');
107 
108  // render the PDFPage onto the template page
109  if ($pageIndex < sizeof($this->pages)) {
110  $curPage = $this->pages[$pageIndex];
111  if ($curPage instanceof PDFPage) {
112  $this->pdf->startPage();
113  $curPage->render($this->pdf, $i, $this->data);
114  $this->pdf->endPage();
115  }
116  }
117  $pageIndex++;
118 
119  // cycle pages if required
120  if ($this->cycle && $pageIndex == sizeof($this->pages)) {
121  $pageIndex = 0;
122  }
123  }
124 
125  // output the pdf
126  return $this->pdf->Output($name, $dest);
127  }
128 }
129 ?>
PDFTemplate is used to output pdf files based on a given pdf template.
Definition: PDFTemplate.php:42
PDF extends FPDF/FPDI.
Definition: PDF.php:29
setTemplate($filename)
Set the template filename.
Definition: PDFTemplate.php:67
output($name='', $dest='')
Output the pdf.
Definition: PDFTemplate.php:94
PDF related interfaces and classes.
Definition: namespaces.php:37
PDFPage instances define the content of a pdf page by using a set of FPDF/FPDI commands inside the PD...
Definition: PDFPage.php:19
__construct($pdf)
Constructor.
Definition: PDFTemplate.php:54
setPages($pages, $cycle=false, $data=null)
Set the PDFPage instances used to write the content to the template.
Definition: PDFTemplate.php:81