Complex classes like FPDF often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FPDF, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 4 | |||
| 5 | class FPDF |
||
|
|
|||
| 6 | { |
||
| 7 | const FPDF_VERSION = '1.81'; |
||
| 8 | const FPDF_FONTPATH = 'font/'; |
||
| 9 | |||
| 10 | protected $page; // current page number |
||
| 11 | protected $n; // current object number |
||
| 12 | protected $offsets; // array of object offsets |
||
| 13 | protected $buffer; // buffer holding in-memory PDF |
||
| 14 | protected $pages; // array containing pages |
||
| 15 | protected $state; // current document state |
||
| 16 | protected $compress; // compression flag |
||
| 17 | protected $k; // scale factor (number of points in user unit) |
||
| 18 | protected $defOrientation; // default orientation |
||
| 19 | protected $curOrientation; // current orientation |
||
| 20 | protected $stdPageSizes; // standard page sizes |
||
| 21 | protected $defPageSize; // default page size |
||
| 22 | protected $curPageSize; // current page size |
||
| 23 | protected $curRotation; // current page rotation |
||
| 24 | protected $pageInfo; // page-related data |
||
| 25 | protected $wPt; |
||
| 26 | protected $hPt; // dimensions of current page in points |
||
| 27 | protected $w; |
||
| 28 | protected $h; // dimensions of current page in user unit |
||
| 29 | protected $lMargin; // left margin |
||
| 30 | protected $tMargin; // top margin |
||
| 31 | protected $rMargin; // right margin |
||
| 32 | protected $bMargin; // page break margin |
||
| 33 | protected $cMargin; // cell margin |
||
| 34 | protected $x; |
||
| 35 | protected $y; // current position in user unit |
||
| 36 | protected $lasth; // height of last printed cell |
||
| 37 | protected $lineWidth; // line width in user unit |
||
| 38 | protected $fontpath; // path containing fonts |
||
| 39 | protected $coreFonts; // array of core font names |
||
| 40 | protected $fonts; // array of used fonts |
||
| 41 | protected $fontFiles; // array of font files |
||
| 42 | protected $encodings; // array of encodings |
||
| 43 | protected $cmaps; // array of ToUnicode CMaps |
||
| 44 | protected $fontFamily; // current font family |
||
| 45 | protected $fontStyle; // current font style |
||
| 46 | protected $underline; // underlining flag |
||
| 47 | protected $currentFont; // current font info |
||
| 48 | protected $fontSizePt; // current font size in points |
||
| 49 | protected $fontSize; // current font size in user unit |
||
| 50 | protected $drawColor; // commands for drawing color |
||
| 51 | protected $fillColor; // commands for filling color |
||
| 52 | protected $textColor; // commands for text color |
||
| 53 | protected $colorFlag; // indicates whether fill and text colors are different |
||
| 54 | protected $withAlpha; // indicates whether alpha channel is used |
||
| 55 | protected $ws; // word spacing |
||
| 56 | protected $images; // array of used images |
||
| 57 | protected $pageLinks; // array of links in pages |
||
| 58 | protected $links; // array of internal links |
||
| 59 | protected $autoPageBreak; // automatic page breaking |
||
| 60 | protected $pageBreakTrigger; // threshold used to trigger page breaks |
||
| 61 | protected $inHeader; // flag set when processing header |
||
| 62 | protected $infooter; // flag set when processing footer |
||
| 63 | protected $aliasNbPages; // alias for total number of pages |
||
| 64 | protected $zoomMode; // zoom display mode |
||
| 65 | protected $layoutMode; // layout display mode |
||
| 66 | protected $metadata; // document properties |
||
| 67 | protected $pdfVersion; // PDF version number |
||
| 68 | |||
| 69 | public function __construct($orientation = 'P', $unit = 'mm', $size = 'A4') |
||
| 70 | { |
||
| 71 | // Some checks |
||
| 72 | $this->doChecks(); |
||
| 73 | // Initialization of properties |
||
| 74 | $this->state = 0; |
||
| 75 | $this->page = 0; |
||
| 76 | $this->n = 2; |
||
| 77 | $this->buffer = ''; |
||
| 78 | $this->pages = []; |
||
| 79 | $this->pageInfo = []; |
||
| 80 | $this->fonts = []; |
||
| 81 | $this->fontFiles = []; |
||
| 82 | $this->encodings = []; |
||
| 83 | $this->cmaps = []; |
||
| 84 | $this->images = []; |
||
| 85 | $this->links = []; |
||
| 86 | $this->inHeader = false; |
||
| 87 | $this->infooter = false; |
||
| 88 | $this->lasth = 0; |
||
| 89 | $this->fontFamily = ''; |
||
| 90 | $this->fontStyle = ''; |
||
| 91 | $this->fontSizePt = 12; |
||
| 92 | $this->underline = false; |
||
| 93 | $this->drawColor = '0 G'; |
||
| 94 | $this->fillColor = '0 g'; |
||
| 95 | $this->textColor = '0 g'; |
||
| 96 | $this->colorFlag = false; |
||
| 97 | $this->withAlpha = false; |
||
| 98 | $this->ws = 0; |
||
| 99 | |||
| 100 | $this->fontpath = __DIR__. FPDF_FONTPATH; |
||
| 101 | |||
| 102 | // Core fonts |
||
| 103 | $this->coreFonts = [ |
||
| 104 | 'courier', |
||
| 105 | 'helvetica', |
||
| 106 | 'times', |
||
| 107 | 'symbol', |
||
| 108 | 'zapfdingbats' |
||
| 109 | ]; |
||
| 110 | |||
| 111 | switch ($unit) { |
||
| 112 | case 'pt': |
||
| 113 | $this->k = 1; |
||
| 114 | break; |
||
| 115 | case 'cm': |
||
| 116 | $this->k = 72 / 2.54; |
||
| 117 | break; |
||
| 118 | case 'in': |
||
| 119 | $this->k = 72; |
||
| 120 | break; |
||
| 121 | case 'mm': |
||
| 122 | default: |
||
| 123 | $this->k = 72 / 25.4; |
||
| 124 | } |
||
| 125 | |||
| 126 | // Page sizes |
||
| 127 | $this->stdPageSizes = [ |
||
| 128 | 'a3' => [841.89, 1190.55], |
||
| 129 | 'a4' => [595.28, 841.89], |
||
| 130 | 'a5' => [420.94, 595.28], |
||
| 131 | 'letter' => [612, 792], |
||
| 132 | 'legal' => [612, 1008] |
||
| 133 | ]; |
||
| 134 | |||
| 135 | $size = $this->getPageSize($size); |
||
| 136 | $this->defPageSize = $size; |
||
| 137 | $this->curPageSize = $size; |
||
| 138 | // Page orientation |
||
| 139 | $orientation = strtolower($orientation); |
||
| 140 | if ($orientation == 'p' || $orientation == 'portrait') { |
||
| 141 | $this->defOrientation = 'P'; |
||
| 142 | $this->w = $size[0]; |
||
| 143 | $this->h = $size[1]; |
||
| 144 | } elseif ($orientation == 'l' || $orientation == 'landscape') { |
||
| 145 | $this->defOrientation = 'L'; |
||
| 146 | $this->w = $size[1]; |
||
| 147 | $this->h = $size[0]; |
||
| 148 | } else { |
||
| 149 | $this->defOrientation = 'P'; |
||
| 150 | $this->w = $size[0]; |
||
| 151 | $this->h = $size[1]; |
||
| 152 | } |
||
| 153 | $this->curOrientation = $this->defOrientation; |
||
| 154 | $this->wPt = $this->w * $this->k; |
||
| 155 | $this->hPt = $this->h * $this->k; |
||
| 156 | // Page rotation |
||
| 157 | $this->curRotation = 0; |
||
| 158 | // Page margins (1 cm) |
||
| 159 | $margin = 28.35 / $this->k; |
||
| 160 | $this->setMargins($margin, $margin); |
||
| 161 | // Interior cell margin (1 mm) |
||
| 162 | $this->cMargin = $margin / 10; |
||
| 163 | // Line width (0.2 mm) |
||
| 164 | $this->lineWidth = .567 / $this->k; |
||
| 165 | // Automatic page break |
||
| 166 | $this->setautoPageBreak(true, 2 * $margin); |
||
| 167 | // Default display mode |
||
| 168 | $this->setDisplayMode('default'); |
||
| 169 | // Enable compression |
||
| 170 | $this->setCompression(true); |
||
| 171 | // Set default PDF version number |
||
| 172 | $this->pdfVersion = '1.3'; |
||
| 173 | } |
||
| 174 | |||
| 175 | public function setMargins($left, $top, $right = null) |
||
| 176 | { |
||
| 177 | // Set left, top and right margins |
||
| 178 | $this->lMargin = $left; |
||
| 179 | $this->tMargin = $top; |
||
| 180 | if ($right === null) { |
||
| 181 | $right = $left; |
||
| 182 | } |
||
| 183 | $this->rMargin = $right; |
||
| 184 | } |
||
| 185 | |||
| 186 | public function setLeftMargin($margin) |
||
| 187 | { |
||
| 188 | // Set left margin |
||
| 189 | $this->lMargin = $margin; |
||
| 190 | if ($this->page > 0 && $this->x < $margin) { |
||
| 191 | $this->x = $margin; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | public function setTopMargin($margin) |
||
| 196 | { |
||
| 197 | // Set top margin |
||
| 198 | $this->tMargin = $margin; |
||
| 199 | } |
||
| 200 | |||
| 201 | public function setRightMargin($margin) |
||
| 202 | { |
||
| 203 | // Set right margin |
||
| 204 | $this->rMargin = $margin; |
||
| 205 | } |
||
| 206 | |||
| 207 | public function setautoPageBreak($auto, $margin = 0) |
||
| 208 | { |
||
| 209 | // Set auto page break mode and triggering margin |
||
| 210 | $this->autoPageBreak = $auto; |
||
| 211 | $this->bMargin = $margin; |
||
| 212 | $this->pageBreakTrigger = $this->h - $margin; |
||
| 213 | } |
||
| 214 | public function setDisplayMode($zoom, $layout = 'default') |
||
| 215 | { |
||
| 216 | // Set display mode in viewer |
||
| 217 | if ($zoom == 'fullpage' || $zoom == 'fullwidth' || $zoom == 'real' || $zoom == 'default' || !is_string($zoom)) { |
||
| 218 | $this->zoomMode = $zoom; |
||
| 219 | } else { |
||
| 220 | $this->zoomMode = 'fullpage'; |
||
| 221 | } |
||
| 222 | if ($layout == 'single' || $layout == 'continuous' || $layout == 'two' || $layout == 'default') { |
||
| 223 | $this->layoutMode = $layout; |
||
| 224 | } else { |
||
| 225 | $this->layoutMode = 'single'; |
||
| 226 | } |
||
| 227 | } |
||
| 228 | |||
| 229 | public function setCompression($compress) |
||
| 230 | { |
||
| 231 | // Set page compression |
||
| 232 | if (function_exists('gzcompress')) { |
||
| 233 | $this->compress = $compress; |
||
| 234 | } else { |
||
| 235 | $this->compress = false; |
||
| 236 | } |
||
| 237 | } |
||
| 238 | |||
| 239 | public function setTitle($title, $isUTF8 = false) |
||
| 240 | { |
||
| 241 | // Title of document |
||
| 242 | $this->metadata['Title'] = $isUTF8 ? $title : utf8_encode($title); |
||
| 243 | } |
||
| 244 | |||
| 245 | public function setAuthor($author, $isUTF8 = false) |
||
| 246 | { |
||
| 247 | // Author of document |
||
| 248 | $this->metadata['Author'] = $isUTF8 ? $author : utf8_encode($author); |
||
| 249 | } |
||
| 250 | |||
| 251 | public function setSubject($subject, $isUTF8 = false) |
||
| 252 | { |
||
| 253 | // Subject of document |
||
| 254 | $this->metadata['Subject'] = $isUTF8 ? $subject : utf8_encode($subject); |
||
| 255 | } |
||
| 256 | |||
| 257 | public function setKeywords($keywords, $isUTF8 = false) |
||
| 258 | { |
||
| 259 | // Keywords of document |
||
| 260 | $this->metadata['Keywords'] = $isUTF8 ? $keywords : utf8_encode($keywords); |
||
| 261 | } |
||
| 262 | |||
| 263 | public function setCreator($creator, $isUTF8 = false) |
||
| 264 | { |
||
| 265 | // Creator of document |
||
| 266 | $this->metadata['Creator'] = $isUTF8 ? $creator : utf8_encode($creator); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function aliasNbPages($alias = '{nb}') |
||
| 270 | { |
||
| 271 | // Define an alias for total number of pages |
||
| 272 | $this->aliasNbPages = $alias; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function error($msg) |
||
| 276 | { |
||
| 277 | // Fatal error |
||
| 278 | throw new \Exception('FPDF error: ' . $msg); |
||
| 279 | } |
||
| 280 | |||
| 281 | public function close() |
||
| 282 | { |
||
| 283 | // Terminate document |
||
| 284 | if ($this->state == 3) { |
||
| 285 | return; |
||
| 286 | } |
||
| 287 | if ($this->page == 0) { |
||
| 288 | $this->addPage(); |
||
| 289 | } |
||
| 290 | // Page footer |
||
| 291 | $this->infooter = true; |
||
| 292 | $this->footer(); |
||
| 293 | $this->infooter = false; |
||
| 294 | // close page |
||
| 295 | $this->endPage(); |
||
| 296 | // close document |
||
| 297 | $this->endDoc(); |
||
| 298 | } |
||
| 299 | |||
| 300 | public function addPage($orientation = '', $size = '', $rotation = 0) |
||
| 301 | { |
||
| 302 | // Start a new page |
||
| 303 | if ($this->state == 3) { |
||
| 304 | $this->error('The document is closed'); |
||
| 305 | } |
||
| 306 | $family = $this->fontFamily; |
||
| 307 | $style = $this->fontStyle . ($this->underline ? 'U' : ''); |
||
| 308 | $fontsize = $this->fontSizePt; |
||
| 309 | $lw = $this->lineWidth; |
||
| 310 | $dc = $this->drawColor; |
||
| 311 | $fc = $this->fillColor; |
||
| 312 | $tc = $this->textColor; |
||
| 313 | $cf = $this->colorFlag; |
||
| 314 | if ($this->page > 0) { |
||
| 315 | // Page footer |
||
| 316 | $this->infooter = true; |
||
| 317 | $this->footer(); |
||
| 318 | $this->infooter = false; |
||
| 319 | // close page |
||
| 320 | $this->endPage(); |
||
| 321 | } |
||
| 322 | // Start new page |
||
| 323 | $this->beginPage($orientation, $size, $rotation); |
||
| 324 | // Set line cap style to square |
||
| 325 | $this->out('2 J'); |
||
| 326 | // Set line width |
||
| 327 | $this->lineWidth = $lw; |
||
| 328 | $this->out(sprintf('%.2F w', $lw * $this->k)); |
||
| 329 | // Set font |
||
| 330 | if ($family) { |
||
| 331 | $this->setFont($family, $style, $fontsize); |
||
| 332 | } |
||
| 333 | // Set colors |
||
| 334 | $this->drawColor = $dc; |
||
| 335 | if ($dc != '0 G') { |
||
| 336 | $this->out($dc); |
||
| 337 | } |
||
| 338 | $this->fillColor = $fc; |
||
| 339 | if ($fc != '0 g') { |
||
| 340 | $this->out($fc); |
||
| 341 | } |
||
| 342 | $this->textColor = $tc; |
||
| 343 | $this->colorFlag = $cf; |
||
| 344 | // Page header |
||
| 345 | $this->inHeader = true; |
||
| 346 | $this->header(); |
||
| 347 | $this->inHeader = false; |
||
| 348 | // Restore line width |
||
| 349 | if ($this->lineWidth != $lw) { |
||
| 350 | $this->lineWidth = $lw; |
||
| 351 | $this->out(sprintf('%.2F w', $lw * $this->k)); |
||
| 352 | } |
||
| 353 | // Restore font |
||
| 354 | if ($family) { |
||
| 355 | $this->setFont($family, $style, $fontsize); |
||
| 356 | } |
||
| 357 | // Restore colors |
||
| 358 | if ($this->drawColor != $dc) { |
||
| 359 | $this->drawColor = $dc; |
||
| 360 | $this->out($dc); |
||
| 361 | } |
||
| 362 | if ($this->fillColor != $fc) { |
||
| 363 | $this->fillColor = $fc; |
||
| 364 | $this->out($fc); |
||
| 365 | } |
||
| 366 | $this->textColor = $tc; |
||
| 367 | $this->colorFlag = $cf; |
||
| 368 | } |
||
| 369 | |||
| 370 | public function header() |
||
| 371 | { |
||
| 372 | // To be implemented in your own inherited class |
||
| 373 | } |
||
| 374 | |||
| 375 | public function footer() |
||
| 376 | { |
||
| 377 | // To be implemented in your own inherited class |
||
| 378 | } |
||
| 379 | |||
| 380 | public function pageNo() |
||
| 381 | { |
||
| 382 | // Get current page number |
||
| 383 | return $this->page; |
||
| 384 | } |
||
| 385 | public function setdrawColor($r, $g = null, $b = null) |
||
| 386 | { |
||
| 387 | // Set color for all stroking operations |
||
| 388 | if (($r == 0 && $g == 0 && $b == 0) || $g === null) { |
||
| 389 | $this->drawColor = sprintf('%.3F G', $r / 255); |
||
| 390 | } else { |
||
| 391 | $this->drawColor = sprintf('%.3F %.3F %.3F RG', $r / 255, $g / 255, $b / 255); |
||
| 392 | } |
||
| 393 | if ($this->page > 0) { |
||
| 394 | $this->out($this->drawColor); |
||
| 395 | } |
||
| 396 | } |
||
| 397 | |||
| 398 | public function setfillColor($r, $g = null, $b = null) |
||
| 399 | { |
||
| 400 | // Set color for all filling operations |
||
| 401 | if (($r == 0 && $g == 0 && $b == 0) || $g === null) { |
||
| 402 | $this->fillColor = sprintf('%.3F g', $r / 255); |
||
| 403 | } else { |
||
| 404 | $this->fillColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255); |
||
| 405 | } |
||
| 406 | $this->colorFlag = ($this->fillColor != $this->textColor); |
||
| 407 | if ($this->page > 0) { |
||
| 408 | $this->out($this->fillColor); |
||
| 409 | } |
||
| 410 | } |
||
| 411 | |||
| 412 | public function settextColor($r, $g = null, $b = null) |
||
| 413 | { |
||
| 414 | // Set color for text |
||
| 415 | if (($r == 0 && $g == 0 && $b == 0) || $g === null) { |
||
| 416 | $this->textColor = sprintf('%.3F g', $r / 255); |
||
| 417 | } else { |
||
| 418 | $this->textColor = sprintf('%.3F %.3F %.3F rg', $r / 255, $g / 255, $b / 255); |
||
| 419 | } |
||
| 420 | $this->colorFlag = ($this->fillColor != $this->textColor); |
||
| 421 | } |
||
| 422 | |||
| 423 | public function getStringWidth($s) |
||
| 424 | { |
||
| 425 | // Get width of a string in the current font |
||
| 426 | $s = (string) $s; |
||
| 427 | $cw = &$this->currentFont['cw']; |
||
| 428 | $w = 0; |
||
| 429 | $l = strlen($s); |
||
| 430 | for ($i = 0; $i < $l; $i++) { |
||
| 431 | $w += $cw[$s[$i]]; |
||
| 432 | } |
||
| 433 | return $w * $this->fontSize / 1000; |
||
| 434 | } |
||
| 435 | |||
| 436 | public function setlineWidth($width) |
||
| 437 | { |
||
| 438 | // Set line width |
||
| 439 | $this->lineWidth = $width; |
||
| 440 | if ($this->page > 0) { |
||
| 441 | $this->out(sprintf('%.2F w', $width * $this->k)); |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | public function line($x1, $y1, $x2, $y2) |
||
| 446 | { |
||
| 447 | // Draw a line |
||
| 448 | $this->out( |
||
| 449 | sprintf( |
||
| 450 | '%.2F %.2F m %.2F %.2F l S', |
||
| 451 | $x1 * $this->k, |
||
| 452 | ($this->h - $y1) * $this->k, |
||
| 453 | $x2 * $this->k, |
||
| 454 | ($this->h - $y2) * $this->k |
||
| 455 | ) |
||
| 456 | ); |
||
| 457 | } |
||
| 458 | |||
| 459 | public function rect($x, $y, $w, $h, $style = '') |
||
| 460 | { |
||
| 461 | // Draw a rectangle |
||
| 462 | if ($style == 'F') { |
||
| 463 | $op = 'f'; |
||
| 464 | } elseif ($style == 'FD' || $style == 'DF') { |
||
| 465 | $op = 'B'; |
||
| 466 | } else { |
||
| 467 | $op = 'S'; |
||
| 468 | } |
||
| 469 | $this->out( |
||
| 470 | sprintf( |
||
| 471 | '%.2F %.2F %.2F %.2F re %s', |
||
| 472 | $x * $this->k, |
||
| 473 | ($this->h - $y) * $this->k, |
||
| 474 | $w * $this->k, |
||
| 475 | -$h * $this->k, |
||
| 476 | $op |
||
| 477 | ) |
||
| 478 | ); |
||
| 479 | } |
||
| 480 | |||
| 481 | public function addFont($family, $style = '', $file = '') |
||
| 482 | { |
||
| 483 | // Add a TrueType, OpenType or Type1 font |
||
| 484 | $family = strtolower($family); |
||
| 485 | if ($file == '') { |
||
| 486 | $file = str_replace(' ', '', $family) . strtolower($style) . '.php'; |
||
| 487 | } |
||
| 488 | $style = strtoupper($style); |
||
| 489 | if ($style == 'IB') { |
||
| 490 | $style = 'BI'; |
||
| 491 | } |
||
| 492 | $fontkey = $family . $style; |
||
| 493 | if (isset($this->fonts[$fontkey])) { |
||
| 494 | return; |
||
| 495 | } |
||
| 496 | $info = $this->loadFont($file); |
||
| 497 | $info['i'] = count($this->fonts) + 1; |
||
| 498 | if (!empty($info['file'])) { |
||
| 499 | // Embedded font |
||
| 500 | if ($info['type'] == 'TrueType') { |
||
| 501 | $this->fontFiles[$info['file']] = ['length1' => $info['originalsize']]; |
||
| 502 | } else { |
||
| 503 | $this->fontFiles[$info['file']] = ['length1' => $info['size1'], 'length2' => $info['size2']]; |
||
| 504 | } |
||
| 505 | } |
||
| 506 | $this->fonts[$fontkey] = $info; |
||
| 507 | } |
||
| 508 | |||
| 509 | public function setFont($family, $style = '', $size = 0) |
||
| 510 | { |
||
| 511 | // Select a font; size given in points |
||
| 512 | if ($family == '') { |
||
| 513 | $family = $this->fontFamily; |
||
| 514 | } else { |
||
| 515 | $family = strtolower($family); |
||
| 516 | } |
||
| 517 | $style = strtoupper($style); |
||
| 518 | if (strpos($style, 'U') !== false) { |
||
| 519 | $this->underline = true; |
||
| 520 | $style = str_replace('U', '', $style); |
||
| 521 | } else { |
||
| 522 | $this->underline = false; |
||
| 523 | } |
||
| 524 | if ($style == 'IB') { |
||
| 525 | $style = 'BI'; |
||
| 526 | } |
||
| 527 | if ($size == 0) { |
||
| 528 | $size = $this->fontSizePt; |
||
| 529 | } |
||
| 530 | // Test if font is already selected |
||
| 531 | if ($this->fontFamily == $family && $this->fontStyle == $style && $this->fontSizePt == $size) { |
||
| 532 | return; |
||
| 533 | } |
||
| 534 | // Test if font is already loaded |
||
| 535 | $fontkey = $family . $style; |
||
| 536 | if (!isset($this->fonts[$fontkey])) { |
||
| 537 | // Test if one of the core fonts |
||
| 538 | if ($family == 'arial') { |
||
| 539 | $family = 'helvetica'; |
||
| 540 | } |
||
| 541 | if (in_array($family, $this->coreFonts)) { |
||
| 542 | if ($family == 'symbol' || $family == 'zapfdingbats') { |
||
| 543 | $style = ''; |
||
| 544 | } |
||
| 545 | $fontkey = $family . $style; |
||
| 546 | if (!isset($this->fonts[$fontkey])) { |
||
| 547 | $this->addFont($family, $style); |
||
| 548 | } |
||
| 549 | } else { |
||
| 550 | $this->error('Undefined font: ' . $family . ' ' . $style); |
||
| 551 | } |
||
| 552 | } |
||
| 553 | // Select it |
||
| 554 | $this->fontFamily = $family; |
||
| 555 | $this->fontStyle = $style; |
||
| 556 | $this->fontSizePt = $size; |
||
| 557 | $this->fontSize = $size / $this->k; |
||
| 558 | $this->currentFont = &$this->fonts[$fontkey]; |
||
| 559 | if ($this->page > 0) { |
||
| 560 | $this->out(sprintf('BT /F%d %.2F Tf ET', $this->currentFont['i'], $this->fontSizePt)); |
||
| 561 | } |
||
| 562 | } |
||
| 563 | |||
| 564 | public function setfontSize($size) |
||
| 565 | { |
||
| 566 | // Set font size in points |
||
| 567 | if ($this->fontSizePt == $size) { |
||
| 568 | return; |
||
| 569 | } |
||
| 570 | $this->fontSizePt = $size; |
||
| 571 | $this->fontSize = $size / $this->k; |
||
| 572 | if ($this->page > 0) { |
||
| 573 | $this->out(sprintf('BT /F%d %.2F Tf ET', $this->currentFont['i'], $this->fontSizePt)); |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | public function addLink() |
||
| 578 | { |
||
| 579 | // Create a new internal link |
||
| 580 | $n = count($this->links) + 1; |
||
| 581 | $this->links[$n] = [0, 0]; |
||
| 582 | return $n; |
||
| 583 | } |
||
| 584 | |||
| 585 | public function setLink($link, $y = 0, $page = -1) |
||
| 586 | { |
||
| 587 | // Set destination of internal link |
||
| 588 | if ($y == -1) { |
||
| 589 | $y = $this->y; |
||
| 590 | } |
||
| 591 | if ($page == -1) { |
||
| 592 | $page = $this->page; |
||
| 593 | } |
||
| 594 | $this->links[$link] = [$page, $y]; |
||
| 595 | } |
||
| 596 | |||
| 597 | public function link($x, $y, $w, $h, $link) |
||
| 598 | { |
||
| 599 | // Put a link on the page |
||
| 600 | $this->pageLinks[$this->page][] = [ |
||
| 601 | $x * $this->k, |
||
| 602 | $this->hPt - $y * $this->k, |
||
| 603 | $w * $this->k, |
||
| 604 | $h * $this->k, |
||
| 605 | $link |
||
| 606 | ]; |
||
| 607 | } |
||
| 608 | |||
| 609 | public function text($x, $y, $txt) |
||
| 610 | { |
||
| 611 | // output a string |
||
| 612 | if (!isset($this->currentFont)) { |
||
| 613 | $this->error('No font has been set'); |
||
| 614 | } |
||
| 615 | $s = sprintf( |
||
| 616 | 'BT %.2F %.2F Td (%s) Tj ET', |
||
| 617 | $x * $this->k, |
||
| 618 | ($this->h - $y) * $this->k, |
||
| 619 | $this->escape($txt) |
||
| 620 | ); |
||
| 621 | if ($this->underline && $txt != '') { |
||
| 622 | $s .= ' ' . $this->doUnderLine($x, $y, $txt); |
||
| 623 | } |
||
| 624 | if ($this->colorFlag) { |
||
| 625 | $s = 'q ' . $this->textColor . ' ' . $s . ' Q'; |
||
| 626 | } |
||
| 627 | $this->out($s); |
||
| 628 | } |
||
| 629 | |||
| 630 | public function acceptPageBreak() |
||
| 631 | { |
||
| 632 | // Accept automatic page break or not |
||
| 633 | return $this->autoPageBreak; |
||
| 634 | } |
||
| 635 | |||
| 636 | public function cell($w, $h = 0, $txt = '', $border = 0, $ln = 0, $align = '', $fill = false, $link = '') |
||
| 637 | { |
||
| 638 | // output a cell |
||
| 639 | $k = $this->k; |
||
| 640 | if ($this->y + $h > $this->pageBreakTrigger |
||
| 641 | && !$this->inHeader |
||
| 642 | && !$this->infooter |
||
| 643 | && $this->acceptPageBreak() |
||
| 644 | ) { |
||
| 645 | // Automatic page break |
||
| 646 | $x = $this->x; |
||
| 647 | $ws = $this->ws; |
||
| 648 | if ($ws > 0) { |
||
| 649 | $this->ws = 0; |
||
| 650 | $this->out('0 Tw'); |
||
| 651 | } |
||
| 652 | $this->addPage($this->curOrientation, $this->curPageSize, $this->curRotation); |
||
| 653 | $this->x = $x; |
||
| 654 | if ($ws > 0) { |
||
| 655 | $this->ws = $ws; |
||
| 656 | $this->out(sprintf('%.3F Tw', $ws * $k)); |
||
| 657 | } |
||
| 658 | } |
||
| 659 | if ($w == 0) { |
||
| 660 | $w = $this->w - $this->rMargin - $this->x; |
||
| 661 | } |
||
| 662 | $s = ''; |
||
| 663 | if ($fill || $border == 1) { |
||
| 664 | if ($fill) { |
||
| 665 | $op = ($border == 1) ? 'B' : 'f'; |
||
| 666 | } else { |
||
| 667 | $op = 'S'; |
||
| 668 | } |
||
| 669 | $s = sprintf( |
||
| 670 | '%.2F %.2F %.2F %.2F re %s ', |
||
| 671 | $this->x * $k, |
||
| 672 | ($this->h - $this->y) * $k, |
||
| 673 | $w * $k, |
||
| 674 | -$h * $k, |
||
| 675 | $op |
||
| 676 | ); |
||
| 677 | } |
||
| 678 | if (is_string($border)) { |
||
| 679 | $x = $this->x; |
||
| 680 | $y = $this->y; |
||
| 681 | if (strpos($border, 'L') !== false) { |
||
| 682 | $s .= sprintf( |
||
| 683 | '%.2F %.2F m %.2F %.2F l S ', |
||
| 684 | $x * $k, |
||
| 685 | ($this->h - $y) * $k, |
||
| 686 | $x * $k, |
||
| 687 | ($this->h - ($y + $h)) * $k |
||
| 688 | ); |
||
| 689 | } |
||
| 690 | if (strpos($border, 'T') !== false) { |
||
| 691 | $s .= sprintf( |
||
| 692 | '%.2F %.2F m %.2F %.2F l S ', |
||
| 693 | $x * $k, |
||
| 694 | ($this->h - $y) * $k, |
||
| 695 | ($x + $w) * $k, |
||
| 696 | ($this->h - $y) * $k |
||
| 697 | ); |
||
| 698 | } |
||
| 699 | if (strpos($border, 'R') !== false) { |
||
| 700 | $s .= sprintf( |
||
| 701 | '%.2F %.2F m %.2F %.2F l S ', |
||
| 702 | ($x + $w) * $k, |
||
| 703 | ($this->h - $y) * $k, |
||
| 704 | ($x + $w) * $k, |
||
| 705 | ($this->h - ($y + $h)) * $k |
||
| 706 | ); |
||
| 707 | } |
||
| 708 | if (strpos($border, 'B') !== false) { |
||
| 709 | $s .= sprintf( |
||
| 710 | '%.2F %.2F m %.2F %.2F l S ', |
||
| 711 | $x * $k, |
||
| 712 | ($this->h - ($y + $h)) * $k, |
||
| 713 | ($x + $w) * $k, |
||
| 714 | ($this->h - ($y + $h)) * $k |
||
| 715 | ); |
||
| 716 | } |
||
| 717 | } |
||
| 718 | if ($txt !== '') { |
||
| 719 | if (!isset($this->currentFont)) { |
||
| 720 | $this->error('No font has been set'); |
||
| 721 | } |
||
| 722 | if ($align == 'R') { |
||
| 723 | $dx = $w - $this->cMargin - $this->getStringWidth($txt); |
||
| 724 | } elseif ($align == 'C') { |
||
| 725 | $dx = ($w - $this->getStringWidth($txt)) / 2; |
||
| 726 | } else { |
||
| 727 | $dx = $this->cMargin; |
||
| 728 | } |
||
| 729 | if ($this->colorFlag) { |
||
| 730 | $s .= 'q ' . $this->textColor . ' '; |
||
| 731 | } |
||
| 732 | $s .= sprintf( |
||
| 733 | 'BT %.2F %.2F Td (%s) Tj ET', |
||
| 734 | ($this->x + $dx) * $k, |
||
| 735 | ($this->h - ($this->y + .5 * $h + .3 * $this->fontSize)) * $k, |
||
| 736 | $this->escape($txt) |
||
| 737 | ); |
||
| 738 | if ($this->underline) { |
||
| 739 | $s .= ' ' . $this->doUnderLine( |
||
| 740 | $this->x + $dx, |
||
| 741 | $this->y + .5 * $h + .3 * $this->fontSize, |
||
| 742 | $txt |
||
| 743 | ); |
||
| 744 | } |
||
| 745 | if ($this->colorFlag) { |
||
| 746 | $s .= ' Q'; |
||
| 747 | } |
||
| 748 | if ($link) { |
||
| 749 | $this->link( |
||
| 750 | $this->x + $dx, |
||
| 751 | $this->y + .5 * $h - .5 * $this->fontSize, |
||
| 752 | $this->getStringWidth($txt), |
||
| 753 | $this->fontSize, |
||
| 754 | $link |
||
| 755 | ); |
||
| 756 | } |
||
| 757 | } |
||
| 758 | if ($s) { |
||
| 759 | $this->out($s); |
||
| 760 | } |
||
| 761 | $this->lasth = $h; |
||
| 762 | if ($ln > 0) { |
||
| 763 | // Go to next line |
||
| 764 | $this->y += $h; |
||
| 765 | if ($ln == 1) { |
||
| 766 | $this->x = $this->lMargin; |
||
| 767 | } |
||
| 768 | } else { |
||
| 769 | $this->x += $w; |
||
| 770 | } |
||
| 771 | } |
||
| 772 | |||
| 773 | public function multiCell($w, $h, $txt, $border = 0, $align = 'J', $fill = false) |
||
| 774 | { |
||
| 775 | // output text with automatic or explicit line breaks |
||
| 776 | if (!isset($this->currentFont)) { |
||
| 777 | $this->error('No font has been set'); |
||
| 778 | } |
||
| 779 | $cw = &$this->currentFont['cw']; |
||
| 780 | if ($w == 0) { |
||
| 781 | $w = $this->w - $this->rMargin - $this->x; |
||
| 782 | } |
||
| 783 | $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->fontSize; |
||
| 784 | $s = str_replace("\r", '', $txt); |
||
| 785 | $nb = strlen($s); |
||
| 786 | if ($nb > 0 && $s[$nb - 1] == "\n") { |
||
| 787 | $nb--; |
||
| 788 | } |
||
| 789 | $b = 0; |
||
| 790 | if ($border) { |
||
| 791 | if ($border == 1) { |
||
| 792 | $border = 'LTRB'; |
||
| 793 | $b = 'LRT'; |
||
| 794 | $b2 = 'LR'; |
||
| 795 | } else { |
||
| 796 | $b2 = ''; |
||
| 797 | if (strpos($border, 'L') !== false) { |
||
| 798 | $b2 .= 'L'; |
||
| 799 | } |
||
| 800 | if (strpos($border, 'R') !== false) { |
||
| 801 | $b2 .= 'R'; |
||
| 802 | } |
||
| 803 | $b = (strpos($border, 'T') !== false) ? $b2 . 'T' : $b2; |
||
| 804 | } |
||
| 805 | } |
||
| 806 | $sep = -1; |
||
| 807 | $i = 0; |
||
| 808 | $j = 0; |
||
| 809 | $l = 0; |
||
| 810 | $ns = 0; |
||
| 811 | $nl = 1; |
||
| 812 | while ($i < $nb) { |
||
| 813 | // Get next character |
||
| 814 | $c = $s[$i]; |
||
| 815 | if ($c == "\n") { |
||
| 816 | // Explicit line break |
||
| 817 | if ($this->ws > 0) { |
||
| 818 | $this->ws = 0; |
||
| 819 | $this->out('0 Tw'); |
||
| 820 | } |
||
| 821 | $this->cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); |
||
| 822 | $i++; |
||
| 823 | $sep = -1; |
||
| 824 | $j = $i; |
||
| 825 | $l = 0; |
||
| 826 | $ns = 0; |
||
| 827 | $nl++; |
||
| 828 | if ($border && $nl == 2) { |
||
| 829 | $b = $b2; |
||
| 830 | } |
||
| 831 | continue; |
||
| 832 | } |
||
| 833 | if ($c == ' ') { |
||
| 834 | $sep = $i; |
||
| 835 | $ls = $l; |
||
| 836 | $ns++; |
||
| 837 | } |
||
| 838 | $l += $cw[$c]; |
||
| 839 | if ($l > $wmax) { |
||
| 840 | // Automatic line break |
||
| 841 | if ($sep == -1) { |
||
| 842 | if ($i == $j) { |
||
| 843 | $i++; |
||
| 844 | } |
||
| 845 | if ($this->ws > 0) { |
||
| 846 | $this->ws = 0; |
||
| 847 | $this->out('0 Tw'); |
||
| 848 | } |
||
| 849 | $this->cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); |
||
| 850 | } else { |
||
| 851 | if ($align == 'J') { |
||
| 852 | $this->ws = ($ns > 1) |
||
| 853 | ? ($wmax - $ls) / 1000 * $this->fontSize / ($ns - 1) |
||
| 854 | : 0; |
||
| 855 | $this->out(sprintf('%.3F Tw', $this->ws * $this->k)); |
||
| 856 | } |
||
| 857 | $this->cell($w, $h, substr($s, $j, $sep - $j), $b, 2, $align, $fill); |
||
| 858 | $i = $sep + 1; |
||
| 859 | } |
||
| 860 | $sep = -1; |
||
| 861 | $j = $i; |
||
| 862 | $l = 0; |
||
| 863 | $ns = 0; |
||
| 864 | $nl++; |
||
| 865 | if ($border && $nl == 2) { |
||
| 866 | $b = $b2; |
||
| 867 | } |
||
| 868 | } else { |
||
| 869 | $i++; |
||
| 870 | } |
||
| 871 | } |
||
| 872 | // Last chunk |
||
| 873 | if ($this->ws > 0) { |
||
| 874 | $this->ws = 0; |
||
| 875 | $this->out('0 Tw'); |
||
| 876 | } |
||
| 877 | if ($border && strpos($border, 'B') !== false) { |
||
| 878 | $b .= 'B'; |
||
| 879 | } |
||
| 880 | $this->cell($w, $h, substr($s, $j, $i - $j), $b, 2, $align, $fill); |
||
| 881 | $this->x = $this->lMargin; |
||
| 882 | } |
||
| 883 | |||
| 884 | public function write($h, $txt, $link = '') |
||
| 885 | { |
||
| 886 | // output text in flowing mode |
||
| 887 | if (!isset($this->currentFont)) { |
||
| 888 | $this->error('No font has been set'); |
||
| 889 | } |
||
| 890 | $cw = &$this->currentFont['cw']; |
||
| 891 | $w = $this->w - $this->rMargin - $this->x; |
||
| 892 | $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->fontSize; |
||
| 893 | $s = str_replace("\r", '', $txt); |
||
| 894 | $nb = strlen($s); |
||
| 895 | $sep = -1; |
||
| 896 | $i = 0; |
||
| 897 | $j = 0; |
||
| 898 | $l = 0; |
||
| 899 | $nl = 1; |
||
| 900 | while ($i < $nb) { |
||
| 901 | // Get next character |
||
| 902 | $c = $s[$i]; |
||
| 903 | if ($c == "\n") { |
||
| 904 | // Explicit line break |
||
| 905 | $this->cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', false, $link); |
||
| 906 | $i++; |
||
| 907 | $sep = -1; |
||
| 908 | $j = $i; |
||
| 909 | $l = 0; |
||
| 910 | if ($nl == 1) { |
||
| 911 | $this->x = $this->lMargin; |
||
| 912 | $w = $this->w - $this->rMargin - $this->x; |
||
| 913 | $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->fontSize; |
||
| 914 | } |
||
| 915 | $nl++; |
||
| 916 | continue; |
||
| 917 | } |
||
| 918 | if ($c == ' ') { |
||
| 919 | $sep = $i; |
||
| 920 | } |
||
| 921 | $l += $cw[$c]; |
||
| 922 | if ($l > $wmax) { |
||
| 923 | // Automatic line break |
||
| 924 | if ($sep == -1) { |
||
| 925 | if ($this->x > $this->lMargin) { |
||
| 926 | // Move to next line |
||
| 927 | $this->x = $this->lMargin; |
||
| 928 | $this->y += $h; |
||
| 929 | $w = $this->w - $this->rMargin - $this->x; |
||
| 930 | $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->fontSize; |
||
| 931 | $i++; |
||
| 932 | $nl++; |
||
| 933 | continue; |
||
| 934 | } |
||
| 935 | if ($i == $j) { |
||
| 936 | $i++; |
||
| 937 | } |
||
| 938 | $this->cell($w, $h, substr($s, $j, $i - $j), 0, 2, '', false, $link); |
||
| 939 | } else { |
||
| 940 | $this->cell($w, $h, substr($s, $j, $sep - $j), 0, 2, '', false, $link); |
||
| 941 | $i = $sep + 1; |
||
| 942 | } |
||
| 943 | $sep = -1; |
||
| 944 | $j = $i; |
||
| 945 | $l = 0; |
||
| 946 | if ($nl == 1) { |
||
| 947 | $this->x = $this->lMargin; |
||
| 948 | $w = $this->w - $this->rMargin - $this->x; |
||
| 949 | $wmax = ($w - 2 * $this->cMargin) * 1000 / $this->fontSize; |
||
| 950 | } |
||
| 951 | $nl++; |
||
| 952 | } else { |
||
| 953 | $i++; |
||
| 954 | } |
||
| 955 | } |
||
| 956 | // Last chunk |
||
| 957 | if ($i != $j) { |
||
| 958 | $this->cell($l / 1000 * $this->fontSize, $h, substr($s, $j), 0, 0, '', false, $link); |
||
| 959 | } |
||
| 960 | } |
||
| 961 | |||
| 962 | public function ln($h = null) |
||
| 963 | { |
||
| 964 | // Line feed; default value is the last cell height |
||
| 965 | $this->x = $this->lMargin; |
||
| 966 | if ($h === null) { |
||
| 967 | $this->y += $this->lasth; |
||
| 968 | } else { |
||
| 969 | $this->y += $h; |
||
| 970 | } |
||
| 971 | } |
||
| 972 | |||
| 973 | public function image($file, $x = null, $y = null, $w = 0, $h = 0, $type = '', $link = '') |
||
| 974 | { |
||
| 975 | // Put an image on the page |
||
| 976 | if ($file == '') { |
||
| 977 | $this->error('Image file name is empty'); |
||
| 978 | } |
||
| 979 | if (!isset($this->images[$file])) { |
||
| 980 | // First use of this image, get info |
||
| 981 | if ($type == '') { |
||
| 982 | $pos = strrpos($file, '.'); |
||
| 983 | if (!$pos) { |
||
| 984 | $this->error( |
||
| 985 | 'Image file has no extension and no type was specified: ' |
||
| 986 | . $file |
||
| 987 | ); |
||
| 988 | } |
||
| 989 | $type = substr($file, $pos + 1); |
||
| 990 | } |
||
| 991 | $type = strtolower($type); |
||
| 992 | if ($type == 'jpeg') { |
||
| 993 | $type = 'jpg'; |
||
| 994 | } |
||
| 995 | $mtd = '_parse' . $type; |
||
| 996 | if (!method_exists($this, $mtd)) { |
||
| 997 | $this->error('Unsupported image type: ' . $type); |
||
| 998 | } |
||
| 999 | $info = $this->$mtd($file); |
||
| 1000 | $info['i'] = count($this->images) + 1; |
||
| 1001 | $this->images[$file] = $info; |
||
| 1002 | } else { |
||
| 1003 | $info = $this->images[$file]; |
||
| 1004 | } |
||
| 1005 | // Automatic width and height calculation if needed |
||
| 1006 | if ($w == 0 && $h == 0) { |
||
| 1007 | // Put image at 96 dpi |
||
| 1008 | $w = -96; |
||
| 1009 | $h = -96; |
||
| 1010 | } |
||
| 1011 | if ($w < 0) { |
||
| 1012 | $w = -$info['w'] * 72 / $w / $this->k; |
||
| 1013 | } |
||
| 1014 | if ($h < 0) { |
||
| 1015 | $h = -$info['h'] * 72 / $h / $this->k; |
||
| 1016 | } |
||
| 1017 | if ($w == 0) { |
||
| 1018 | $w = $h * $info['w'] / $info['h']; |
||
| 1019 | } |
||
| 1020 | if ($h == 0) { |
||
| 1021 | $h = $w * $info['h'] / $info['w']; |
||
| 1022 | } |
||
| 1023 | // Flowing mode |
||
| 1024 | if ($y === null) { |
||
| 1025 | if ($this->y + $h > $this->pageBreakTrigger |
||
| 1026 | && !$this->inHeader |
||
| 1027 | && !$this->infooter |
||
| 1028 | && $this->acceptPageBreak() |
||
| 1029 | ) { |
||
| 1030 | // Automatic page break |
||
| 1031 | $x2 = $this->x; |
||
| 1032 | $this->addPage( |
||
| 1033 | $this->curOrientation, |
||
| 1034 | $this->curPageSize, |
||
| 1035 | $this->curRotation |
||
| 1036 | ); |
||
| 1037 | $this->x = $x2; |
||
| 1038 | } |
||
| 1039 | $y = $this->y; |
||
| 1040 | $this->y += $h; |
||
| 1041 | } |
||
| 1042 | if ($x === null) { |
||
| 1043 | $x = $this->x; |
||
| 1044 | } |
||
| 1045 | $this->out( |
||
| 1046 | sprintf( |
||
| 1047 | 'q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q', |
||
| 1048 | $w * $this->k, |
||
| 1049 | $h * $this->k, |
||
| 1050 | $x * $this->k, |
||
| 1051 | ($this->h - ($y + $h)) * $this->k, |
||
| 1052 | $info['i'] |
||
| 1053 | ) |
||
| 1054 | ); |
||
| 1055 | if ($link) { |
||
| 1056 | $this->link($x, $y, $w, $h, $link); |
||
| 1057 | } |
||
| 1058 | } |
||
| 1059 | |||
| 1060 | public function getPageWidth() |
||
| 1061 | { |
||
| 1062 | // Get current page width |
||
| 1063 | return $this->w; |
||
| 1064 | } |
||
| 1065 | |||
| 1066 | public function getPageHeight() |
||
| 1067 | { |
||
| 1068 | // Get current page height |
||
| 1069 | return $this->h; |
||
| 1070 | } |
||
| 1071 | |||
| 1072 | public function getX() |
||
| 1073 | { |
||
| 1074 | // Get x position |
||
| 1075 | return $this->x; |
||
| 1076 | } |
||
| 1077 | |||
| 1078 | public function setX($x) |
||
| 1079 | { |
||
| 1080 | // Set x position |
||
| 1081 | if ($x >= 0) { |
||
| 1082 | $this->x = $x; |
||
| 1083 | } else { |
||
| 1084 | $this->x = $this->w + $x; |
||
| 1085 | } |
||
| 1086 | } |
||
| 1087 | |||
| 1088 | public function getY() |
||
| 1089 | { |
||
| 1090 | // Get y position |
||
| 1091 | return $this->y; |
||
| 1092 | } |
||
| 1093 | |||
| 1094 | public function setY($y, $resetX = true) |
||
| 1095 | { |
||
| 1096 | // Set y position and optionally reset x |
||
| 1097 | if ($y >= 0) { |
||
| 1098 | $this->y = $y; |
||
| 1099 | } else { |
||
| 1100 | $this->y = $this->h + $y; |
||
| 1101 | } |
||
| 1102 | if ($resetX) { |
||
| 1103 | $this->x = $this->lMargin; |
||
| 1104 | } |
||
| 1105 | } |
||
| 1106 | |||
| 1107 | public function setXY($x, $y) |
||
| 1108 | { |
||
| 1109 | // Set x and y positions |
||
| 1110 | $this->setX($x); |
||
| 1111 | $this->setY($y, false); |
||
| 1112 | } |
||
| 1113 | |||
| 1114 | public function output($dest = '', $name = '', $isUTF8 = false) |
||
| 1115 | { |
||
| 1116 | // output PDF to some destination |
||
| 1117 | $this->close(); |
||
| 1118 | if (strlen($name) == 1 && strlen($dest) != 1) { |
||
| 1119 | // Fix parameter order |
||
| 1120 | $tmp = $dest; |
||
| 1121 | $dest = $name; |
||
| 1122 | $name = $tmp; |
||
| 1123 | } |
||
| 1124 | if ($dest == '') { |
||
| 1125 | $dest = 'I'; |
||
| 1126 | } |
||
| 1127 | if ($name == '') { |
||
| 1128 | $name = 'doc.pdf'; |
||
| 1129 | } |
||
| 1130 | switch (strtoupper($dest)) { |
||
| 1131 | case 'I': |
||
| 1132 | // Send to standard output |
||
| 1133 | $this->checkOutput(); |
||
| 1134 | if (PHP_SAPI != 'cli') { |
||
| 1135 | // We send to a browser |
||
| 1136 | header('Content-Type: application/pdf'); |
||
| 1137 | header('Content-Disposition: inline; ' |
||
| 1138 | . $this->httpencode('filename', $name, $isUTF8)); |
||
| 1139 | header('Cache-Control: private, max-age=0, must-revalidate'); |
||
| 1140 | header('Pragma: public'); |
||
| 1141 | } |
||
| 1142 | echo $this->buffer; |
||
| 1143 | break; |
||
| 1144 | case 'D': |
||
| 1145 | // Download file |
||
| 1146 | $this->checkOutput(); |
||
| 1147 | header('Content-Type: application/x-download'); |
||
| 1148 | header('Content-Disposition: attachment; ' |
||
| 1149 | . $this->httpencode('filename', $name, $isUTF8)); |
||
| 1150 | header('Cache-Control: private, max-age=0, must-revalidate'); |
||
| 1151 | header('Pragma: public'); |
||
| 1152 | echo $this->buffer; |
||
| 1153 | break; |
||
| 1154 | case 'F': |
||
| 1155 | // Save to local file |
||
| 1156 | if (!fileput_contents($name, $this->buffer)) { |
||
| 1157 | $this->error('Unable to create output file: ' . $name); |
||
| 1158 | } |
||
| 1159 | break; |
||
| 1160 | case 'S': |
||
| 1161 | // Return as a string |
||
| 1162 | return $this->buffer; |
||
| 1163 | default: |
||
| 1164 | $this->error('Incorrect output destination: ' . $dest); |
||
| 1165 | } |
||
| 1166 | return ''; |
||
| 1167 | } |
||
| 1168 | |||
| 1169 | |||
| 1170 | protected function doChecks() |
||
| 1171 | { |
||
| 1172 | // Check mbstring overloading |
||
| 1173 | if (ini_get('mbstring.func_overload') & 2) { |
||
| 1174 | $this->error('mbstring overloading must be disabled'); |
||
| 1175 | } |
||
| 1176 | } |
||
| 1177 | |||
| 1178 | protected function checkOutput() |
||
| 1179 | { |
||
| 1180 | if (PHP_SAPI != 'cli') { |
||
| 1181 | if (headers_sent($file, $line)) { |
||
| 1182 | $this->error( |
||
| 1183 | "Some data has already been output, can't send PDF file " |
||
| 1184 | . "(output started at $file:$line)" |
||
| 1185 | ); |
||
| 1186 | } |
||
| 1187 | } |
||
| 1188 | if (ob_get_length()) { |
||
| 1189 | // The output buffer is not empty |
||
| 1190 | if (preg_match('/^(\xEF\xBB\xBF)?\s*$/', ob_get_contents())) { |
||
| 1191 | // It contains only a UTF-8 BOM and/or whitespace, let's clean it |
||
| 1192 | ob_clean(); |
||
| 1193 | } else { |
||
| 1194 | $this->error("Some data has already been output, can't send PDF file"); |
||
| 1195 | } |
||
| 1196 | } |
||
| 1197 | } |
||
| 1198 | |||
| 1199 | protected function getPageSize($size) |
||
| 1200 | { |
||
| 1201 | if (is_string($size)) { |
||
| 1202 | $size = strtolower($size); |
||
| 1203 | if (!isset($this->stdPageSizes[$size])) { |
||
| 1204 | $this->error('Unknown page size: ' . $size); |
||
| 1205 | } |
||
| 1206 | $a = $this->stdPageSizes[$size]; |
||
| 1207 | return [$a[0] / $this->k, $a[1] / $this->k]; |
||
| 1208 | } else { |
||
| 1209 | if ($size[0] > $size[1]) { |
||
| 1210 | return [$size[1], $size[0]]; |
||
| 1211 | } else { |
||
| 1212 | return $size; |
||
| 1213 | } |
||
| 1214 | } |
||
| 1215 | } |
||
| 1216 | |||
| 1217 | protected function beginPage($orientation, $size, $rotation) |
||
| 1218 | { |
||
| 1219 | $this->page++; |
||
| 1220 | $this->pages[$this->page] = ''; |
||
| 1221 | $this->state = 2; |
||
| 1222 | $this->x = $this->lMargin; |
||
| 1223 | $this->y = $this->tMargin; |
||
| 1224 | $this->fontFamily = ''; |
||
| 1225 | // Check page size and orientation |
||
| 1226 | if ($orientation == '') { |
||
| 1227 | $orientation = $this->defOrientation; |
||
| 1228 | } else { |
||
| 1229 | $orientation = strtoupper($orientation[0]); |
||
| 1230 | } |
||
| 1231 | if ($size == '') { |
||
| 1232 | $size = $this->defPageSize; |
||
| 1233 | } else { |
||
| 1234 | $size = $this->getPageSize($size); |
||
| 1235 | } |
||
| 1236 | if ($orientation != $this->curOrientation |
||
| 1237 | || $size[0] != $this->curPageSize[0] |
||
| 1238 | || $size[1] != $this->curPageSize[1] |
||
| 1239 | ) { |
||
| 1240 | // New size or orientation |
||
| 1241 | if ($orientation == 'P') { |
||
| 1242 | $this->w = $size[0]; |
||
| 1243 | $this->h = $size[1]; |
||
| 1244 | } else { |
||
| 1245 | $this->w = $size[1]; |
||
| 1246 | $this->h = $size[0]; |
||
| 1247 | } |
||
| 1248 | $this->wPt = $this->w * $this->k; |
||
| 1249 | $this->hPt = $this->h * $this->k; |
||
| 1250 | $this->pageBreakTrigger = $this->h - $this->bMargin; |
||
| 1251 | $this->curOrientation = $orientation; |
||
| 1252 | $this->curPageSize = $size; |
||
| 1253 | } |
||
| 1254 | if ($orientation != $this->defOrientation |
||
| 1255 | || $size[0] != $this->defPageSize[0] |
||
| 1256 | || $size[1] != $this->defPageSize[1] |
||
| 1257 | ) { |
||
| 1258 | $this->pageInfo[$this->page]['size'] = [$this->wPt, $this->hPt]; |
||
| 1259 | } |
||
| 1260 | if ($rotation != 0) { |
||
| 1261 | if ($rotation % 90 != 0) { |
||
| 1262 | $this->error('Incorrect rotation value: ' . $rotation); |
||
| 1263 | } |
||
| 1264 | $this->curRotation = $rotation; |
||
| 1265 | $this->pageInfo[$this->page]['rotation'] = $rotation; |
||
| 1266 | } |
||
| 1267 | } |
||
| 1268 | |||
| 1269 | protected function endPage() |
||
| 1270 | { |
||
| 1271 | $this->state = 1; |
||
| 1272 | } |
||
| 1273 | |||
| 1274 | protected function loadFont($font) |
||
| 1275 | { |
||
| 1276 | // Load a font definition file from the font directory |
||
| 1277 | if (strpos($font, '/') !== false || strpos($font, "\\") !== false) { |
||
| 1278 | $this->error('Incorrect font definition file name: ' . $font); |
||
| 1279 | } |
||
| 1280 | include($this->fontpath . $font); |
||
| 1281 | if (!isset($name)) { |
||
| 1282 | $this->error('Could not include font definition file'); |
||
| 1283 | } |
||
| 1284 | if (isset($enc)) { |
||
| 1285 | $enc = strtolower($enc); |
||
| 1286 | } |
||
| 1287 | if (!isset($subsetted)) { |
||
| 1288 | $subsetted = false; |
||
| 1289 | } |
||
| 1290 | return get_defined_vars(); |
||
| 1291 | } |
||
| 1292 | |||
| 1293 | protected function isAscii($s) |
||
| 1294 | { |
||
| 1295 | // Test if string is ASCII |
||
| 1296 | $nb = strlen($s); |
||
| 1297 | for ($i = 0; $i < $nb; $i++) { |
||
| 1298 | if (ord($s[$i]) > 127) { |
||
| 1299 | return false; |
||
| 1300 | } |
||
| 1301 | } |
||
| 1302 | return true; |
||
| 1303 | } |
||
| 1304 | |||
| 1305 | protected function httpencode($param, $value, $isUTF8) |
||
| 1306 | { |
||
| 1307 | // Encode HTTP header field parameter |
||
| 1308 | if ($this->isAscii($value)) { |
||
| 1309 | return $param . '="' . $value . '"'; |
||
| 1310 | } |
||
| 1311 | if (!$isUTF8) { |
||
| 1312 | $value = utf8_encode($value); |
||
| 1313 | } |
||
| 1314 | if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { |
||
| 1315 | return $param . '="' . rawurlencode($value) . '"'; |
||
| 1316 | } else { |
||
| 1317 | return $param . "*=UTF-8''" . rawurlencode($value); |
||
| 1318 | } |
||
| 1319 | } |
||
| 1320 | |||
| 1321 | protected function utf8ToUtf16($s) |
||
| 1322 | { |
||
| 1323 | // Convert UTF-8 to UTF-16BE with BOM |
||
| 1324 | $res = "\xFE\xFF"; |
||
| 1325 | $nb = strlen($s); |
||
| 1326 | $i = 0; |
||
| 1327 | while ($i < $nb) { |
||
| 1328 | $c1 = ord($s[$i++]); |
||
| 1329 | if ($c1 >= 224) { |
||
| 1330 | // 3-byte character |
||
| 1331 | $c2 = ord($s[$i++]); |
||
| 1332 | $c3 = ord($s[$i++]); |
||
| 1333 | $res .= chr((($c1 & 0x0F) << 4) + (($c2 & 0x3C) >> 2)); |
||
| 1334 | $res .= chr((($c2 & 0x03) << 6) + ($c3 & 0x3F)); |
||
| 1335 | } elseif ($c1 >= 192) { |
||
| 1336 | // 2-byte character |
||
| 1337 | $c2 = ord($s[$i++]); |
||
| 1338 | $res .= chr(($c1 & 0x1C) >> 2); |
||
| 1339 | $res .= chr((($c1 & 0x03) << 6) + ($c2 & 0x3F)); |
||
| 1340 | } else { |
||
| 1341 | // Single-byte character |
||
| 1342 | $res .= "\0" . chr($c1); |
||
| 1343 | } |
||
| 1344 | } |
||
| 1345 | return $res; |
||
| 1346 | } |
||
| 1347 | |||
| 1348 | protected function escape($s) |
||
| 1349 | { |
||
| 1350 | // Escape special characters |
||
| 1351 | if (strpos($s, '(') !== false |
||
| 1352 | || strpos($s, ')') !== false |
||
| 1353 | || strpos($s, '\\') !== false |
||
| 1354 | || strpos($s, "\r") !== false |
||
| 1355 | ) { |
||
| 1356 | return str_replace( |
||
| 1357 | ['\\', '(', ')', "\r"], |
||
| 1358 | ['\\\\', '\\(', '\\)', '\\r'], |
||
| 1359 | $s |
||
| 1360 | ); |
||
| 1361 | } else { |
||
| 1362 | return $s; |
||
| 1363 | } |
||
| 1364 | } |
||
| 1365 | |||
| 1366 | protected function textString($s) |
||
| 1367 | { |
||
| 1368 | // Format a text string |
||
| 1369 | if (!$this->isAscii($s)) { |
||
| 1370 | $s = $this->utf8ToUtf16($s); |
||
| 1371 | } |
||
| 1372 | return '(' . $this->escape($s) . ')'; |
||
| 1373 | } |
||
| 1374 | |||
| 1375 | protected function doUnderLine($x, $y, $txt) |
||
| 1376 | { |
||
| 1377 | // Underline text |
||
| 1378 | $up = $this->currentFont['up']; |
||
| 1379 | $ut = $this->currentFont['ut']; |
||
| 1380 | $w = $this->getStringWidth($txt) + $this->ws * substr_count($txt, ' '); |
||
| 1381 | return sprintf( |
||
| 1382 | '%.2F %.2F %.2F %.2F re f', |
||
| 1383 | $x * $this->k, |
||
| 1384 | ($this->h - ($y - $up / 1000 * $this->fontSize)) * $this->k, |
||
| 1385 | $w * $this->k, |
||
| 1386 | -$ut / 1000 * $this->fontSizePt |
||
| 1387 | ); |
||
| 1388 | } |
||
| 1389 | |||
| 1390 | protected function parseJpg($file) |
||
| 1391 | { |
||
| 1392 | // Extract info from a JPEG file |
||
| 1393 | $a = getimagesize($file); |
||
| 1394 | if (!$a) { |
||
| 1395 | $this->error('Missing or incorrect image file: ' . $file); |
||
| 1396 | } |
||
| 1397 | if ($a[2] != 2) { |
||
| 1398 | $this->error('Not a JPEG file: ' . $file); |
||
| 1399 | } |
||
| 1400 | if (!isset($a['channels']) || $a['channels'] == 3) { |
||
| 1401 | $colspace = 'DeviceRGB'; |
||
| 1402 | } elseif ($a['channels'] == 4) { |
||
| 1403 | $colspace = 'DeviceCMYK'; |
||
| 1404 | } else { |
||
| 1405 | $colspace = 'DeviceGray'; |
||
| 1406 | } |
||
| 1407 | $bpc = isset($a['bits']) ? $a['bits'] : 8; |
||
| 1408 | $data = file_get_contents($file); |
||
| 1409 | return [ |
||
| 1410 | 'w' => $a[0], |
||
| 1411 | 'h' => $a[1], |
||
| 1412 | 'cs' => $colspace, |
||
| 1413 | 'bpc' => $bpc, |
||
| 1414 | 'f' => 'DCTDecode', |
||
| 1415 | 'data' => $data |
||
| 1416 | ]; |
||
| 1417 | } |
||
| 1418 | |||
| 1419 | protected function parsePng($file) |
||
| 1420 | { |
||
| 1421 | // Extract info from a PNG file |
||
| 1422 | $f = fopen($file, 'rb'); |
||
| 1423 | if (!$f) { |
||
| 1424 | $this->error('Can\'t open image file: ' . $file); |
||
| 1425 | } |
||
| 1426 | $info = $this->parsePngstream($f, $file); |
||
| 1427 | fclose($f); |
||
| 1428 | return $info; |
||
| 1429 | } |
||
| 1430 | |||
| 1431 | protected function parsePngstream($f, $file) |
||
| 1432 | { |
||
| 1433 | // Check signature |
||
| 1434 | if ($this->readStream($f, 8) != chr(137) . 'PNG' |
||
| 1435 | . chr(13) . chr(10) . chr(26) . chr(10) |
||
| 1436 | ) { |
||
| 1437 | $this->error('Not a PNG file: ' . $file); |
||
| 1438 | } |
||
| 1439 | // Read header chunk |
||
| 1440 | $this->readStream($f, 4); |
||
| 1441 | if ($this->readStream($f, 4) != 'IHDR') { |
||
| 1442 | $this->error('Incorrect PNG file: ' . $file); |
||
| 1443 | } |
||
| 1444 | $w = $this->readInt($f); |
||
| 1445 | $h = $this->readInt($f); |
||
| 1446 | $bpc = ord($this->readStream($f, 1)); |
||
| 1447 | if ($bpc > 8) { |
||
| 1448 | $this->error('16-bit depth not supported: ' . $file); |
||
| 1449 | } |
||
| 1450 | $ct = ord($this->readStream($f, 1)); |
||
| 1451 | if ($ct == 0 || $ct == 4) { |
||
| 1452 | $colspace = 'DeviceGray'; |
||
| 1453 | } elseif ($ct == 2 || $ct == 6) { |
||
| 1454 | $colspace = 'DeviceRGB'; |
||
| 1455 | } elseif ($ct == 3) { |
||
| 1456 | $colspace = 'Indexed'; |
||
| 1457 | } else { |
||
| 1458 | $this->error('Unknown color type: ' . $file); |
||
| 1459 | } |
||
| 1460 | if (ord($this->readStream($f, 1)) != 0) { |
||
| 1461 | $this->error('Unknown compression method: ' . $file); |
||
| 1462 | } |
||
| 1463 | if (ord($this->readStream($f, 1)) != 0) { |
||
| 1464 | $this->error('Unknown filter method: ' . $file); |
||
| 1465 | } |
||
| 1466 | if (ord($this->readStream($f, 1)) != 0) { |
||
| 1467 | $this->error('Interlacing not supported: ' . $file); |
||
| 1468 | } |
||
| 1469 | $this->readStream($f, 4); |
||
| 1470 | $dp = '/Predictor 15 /Colors ' |
||
| 1471 | . ($colspace == 'DeviceRGB' ? 3 : 1) |
||
| 1472 | . ' /BitsPerComponent ' |
||
| 1473 | . $bpc |
||
| 1474 | . ' /Columns ' |
||
| 1475 | . $w; |
||
| 1476 | // Scan chunks looking for palette, transparency and image data |
||
| 1477 | $pal = ''; |
||
| 1478 | $trns = ''; |
||
| 1479 | $data = ''; |
||
| 1480 | do { |
||
| 1481 | $n = $this->readInt($f); |
||
| 1482 | $type = $this->readStream($f, 4); |
||
| 1483 | if ($type == 'PLTE') { |
||
| 1484 | // Read palette |
||
| 1485 | $pal = $this->readStream($f, $n); |
||
| 1486 | $this->readStream($f, 4); |
||
| 1487 | } elseif ($type == 'tRNS') { |
||
| 1488 | // Read transparency info |
||
| 1489 | $t = $this->readStream($f, $n); |
||
| 1490 | if ($ct == 0) { |
||
| 1491 | $trns = [ord(substr($t, 1, 1))]; |
||
| 1492 | } elseif ($ct == 2) { |
||
| 1493 | $trns = [ |
||
| 1494 | ord(substr($t, 1, 1)), |
||
| 1495 | ord(substr($t, 3, 1)), |
||
| 1496 | ord(substr($t, 5, 1)) |
||
| 1497 | ]; |
||
| 1498 | } else { |
||
| 1499 | $pos = strpos($t, chr(0)); |
||
| 1500 | if ($pos !== false) { |
||
| 1501 | $trns = [$pos]; |
||
| 1502 | } |
||
| 1503 | } |
||
| 1504 | $this->readStream($f, 4); |
||
| 1505 | } elseif ($type == 'IDAT') { |
||
| 1506 | // Read image data block |
||
| 1507 | $data .= $this->readStream($f, $n); |
||
| 1508 | $this->readStream($f, 4); |
||
| 1509 | } elseif ($type == 'IEND') { |
||
| 1510 | break; |
||
| 1511 | } else { |
||
| 1512 | $this->readStream($f, $n + 4); |
||
| 1513 | } |
||
| 1514 | } while ($n); |
||
| 1515 | if ($colspace == 'Indexed' && empty($pal)) { |
||
| 1516 | $this->error('Missing palette in ' . $file); |
||
| 1517 | } |
||
| 1518 | $info = [ |
||
| 1519 | 'w' => $w, |
||
| 1520 | 'h' => $h, |
||
| 1521 | 'cs' => $colspace, |
||
| 1522 | 'bpc' => $bpc, |
||
| 1523 | 'f' => 'FlateDecode', |
||
| 1524 | 'dp' => $dp, |
||
| 1525 | 'pal' => $pal, |
||
| 1526 | 'trns' => $trns |
||
| 1527 | ]; |
||
| 1528 | if ($ct >= 4) { |
||
| 1529 | // Extract alpha channel |
||
| 1530 | if (!function_exists('gzuncompress')) { |
||
| 1531 | $this->error('Zlib not available, can\'t handle alpha channel: ' |
||
| 1532 | . $file); |
||
| 1533 | } |
||
| 1534 | $data = gzuncompress($data); |
||
| 1535 | $color = ''; |
||
| 1536 | $alpha = ''; |
||
| 1537 | if ($ct == 4) { |
||
| 1538 | // Gray image |
||
| 1539 | $len = 2 * $w; |
||
| 1540 | for ($i = 0; $i < $h; $i++) { |
||
| 1541 | $pos = (1 + $len) * $i; |
||
| 1542 | $color .= $data[$pos]; |
||
| 1543 | $alpha .= $data[$pos]; |
||
| 1544 | $line = substr($data, $pos + 1, $len); |
||
| 1545 | $color .= preg_replace('/(.)./s', '$1', $line); |
||
| 1546 | $alpha .= preg_replace('/.(.)/s', '$1', $line); |
||
| 1547 | } |
||
| 1548 | } else { |
||
| 1549 | // RGB image |
||
| 1550 | $len = 4 * $w; |
||
| 1551 | for ($i = 0; $i < $h; $i++) { |
||
| 1552 | $pos = (1 + $len) * $i; |
||
| 1553 | $color .= $data[$pos]; |
||
| 1554 | $alpha .= $data[$pos]; |
||
| 1555 | $line = substr($data, $pos + 1, $len); |
||
| 1556 | $color .= preg_replace('/(.{3})./s', '$1', $line); |
||
| 1557 | $alpha .= preg_replace('/.{3}(.)/s', '$1', $line); |
||
| 1558 | } |
||
| 1559 | } |
||
| 1560 | unset($data); |
||
| 1561 | $data = gzcompress($color); |
||
| 1562 | $info['smask'] = gzcompress($alpha); |
||
| 1563 | $this->withAlpha = true; |
||
| 1564 | if ($this->pdfVersion < '1.4') { |
||
| 1565 | $this->pdfVersion = '1.4'; |
||
| 1566 | } |
||
| 1567 | } |
||
| 1568 | $info['data'] = $data; |
||
| 1569 | return $info; |
||
| 1570 | } |
||
| 1571 | |||
| 1572 | protected function readStream($f, $n) |
||
| 1573 | { |
||
| 1574 | // Read n bytes from stream |
||
| 1575 | $res = ''; |
||
| 1576 | while ($n > 0 && !feof($f)) { |
||
| 1577 | $s = fread($f, $n); |
||
| 1578 | if ($s === false) { |
||
| 1579 | $this->error('Error while reading stream'); |
||
| 1580 | } |
||
| 1581 | $n -= strlen($s); |
||
| 1582 | $res .= $s; |
||
| 1583 | } |
||
| 1584 | if ($n > 0) { |
||
| 1585 | $this->error('Unexpected end of stream'); |
||
| 1586 | } |
||
| 1587 | return $res; |
||
| 1588 | } |
||
| 1589 | |||
| 1590 | protected function readInt($f) |
||
| 1591 | { |
||
| 1592 | // Read a 4-byte integer from stream |
||
| 1593 | $a = unpack('Ni', $this->readStream($f, 4)); |
||
| 1594 | return $a['i']; |
||
| 1595 | } |
||
| 1596 | |||
| 1597 | protected function parseGif($file) |
||
| 1598 | { |
||
| 1599 | // Extract info from a GIF file (via PNG conversion) |
||
| 1600 | if (!function_exists('imagepng')) { |
||
| 1601 | $this->error('GD extension is required for GIF support'); |
||
| 1602 | } |
||
| 1603 | if (!function_exists('imagecreatefromgif')) { |
||
| 1604 | $this->error('GD has no GIF read support'); |
||
| 1605 | } |
||
| 1606 | $im = imagecreatefromgif($file); |
||
| 1607 | if (!$im) { |
||
| 1608 | $this->error('Missing or incorrect image file: ' . $file); |
||
| 1609 | } |
||
| 1610 | imageinterlace($im, 0); |
||
| 1611 | ob_start(); |
||
| 1612 | imagepng($im); |
||
| 1613 | $data = ob_get_clean(); |
||
| 1614 | imagedestroy($im); |
||
| 1615 | $f = fopen('php://temp', 'rb+'); |
||
| 1616 | if (!$f) { |
||
| 1617 | $this->error('Unable to create memory stream'); |
||
| 1618 | } |
||
| 1619 | fwrite($f, $data); |
||
| 1620 | rewind($f); |
||
| 1621 | $info = $this->parsePngstream($f, $file); |
||
| 1622 | fclose($f); |
||
| 1623 | return $info; |
||
| 1624 | } |
||
| 1625 | |||
| 1626 | protected function out($s) |
||
| 1627 | { |
||
| 1628 | // Add a line to the document |
||
| 1629 | if ($this->state == 2) { |
||
| 1630 | $this->pages[$this->page] .= $s . "\n"; |
||
| 1631 | } elseif ($this->state == 1) { |
||
| 1632 | $this->put($s); |
||
| 1633 | } elseif ($this->state == 0) { |
||
| 1634 | $this->error('No page has been added yet'); |
||
| 1635 | } elseif ($this->state == 3) { |
||
| 1636 | $this->error('The document is closed'); |
||
| 1637 | } |
||
| 1638 | } |
||
| 1639 | |||
| 1640 | protected function put($s) |
||
| 1641 | { |
||
| 1642 | $this->buffer .= $s . "\n"; |
||
| 1643 | } |
||
| 1644 | |||
| 1645 | protected function getOffset() |
||
| 1646 | { |
||
| 1647 | return strlen($this->buffer); |
||
| 1648 | } |
||
| 1649 | |||
| 1650 | protected function newObj($n = null) |
||
| 1651 | { |
||
| 1652 | // Begin a new object |
||
| 1653 | if ($n === null) { |
||
| 1654 | $n = ++$this->n; |
||
| 1655 | } |
||
| 1656 | $this->offsets[$n] = $this->getOffset(); |
||
| 1657 | $this->put($n . ' 0 obj'); |
||
| 1658 | } |
||
| 1659 | |||
| 1660 | protected function putStream($data) |
||
| 1661 | { |
||
| 1662 | $this->put('stream'); |
||
| 1663 | $this->put($data); |
||
| 1664 | $this->put('endstream'); |
||
| 1665 | } |
||
| 1666 | |||
| 1667 | protected function putStreamobject($data) |
||
| 1668 | { |
||
| 1669 | if ($this->compress) { |
||
| 1670 | $entries = '/Filter /FlateDecode '; |
||
| 1671 | $data = gzcompress($data); |
||
| 1672 | } else { |
||
| 1673 | $entries = ''; |
||
| 1674 | } |
||
| 1675 | $entries .= '/Length ' . strlen($data); |
||
| 1676 | $this->newObj(); |
||
| 1677 | $this->put('<<' . $entries . '>>'); |
||
| 1678 | $this->putStream($data); |
||
| 1679 | $this->put('endobj'); |
||
| 1680 | } |
||
| 1681 | |||
| 1682 | protected function putpage($n) |
||
| 1683 | { |
||
| 1684 | $this->newObj(); |
||
| 1685 | $this->put('<</Type /Page'); |
||
| 1686 | $this->put('/Parent 1 0 R'); |
||
| 1687 | if (isset($this->pageInfo[$n]['size'])) { |
||
| 1688 | $this->put( |
||
| 1689 | sprintf( |
||
| 1690 | '/MediaBox [0 0 %.2F %.2F]', |
||
| 1691 | $this->pageInfo[$n]['size'][0], |
||
| 1692 | $this->pageInfo[$n]['size'][1] |
||
| 1693 | ) |
||
| 1694 | ); |
||
| 1695 | } |
||
| 1696 | if (isset($this->pageInfo[$n]['rotation'])) { |
||
| 1697 | $this->put('/Rotate ' . $this->pageInfo[$n]['rotation']); |
||
| 1698 | } |
||
| 1699 | $this->put('/Resources 2 0 R'); |
||
| 1700 | if (isset($this->pageLinks[$n])) { |
||
| 1701 | // Links |
||
| 1702 | $annots = '/Annots ['; |
||
| 1703 | foreach ($this->pageLinks[$n] as $pl) { |
||
| 1704 | $rect = sprintf( |
||
| 1705 | '%.2F %.2F %.2F %.2F', |
||
| 1706 | $pl[0], |
||
| 1707 | $pl[1], |
||
| 1708 | $pl[0] + $pl[2], |
||
| 1709 | $pl[1] - $pl[3] |
||
| 1710 | ); |
||
| 1711 | $annots .= '<</Type /Annot /Subtype /Link /Rect [' |
||
| 1712 | . $rect . '] /Border [0 0 0] '; |
||
| 1713 | if (is_string($pl[4])) { |
||
| 1714 | $annots .= '/A <</S /URI /URI ' |
||
| 1715 | . $this->textString($pl[4]) . '>>>>'; |
||
| 1716 | } else { |
||
| 1717 | $l = $this->links[$pl[4]]; |
||
| 1718 | if (isset($this->pageInfo[$l[0]]['size'])) { |
||
| 1719 | $h = $this->pageInfo[$l[0]]['size'][1]; |
||
| 1720 | } else { |
||
| 1721 | $h = ($this->defOrientation == 'P') |
||
| 1722 | ? $this->defPageSize[1] * $this->k |
||
| 1723 | : $this->defPageSize[0] * $this->k; |
||
| 1724 | } |
||
| 1725 | $annots .= sprintf( |
||
| 1726 | '/Dest [%d 0 R /XYZ 0 %.2F null]>>', |
||
| 1727 | $this->pageInfo[$l[0]]['n'], |
||
| 1728 | $h - $l[1] * $this->k |
||
| 1729 | ); |
||
| 1730 | } |
||
| 1731 | } |
||
| 1732 | $this->put($annots . ']'); |
||
| 1733 | } |
||
| 1734 | if ($this->withAlpha) { |
||
| 1735 | $this->put('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>'); |
||
| 1736 | } |
||
| 1737 | $this->put('/Contents ' . ($this->n + 1) . ' 0 R>>'); |
||
| 1738 | $this->put('endobj'); |
||
| 1739 | // Page content |
||
| 1740 | if (!empty($this->aliasNbPages)) { |
||
| 1741 | $this->pages[$n] = str_replace( |
||
| 1742 | $this->aliasNbPages, |
||
| 1743 | $this->page, |
||
| 1744 | $this->pages[$n] |
||
| 1745 | ); |
||
| 1746 | } |
||
| 1747 | $this->putStreamobject($this->pages[$n]); |
||
| 1748 | } |
||
| 1749 | |||
| 1750 | protected function putPages() |
||
| 1751 | { |
||
| 1752 | $nb = $this->page; |
||
| 1753 | for ($n = 1; $n <= $nb; $n++) { |
||
| 1754 | $this->pageInfo[$n]['n'] = $this->n + 1 + 2 * ($n - 1); |
||
| 1755 | } |
||
| 1756 | for ($n = 1; $n <= $nb; $n++) { |
||
| 1757 | $this->putpage($n); |
||
| 1758 | } |
||
| 1759 | // Pages root |
||
| 1760 | $this->newObj(1); |
||
| 1761 | $this->put('<</Type /Pages'); |
||
| 1762 | $kids = '/Kids ['; |
||
| 1763 | for ($n = 1; $n <= $nb; $n++) { |
||
| 1764 | $kids .= $this->pageInfo[$n]['n'] . ' 0 R '; |
||
| 1765 | } |
||
| 1766 | $this->put($kids . ']'); |
||
| 1767 | $this->put('/Count ' . $nb); |
||
| 1768 | if ($this->defOrientation == 'P') { |
||
| 1769 | $w = $this->defPageSize[0]; |
||
| 1770 | $h = $this->defPageSize[1]; |
||
| 1771 | } else { |
||
| 1772 | $w = $this->defPageSize[1]; |
||
| 1773 | $h = $this->defPageSize[0]; |
||
| 1774 | } |
||
| 1775 | $this->put( |
||
| 1776 | sprintf( |
||
| 1777 | '/MediaBox [0 0 %.2F %.2F]', |
||
| 1778 | $w * $this->k, |
||
| 1779 | $h * $this->k |
||
| 1780 | ) |
||
| 1781 | ); |
||
| 1782 | $this->put('>>'); |
||
| 1783 | $this->put('endobj'); |
||
| 1784 | } |
||
| 1785 | |||
| 1786 | protected function putFonts() |
||
| 1787 | { |
||
| 1788 | foreach ($this->fontFiles as $file => $info) { |
||
| 1789 | // Font file embedding |
||
| 1790 | $this->newObj(); |
||
| 1791 | $this->fontFiles[$file]['n'] = $this->n; |
||
| 1792 | $font = file_get_contents($this->fontpath . $file, true); |
||
| 1793 | if (!$font) { |
||
| 1794 | $this->error('Font file not found: ' . $file); |
||
| 1795 | } |
||
| 1796 | $compressed = (substr($file, -2) == '.z'); |
||
| 1797 | if (!$compressed && isset($info['length2'])) { |
||
| 1798 | $font = substr($font, 6, $info['length1']) |
||
| 1799 | . substr($font, 6 + $info['length1'] + 6, $info['length2']); |
||
| 1800 | } |
||
| 1801 | $this->put('<</Length ' . strlen($font)); |
||
| 1802 | if ($compressed) { |
||
| 1803 | $this->put('/Filter /FlateDecode'); |
||
| 1804 | } |
||
| 1805 | $this->put('/Length1 ' . $info['length1']); |
||
| 1806 | if (isset($info['length2'])) { |
||
| 1807 | $this->put('/Length2 ' . $info['length2'] . ' /Length3 0'); |
||
| 1808 | } |
||
| 1809 | $this->put('>>'); |
||
| 1810 | $this->putStream($font); |
||
| 1811 | $this->put('endobj'); |
||
| 1812 | } |
||
| 1813 | foreach ($this->fonts as $k => $font) { |
||
| 1814 | // Encoding |
||
| 1815 | if (isset($font['diff'])) { |
||
| 1816 | if (!isset($this->encodings[$font['enc']])) { |
||
| 1817 | $this->newObj(); |
||
| 1818 | $this->put('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences [' |
||
| 1819 | . $font['diff'] . ']>>'); |
||
| 1820 | $this->put('endobj'); |
||
| 1821 | $this->encodings[$font['enc']] = $this->n; |
||
| 1822 | } |
||
| 1823 | } |
||
| 1824 | // ToUnicode CMap |
||
| 1825 | if (isset($font['uv'])) { |
||
| 1826 | if (isset($font['enc'])) { |
||
| 1827 | $cmapkey = $font['enc']; |
||
| 1828 | } else { |
||
| 1829 | $cmapkey = $font['name']; |
||
| 1830 | } |
||
| 1831 | if (!isset($this->cmaps[$cmapkey])) { |
||
| 1832 | $cmap = $this->toUnicodeCmap($font['uv']); |
||
| 1833 | $this->putStreamobject($cmap); |
||
| 1834 | $this->cmaps[$cmapkey] = $this->n; |
||
| 1835 | } |
||
| 1836 | } |
||
| 1837 | // Font object |
||
| 1838 | $this->fonts[$k]['n'] = $this->n + 1; |
||
| 1839 | $type = $font['type']; |
||
| 1840 | $name = $font['name']; |
||
| 1841 | if ($font['subsetted']) { |
||
| 1842 | $name = 'AAAAAA+' . $name; |
||
| 1843 | } |
||
| 1844 | if ($type == 'Core') { |
||
| 1845 | // Core font |
||
| 1846 | $this->newObj(); |
||
| 1847 | $this->put('<</Type /Font'); |
||
| 1848 | $this->put('/BaseFont /' . $name); |
||
| 1849 | $this->put('/Subtype /Type1'); |
||
| 1850 | if ($name != 'Symbol' && $name != 'ZapfDingbats') { |
||
| 1851 | $this->put('/Encoding /WinAnsiEncoding'); |
||
| 1852 | } |
||
| 1853 | if (isset($font['uv'])) { |
||
| 1854 | $this->put('/ToUnicode ' . $this->cmaps[$cmapkey] . ' 0 R'); |
||
| 1855 | } |
||
| 1856 | $this->put('>>'); |
||
| 1857 | $this->put('endobj'); |
||
| 1858 | } elseif ($type == 'Type1' || $type == 'TrueType') { |
||
| 1859 | // Additional Type1 or TrueType/OpenType font |
||
| 1860 | $this->newObj(); |
||
| 1861 | $this->put('<</Type /Font'); |
||
| 1862 | $this->put('/BaseFont /' . $name); |
||
| 1863 | $this->put('/Subtype /' . $type); |
||
| 1864 | $this->put('/FirstChar 32 /LastChar 255'); |
||
| 1865 | $this->put('/Widths ' . ($this->n + 1) . ' 0 R'); |
||
| 1866 | $this->put('/FontDescriptor ' . ($this->n + 2) . ' 0 R'); |
||
| 1867 | if (isset($font['diff'])) { |
||
| 1868 | $this->put('/Encoding ' . $this->encodings[$font['enc']] . ' 0 R'); |
||
| 1869 | } else { |
||
| 1870 | $this->put('/Encoding /WinAnsiEncoding'); |
||
| 1871 | } |
||
| 1872 | if (isset($font['uv'])) { |
||
| 1873 | $this->put('/ToUnicode ' . $this->cmaps[$cmapkey] . ' 0 R'); |
||
| 1874 | } |
||
| 1875 | $this->put('>>'); |
||
| 1876 | $this->put('endobj'); |
||
| 1877 | // Widths |
||
| 1878 | $this->newObj(); |
||
| 1879 | $cw = &$font['cw']; |
||
| 1880 | $s = '['; |
||
| 1881 | for ($i = 32; $i <= 255; $i++) { |
||
| 1882 | $s .= $cw[chr($i)] . ' '; |
||
| 1883 | } |
||
| 1884 | $this->put($s . ']'); |
||
| 1885 | $this->put('endobj'); |
||
| 1886 | // Descriptor |
||
| 1887 | $this->newObj(); |
||
| 1888 | $s = '<</Type /FontDescriptor /FontName /' . $name; |
||
| 1889 | foreach ($font['desc'] as $k => $v) { |
||
| 1890 | $s .= ' /' . $k . ' ' . $v; |
||
| 1891 | } |
||
| 1892 | if (!empty($font['file'])) { |
||
| 1893 | $s .= ' /FontFile' . ($type == 'Type1' ? '' : '2') . ' ' |
||
| 1894 | . $this->fontFiles[$font['file']]['n'] . ' 0 R'; |
||
| 1895 | } |
||
| 1896 | $this->put($s . '>>'); |
||
| 1897 | $this->put('endobj'); |
||
| 1898 | } else { |
||
| 1899 | // Allow for additional types |
||
| 1900 | $mtd = 'put' . strtolower($type); |
||
| 1901 | if (!method_exists($this, $mtd)) { |
||
| 1902 | $this->error('Unsupported font type: ' . $type); |
||
| 1903 | } |
||
| 1904 | $this->$mtd($font); |
||
| 1905 | } |
||
| 1906 | } |
||
| 1907 | } |
||
| 1908 | |||
| 1909 | protected function toUnicodeCmap($uv) |
||
| 1910 | { |
||
| 1911 | $ranges = ''; |
||
| 1912 | $nbr = 0; |
||
| 1913 | $chars = ''; |
||
| 1914 | $nbc = 0; |
||
| 1915 | foreach ($uv as $c => $v) { |
||
| 1916 | if (is_array($v)) { |
||
| 1917 | $ranges .= sprintf( |
||
| 1918 | "<%02X> <%02X> <%04X>\n", |
||
| 1919 | $c, |
||
| 1920 | $c + $v[1] - 1, |
||
| 1921 | $v[0] |
||
| 1922 | ); |
||
| 1923 | $nbr++; |
||
| 1924 | } else { |
||
| 1925 | $chars .= sprintf("<%02X> <%04X>\n", $c, $v); |
||
| 1926 | $nbc++; |
||
| 2138 |
This check looks for classes that have been defined more than once.
If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.
This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.