This repository was archived by the owner on Jul 10, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpdf.class
More file actions
166 lines (150 loc) · 6.07 KB
/
pdf.class
File metadata and controls
166 lines (150 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
namespace CG;
use CG as r;
use CG\Util as u;
/**
* @file
* Common Good PDF class
* Extends the TCPDF class -- for printing checks, statements, deposit details, etc.
*/
require_once DRUPAL_ROOT . '/vendor/tcpdf/config/tcpdf_config.php'; // lang/eng.php';
require_once DRUPAL_ROOT . '/vendor/tcpdf/tcpdf.php';
//require_once(__DIR__ . '/../fpdi/fpdi.php');
define('PDF_FONT_SIZE', 12); // points
define('CHECK_HEIGHT', 3.5); // inch height of checks
class Pdf extends \TCPDF {
private $lastX; // x-coordinate of last text
private $lastY; // y-coordinate of last text
public $pageW; // page width
public $pageH; // page height
public $lineH; // standard line height in inches
private $hdr = []; // header array [logo, L, C, R]
private $fdr = []; // footer array [logo, L, C, R]
/**
* Create a PDF object.
*/
public function __construct() {
parent::__construct('P', 'in', 'LETTER');
$this->pageW = $this->getPageWidth();
$this->pageH = $this->getPageHeight();
$this->lineH = PDF_FONT_SIZE / 72;
}
/**
* Put some text somewhere on the page.
* @param string $text: what to say
* @param int $x, $y: position coordinates (negative means from trailing edge, '' for same as previous)
* @param int $w, $h: box size (defaults to until page edge)
* If $h is specified, text is justified to the bottom of the box.
* @param string $format: semi-colon-delimited string list of formats: B, I, CAPS, <font-size>, <font-family>
* (default normal, Arial, 10-point)
* @param string $align: horizontal alignment (L, R, C)
* @param mixed $borders: border width if numeric or "B" for bottom, "R" for right, etc. (default none)
*/
public function say($text, $x, $y, $w = '', $h = '', $format = '', $align = '', $borders = 0) {
global $pdfSays; if (isDEV) $pdfSays[] = $text;
// maybe use this instead of pdfSays? global $formOut; if (isDEV) $formOut['text'] .= "$text ";
$formats = [];
$font = 'Arial';
$size = PDF_FONT_SIZE;
if ($x === '') $x = $this->lastX;
if ($y === '') $y = $this->lastY;
if ($w === '') $w = $this->pageW - $x;
$h0 = $h;
if ($h === '') $h = $this->pageH - $y;
if ($format and $fmts = explode(';', $format)) foreach ($fmts as $fmt) if ($fmt) {
if (strpos($fmt, ':')) {$formats[] = "$fmt;"; continue;}
if ($fmt == 'B') {$formats[] = 'font-weight:bold;'; continue;}
if ($fmt == 'I') {$formats[] = 'font-style:italic;'; continue;}
if ($fmt == 'CAPS') {$text = str_replace('<BR>', '<br>', strtoupper($text)); continue;}
if (is_numeric($fmt)) {$size = $fmt; continue;}
$font = $fmt;
}
$formats[] = "font-family:$font;";
$formats[] = "font-size:{$size}pt;";
$format = join(' ', $formats);
$offset = $h0 ? ((PDF_FONT_SIZE - $size) / 72) * (substr_count($text, '<br>') + 1) : 0;
$text = "<div style=\"$format\">$text</div>";
$this->writeHTMLCell($w, $h, $x, $y + $offset, $text, $borders, 0, FALSE, TRUE, $align);
list ($this->lastX, $this->lastY) = [$x, $y];
/// debug("wrote $text at $x,$y");
}
/**
* Output a cell
* Overrides parent just for testing.
*/
public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='', $stretch=0, $ignore_min_height=false, $calign='T', $valign='M') {
global $formOut; if (isDEV) $formOut['text'] = $formOut['text'] . "$txt ";
parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link, $stretch, $ignore_min_height, $calign, $valign);
}
public function setupHeader($args) {$this->hdr = $this->map(func_get_args());}
public function setupFooter($args) {$this->ftr = $this->map(func_get_args());}
public function Header() {
$m = .25; // basic margin
if ($this->hdr[0]) $this->Image($this->hdr[0], $m + 2, $m, 10, '', 'PNG', '', 'T', false, 300, '', false, false, 0, false, false, false);
$this->SetFont('helvetica', '', 9);
$this->say($this->hdr[1], $this->hdr[0] ? $m + .5 : $m, $m);
$this->say($this->hdr[2], $m, $m, '', '', '', 'C');
$this->say($this->hdr[3], $m, $m, '', '', '', 'R');
$this->Line($m, $y = $m + 1.5, $this->pageW - $m, $y); // set y to max after L,C,or R
}
public function Footer() {
$m = .25; // basic margin
$y = $this->pageH - $m - .25;
$this->Line($m, $y - .1, $this->pageW - $m, $y - .1);
$this->say($this->ftr[1], $this->ftr[0] ? $m + .5 : $m, $y);
if (is_array($center = $this->ftr[2])) {
list ($center, $format) = $center;
} else $format = '';
$this->say($center, $m, $y, '', '', $format, 'C');
$this->say($this->ftr[3], $m, $y, '', '', '', 'R');
}
/**
* Return headers or footer content, with standard replacements.
*/
private function map($content) {
if (is_array($content)) {
foreach ($content as $k => $v) $content[$k] = $this->map($v);
return $content;
}
return strtr($content, [
'@PAGE' => $this->getAliasNumPage(),
'@PAGES' => $this->getAliasNbPages(),
]);
}
/**
* Output a solid-background section header bar.
* @param string $subtitle: section header text
*/
function barHead($subtitle) {
$this->SetTextColor(255,255,255);
$this->setFontSize(1.2 * PDF_FONT_SIZE);
$this->SetFillColor(0,0,128);
$this->Cell(0, 0, $subtitle, '', 1, 'C', TRUE);
$this->setFontSize(.5 * PDF_FONT_SIZE);
$this->newLine();
$this->setFontSize(PDF_FONT_SIZE);
$this->SetTextColor();
}
/**
* Output column headers.
*/
function colHeads($fields) {
$this->SetTextColor(0,0,128);
foreach ($fields as $one) {
list ($head, $fieldName, $width, $align) = explode('/', $one);
$this->Cell($width, 0, $head, '', 0, $align);
}
$this->newLine();
$this->SetTextColor();
}
function newLine() {$this->Cell(0, 0, '', '', 1);}
/**
* Close and output PDF document
* @param string $dest: I=inline (to browser) D=download F=to file,...
*/
public function finish($filename = '', $dest = 'I') { // must be same args and defaults as in TCPDF
if (isDEV) parent::Output('testOutput.pdf', 'F'); // for testing
parent::Output($filename, $dest);
$this->Close();
}
}