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