wCMF  3.6
 All Classes Namespaces Files Functions Variables Groups Pages
class.PDFTemplate.php
Go to the documentation of this file.
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2014 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of any of the following licenses
7  * at your choice:
8  *
9  * - GNU Lesser General Public License (LGPL)
10  * http://www.gnu.org/licenses/lgpl.html
11  * - Eclipse Public License (EPL)
12  * http://www.eclipse.org/org/documents/epl-v10.php
13  *
14  * See the license.txt file distributed with this work for
15  * additional information.
16  *
17  * $Id: class.PDFTemplate.php 1462 2014-02-04 23:52:27Z iherwig $
18  */
19 require_once(BASE."wcmf/lib/output/pdf/class.PDF.php");
20 require_once(BASE."wcmf/lib/core/class.WCMFException.php");
21 
22 /**
23  * @class PDFTemplate
24  * @ingroup Output
25  * @brief PDFTemplate is used to output pdf files based on a given pdf template.
26  * PDFTemplate uses FPDI/FPDF. PDFPage instances are used to render data onto
27  * the template pages.
28  *
29  * The following example shows the usage:
30  *
31  * @code
32  * // example Controller method to show a pdf download dialog
33  * // Page1 extends PDFPage and defines what is rendered onto the template
34  * function executeKernel()
35  * {
36  * $template = new PDFTemplate(new MyPDF());
37  * // set the template
38  * $template->setTemplate('include/pdf/wcmf.pdf');
39  * // render Page1 on every second template page
40  * $template->setPages(array(new Page1(), null), true);
41  *
42  * // output the final page
43  * $downloadfile = 'wcmf.pdf';
44  * header("Content-disposition: attachment; filename=$downloadfile");
45  * header("Content-Type: application/force-download");
46  * header("Content-Transfer-Encoding: binary");
47  * header("Pragma: no-cache");
48  * header("Expires: 0");
49  * echo $template->output($downloadfile, 'S');
50  * }
51  * @endcode
52  *
53  * @author ingo herwig <ingo@wemove.com>
54  */
56 {
57  var $_pdf = null;
58  var $_tpl = null;
59  var $_pages = array();
60  var $_cycle = false;
61  var $_data = null;
62 
63  /**
64  * Constructor
65  * @param pdf The PDF instance to render onto, defaults to PDF created with default constructor
66  */
67  function PDFTemplate(&$pdf)
68  {
69  if (!isset($pdf) || ((strtolower(get_class($pdf)) != 'pdf' && !is_a($pdf, 'PDF'))))
70  $this->_pdf = new PDF();
71  else
72  $this->_pdf = &$pdf;
73  }
74 
75  /**
76  * Set the template filename
77  * @param filename The name of the file
78  */
79  function setTemplate($filename)
80  {
81  $this->_tpl = $filename;
82  }
83 
84  /**
85  * Set the PDFPage instances used to write the content to the template.
86  * The page instances will be used one after another: The 1 instance writes to the first
87  * template page, the second to the second and so on. Use the cycle parameter to cycle the
88  * instances (e.g. if the same data should be written to every template page, put one
89  * instance into the pages array and set cycle to true)
90  * @param pages An array of PDFPage instances
91  * @param cycle True/False wether to cycle the PDFPage instances or not [default: false]
92  * @param data An optional data object, that will passed to the PDFPage::render method [default: null]
93  */
94  function setPages($pages, $cycle=false, $data=null)
95  {
96  $this->_pages = $pages;
97  $this->_cycle = $cycle;
98  $this->_data = &$data;
99  }
100 
101  /**
102  * Output the pdf. Delegates to FPDF::Output()
103  * @see http://www.fpdf.de/funktionsreferenz/Output/
104  * @param name The name of the pdf file
105  * @param dest The pdf destination ('I': browser inline, 'D': browser download, 'F': filesystem, 'S': string)
106  * @return The document string in case of dest = 'S', nothing else
107  */
108  function output($name='', $dest='')
109  {
110  if ($this->_tpl == null)
111  {
112  WCMFException::throwEx("No PDF template provided. Use PDFTemplate::setTemplate.", __FILE__, __LINE__);
113  return;
114  }
115 
116  $pageIndex = 0;
117  $numPages = $this->_pdf->setSourceFile($this->_tpl);
118  for ($i=1; $i<=$numPages; $i++)
119  {
120  // add each page
121  $tplIndex = $this->_pdf->ImportPage($i);
122  $size = $this->_pdf->getTemplatesize($tplIndex);
123  $this->_pdf->AddPage($size['h'] > $size['w'] ? 'P' : 'L');
124 
125  // render the PDFPage onto the template page
126  if ($pageIndex < sizeof($this->_pages))
127  {
128  $curPage = &$this->_pages[$pageIndex];
129  if (strtolower(get_class($curPage)) == 'pdfpage' || is_a($curPage, 'PDFPage'))
130  {
131  $this->_pdf->startPage();
132  $curPage->render($this->_pdf, $i, $this->_data);
133  $this->_pdf->endPage();
134  }
135  }
136  $pageIndex++;
137 
138  // cycle pages if required
139  if ($this->_cycle && $pageIndex == sizeof($this->_pages))
140  $pageIndex = 0;
141  }
142 
143  // output the pdf
144  return $this->_pdf->Output($name, $dest);
145  }
146 }
147 ?>
setTemplate($filename)
PDF extends FPDF/FPDI.
Definition: class.PDF.php:29
PDFTemplate is used to output pdf files based on a given pdf template. PDFTemplate uses FPDI/FPDF...
throwEx($message, $file='', $line='')
output($name='', $dest='')
setPages($pages, $cycle=false, $data=null)