Ticket #50: pdf.php

File pdf.php, 14.3 kB (added by nagual69, 14 months ago)

Tweaked

Line 
1<?php
2/**
3 * phpaga
4 *
5 * pdf output functionality.
6 *
7 * This file contains the necessary classes and routines to create pdf documents.
8 *
9 * @author Florian Lanthaler <florian@phpaga.net>
10 * @version $Id: pdf.php 861 2007-09-21 14:48:42Z florian $
11 *
12 * Copyright (c) 2002, Florian Lanthaler <florian@phpaga.net>
13 *
14 * All rights reserved.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are
18 * met:
19 *
20 *    * Redistributions of source code must retain the above copyright
21 *      notice, this list of conditions and the following disclaimer.
22 *
23 *    * Redistributions in binary form must reproduce the above copyright
24 *      notice, this list of conditions and the following disclaimer in
25 *      the documentation and/or other materials provided with the
26 *      distribution.
27 *
28 *    * Neither the name of Florian Lanthaler nor the names of his
29 *      contributors may be used to endorse or promote products derived
30 *      from this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
33 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
34 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
35 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
36 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
37 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
38 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
39 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 */
44
45define("PDFCLASS_FONTDIR", PDFCLASS_DIR."fonts/");
46include_once(PDFCLASS_DIR."class.ezpdf.php");
47
48if (strtolower(PHPAGA_PDF_DOCFORMAT) == 'letter')
49{
50    /* letter */
51    define("PHPAGA_PDF_DOCWIDTH",      612);
52    define("PHPAGA_PDF_DOCHEIGHT",     792);
53}
54else
55{
56    /* a4 */
57    define("PHPAGA_PDF_DOCWIDTH",   595);
58    define("PHPAGA_PDF_DOCHEIGHT"842);
59}
60
61define("PHPAGA_PDF_FONT_SIZE_BILL"PHPAGA_PDF_FONT_SIZE_NORMAL);
62define("PHPAGA_PDF_BILL_NEXTLINE",   PHPAGA_PDF_FONT_SIZE_BILL * 1.5);
63define("PHPAGA_PDF_HEADER_NEXTLINE", PHPAGA_PDF_FONT_SIZE_SMALL * 1.5);
64
65define("PHPAGA_PDF_FONT_SIZE_QUOTATION"PHPAGA_PDF_FONT_SIZE_NORMAL);
66define("PHPAGA_PDF_QUOTATION_NEXTLINE",   PHPAGA_PDF_FONT_SIZE_BILL * 1.5);
67
68define("PHPAGA_PDF_OPLINE_HEIGHT", PHPAGA_PDF_FONT_SIZE_SMALL * 1.25);
69define("PHPAGA_PDF_OPLINE_HEIGHT_NEXTOP", PHPAGA_PDF_FONT_SIZE_SMALL * 1.5);
70
71define("PHPAGA_PDF_BORDER_RIGHT", PHPAGA_PDF_BORDER_LEFT);
72define("PHPAGA_PDF_START_RIGHT", PHPAGA_PDF_DOCWIDTH - PHPAGA_PDF_BORDER_RIGHT - 1);
73
74define("PHPAGA_PDF_BORDER_BOTTOM", PHPAGA_PDF_BORDER_TOP);
75
76define("PHPAGA_PDF_SEPARATOR_TOP_HEIGHT", PHPAGA_PDF_DOCHEIGHT - 175);
77define("PHPAGA_PDF_SEPARATOR_BOTTOM_HEIGHT", PHPAGA_PDF_BORDER_BOTTOM);
78
79define("PHPAGA_PDF_POS_Y_HEADER_EMAIL",
80       PHPAGA_PDF_SEPARATOR_TOP_HEIGHT
81       + PHPAGA_PDF_FONT_SIZE_NORMAL * 2.5);
82
83define("PHPAGA_PDF_POS_Y_HEADER_NAME",
84       PHPAGA_PDF_POS_Y_HEADER_EMAIL
85       + PHPAGA_PDF_FONT_SIZE_HEADER * 2);
86
87define("PHPAGA_PDF_POS_Y_LOGO",
88       PHPAGA_PDF_DOCHEIGHT
89       - PHPAGA_PDF_BORDER_TOP);
90
91define("PHPAGA_PDF_LOGO_MAXWIDTH", PHPAGA_PDF_DOCWIDTH * 0.25);
92define("PHPAGA_PDF_POS_Y_RECIPIENT", PHPAGA_PDF_DOCHEIGHT - 225);
93define("PHPAGA_PDF_POS_X_RECIPIENT", PHPAGA_PDF_DOCWIDTH - 320);
94
95define("PHPAGA_PDF_POS_Y_HEADER_RLINE5",
96       PHPAGA_PDF_SEPARATOR_TOP_HEIGHT
97       + PHPAGA_PDF_FONT_SIZE_NORMAL * 3);
98
99define("PHPAGA_PDF_POS_Y_HEADER_RLINE4",
100       PHPAGA_PDF_POS_Y_HEADER_RLINE5
101       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);
102
103define("PHPAGA_PDF_POS_Y_HEADER_RLINE3",
104       PHPAGA_PDF_POS_Y_HEADER_RLINE4
105       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);
106
107define("PHPAGA_PDF_POS_Y_HEADER_RLINE2",
108       PHPAGA_PDF_POS_Y_HEADER_RLINE3
109       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);
110
111define("PHPAGA_PDF_POS_Y_HEADER_RLINE1",
112       PHPAGA_PDF_POS_Y_HEADER_RLINE2
113       + PHPAGA_PDF_FONT_SIZE_SMALL + 3);
114
115define("PHPAGA_PDF_POS_Y_FOOTER",
116       PHPAGA_PDF_SEPARATOR_BOTTOM_HEIGHT
117       - PHPAGA_PDF_FONT_SIZE_SMALL - 1);
118
119
120/**
121 * This class allows the creation and management of pdf-documents.
122 *
123 * @author Florian Lanthaler <florian@phpaga.net>
124 * @since phpaga 0.1
125 */
126
127class phpaga_pdf_document
128{
129    var $handle;
130    var $creator;
131    var $author;
132    var $title;
133    var $subject;
134    var $filename;
135    var $tpl_letterhead;
136    var $tpl_oplist;
137
138    /**
139     * Constructor
140     *
141     * Creates a new pdf document and opens the corresponding file.
142     *
143     * @param string   $filename   Name of the pdf document
144     *
145     * @return void
146     *
147     * @author Florian Lanthaler <florian@phpaga.net>
148     * @since phpaga 0.1
149     */
150
151    function phpaga_pdf_document($filename = null)
152    {
153        if (!isset($filename) || !strlen($filename))
154            $this->filename = '';
155        else
156            $this->filename = $filename;
157
158        $this->handle =& new Cezpdf(PHPAGA_PDF_DOCFORMAT);
159        $this->handle->selectFont(PDFCLASS_FONTDIR."Helvetica.afm");
160
161    }
162
163    /**
164     * Sets the file name.
165     *
166     * @param string   $filename  File name
167     *
168     * @return void
169     *
170     * @author Florian Lanthaler <florian@phpaga.net>
171     * @since phpaga 0.3
172     */
173
174    function set_filename($filename)
175    {
176        if (strlen(trim($filename)))
177            $this->filename = $filename;
178    }
179
180    /**
181     * Sets the document information.
182     *
183     * @param string   $title     document title
184     * @param string   $subject   subject
185     * @param string   $creator   creator
186     * @param string   $author    author
187     *
188     * @return void
189     *
190     * @author Florian Lanthaler <florian@phpaga.net>
191     * @since phpaga 0.1
192     */
193
194    function set_info($title = " ",
195                      $subject = " ",
196                      $creator = " ",
197                      $author = " ")
198    {
199        $this->handle->addInfo("Title", $title);
200        $this->handle->addInfo("Subject", $subject);
201        $this->handle->addInfo("Creator", $creator);
202        $this->handle->addInfo("Author", $author);
203    }
204
205    /**
206     * Begin a new page
207     *
208     * @return void
209     *
210     * @author Florian Lanthaler <florian@phpaga.net>
211     * @since phpaga 0.1
212     */
213
214    function next_page()
215    {
216        $this->handle->ezNewPage();
217    }
218
219    /**
220     * Add some basic page layout with the company information.
221     *
222     * @param  string $logo_filename   File name of the logo
223     * @return void
224     *
225     * @author Florian Lanthaler <florian@phpaga.net>
226     * @since phpaga 0.1
227     */
228
229    function add_letterhead($logo_filename)
230    {
231        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/letterhead.tpl.php");
232        $this->handle->execTemplate($tid, array("logo_filename" => $logo_filename));
233    }
234
235    /**
236     * Spit the pdf document out.
237     *
238     * @return void
239     *
240     * @author Florian Lanthaler <florian@phpaga.net>
241     * @since phpaga 0.1
242     */
243    function spitout()
244    {
245        $this->handle->ezStream(array("Content-Disposition" =>
246                                      $this->filename));
247    }
248
249    /**
250     * Add recipient information to the page.
251     *
252     * @param array   $cpnInfo_recipient array containing information
253     *                                   about the recipient
254     *
255     * @return void
256     *
257     * @author Florian Lanthaler <florian@phpaga.net>
258     * @since phpaga 0.1
259     */
260
261    function add_recipient($cpnInfo_recipient)
262    {
263        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/recipient.tpl.php");
264        $this->handle->execTemplate($tid, array("recipient" => $cpnInfo_recipient));
265    }
266
267    /**
268     * Add invoice information to the page.
269     *
270     * @param array   $billInfo  array containing invoice information
271     *
272     * @return void
273     *
274     * @author Florian Lanthaler <florian@phpaga.net>
275     * @since phpaga 0.1
276     */
277
278    function add_invoice($billInfo, $options = null)
279    {
280
281        if (!isset($billInfo) || !is_array($billInfo))
282            return false;
283
284        if ($options == null)
285            $options = array("recipient" => null,
286                             "letterhead" => false);
287
288        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/invoice.tpl.php");
289        $this->handle->execTemplate($tid, array("type" => PHPAGA_TYPE_INVOICE,
290                                                "info" => $billInfo,
291                                                "recipient" => $options["recipient"],
292                                                "letterhead" => $options["letterhead"],
293                                                "logo_filename" => PHPAGA_LETTERHEAD_LOGO_FILE));
294    }
295
296    /**
297     * Add quotation information to the page.
298     *
299     * @param array   $quotInfo           array containing quotation information
300     * @param array   $cpnInfo_recipient  array containing information about who receives
301     *                                          the quotation
302     *
303     * @return void
304     *
305     * @author Florian Lanthaler <florian@phpaga.net>
306     * @since phpaga 0.1
307     */
308
309    function add_quotation($quotInfo, $options = null)
310    {
311
312        if (!isset($quotInfo) || !is_array($quotInfo))
313            return false;
314
315         if ($options == null)
316             $options = array("recipient" => null,
317                             "letterhead" => false);
318
319        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/invoice.tpl.php");
320        $this->handle->execTemplate($tid, array("type" => PHPAGA_TYPE_QUOTATION,
321                                                "info" => $quotInfo,
322                                                "recipient" => $options["recipient"],
323                                                "letterhead" => $options["letterhead"],
324                                                "logo_filename" => PHPAGA_LETTERHEAD_LOGO_FILE));
325}
326
327    /**
328     * Add task list to the page.
329     *
330     * @param array $rows           array containing operations
331     * @param array $search_params  search parameters to be displayed on
332     *                              the document (optional)
333     * @param array $show_fields    fields to be shown in the list
334     *
335     * @return void
336     *
337     * @author Florian Lanthaler <florian@phpaga.net>
338     * @since phpaga 0.1
339     */
340
341    function add_oplist($rows, $search_params = "", $show_fields = "", $title = null, $insert_page = true)
342    {
343
344        if ($title == null)
345           $title = _("Tasks");
346
347        if (!is_array($show_fields))
348            $show_fields = array("date" => _("Date"),
349                                 "duration" => _("Duration"),
350                                 "project" => _("Project"),
351                                 "person" => _("Person"),
352                                 "task" => _("Task"),
353                                 "deadline" => _("Deadline"));
354
355        $h_date = "";
356        $show_operations = array();
357
358        /* cycle through the operations and pick the fields we want
359         * to print
360         */
361
362        foreach ($rows as $op)
363        {
364
365            $h_date = $op["op_startdate"]." ".$op["op_starthours"]." - ";
366
367            if ($op["op_startdate"] != $op["op_enddate"])
368                $h_date .= "\n".$op["op_enddate"]." ";
369
370            $h_date .= $op["op_endhours"];
371
372            $show_operations[] = array("date" => $h_date,
373                                       "duration" => $op["op_duration_hrs"],
374                                       "project" => $op["prj_title"],
375                                       "person" => $op["op_person"],
376                                       "task" => $op["op_desc"],
377                                       "status" => $op["prjstat_title"],
378                                       "deadline" => $op["prj_deadline"]
379                );
380        }
381
382        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/operations.tpl.php");
383        $this->handle->execTemplate($tid, array("show_operations" => $show_operations,
384                                                "show_fields" => $show_fields,
385                                                "search_params" => $search_params,
386                                                "title" => $title,
387                                                "insert_page" => $insert_page));
388    }
389
390    /**
391     * Add project list to the page.
392     *
393     * @param array $rows           array containing operations
394     * @param array $search_params  search parameters to be displayed on
395     *                              the document (optional)
396     * @param array $show_fields    fields to be shown in the list
397     *
398     * @return void
399     *
400     * @author Mark Parssey <markpa@users.sourceforge.net>
401     * @since phpaga 0.3
402     */
403
404    function add_prjlist($rows, $search_params = "", $show_fields = "")
405    {
406
407        if (!is_array($show_fields))
408            $show_fields = array("date" => _("Date"),
409                                 "title" => _("Project"),
410                                 "company" => _("Company"),
411                                 "desc" => _("Description"),
412                                 "status" => _("Status"),
413                                 "deadline" => _("Deadline"));
414
415        $h_date = "";
416        $show_projects = array();
417
418        /* cycle through the projests and pick the fields we want
419         * to print
420         */
421
422        foreach ($rows as $prj)
423        {
424
425            $h_date = $prj["prj_startdate"];
426
427            $show_projects[] = array("date" => $h_date,
428                                     "title" => $prj["prj_title"],
429                                     "company" => $prj["cpn_name"],
430                                     "desc" => $prj["prj_desc"],
431                                     "status" => $prj["prjstat_title"],
432                                     "deadline" => $prj["prj_deadline"]
433                );
434        }
435
436        $tid = $this->handle->loadTemplate(PHPAGA_BASEPATH."pdftemplates/projects.tpl.php");
437        $this->handle->execTemplate($tid, array("show_projects" => $show_projects,
438                                                "show_fields" => $show_fields,
439                                                "search_params" => $search_params));
440    }
441
442}
443
444?>