noalyss Version-9
tfpdf.php
Go to the documentation of this file.
1<?php
2/*******************************************************************************
3* tFPDF (based on FPDF 1.85) *
4* *
5* Version: 1.33 *
6* Date: 2022-12-20 *
7* Authors: Ian Back <ianb@bpm1.com> *
8* Tycho Veltmeijer <tfpdf@tychoveltmeijer.nl> (versions 1.30+) *
9* License: LGPL *
10*******************************************************************************/
11
12class tFPDF
13{
14const VERSION = '1.33';
15protected $unifontSubset;
16protected $page; // current page number
17protected $n; // current object number
18protected $offsets; // array of object offsets
19protected $buffer; // buffer holding in-memory PDF
20protected $pages; // array containing pages
21protected $state; // current document state
22protected $compress; // compression flag
23protected $k; // scale factor (number of points in user unit)
24protected $DefOrientation; // default orientation
25protected $CurOrientation; // current orientation
26protected $StdPageSizes; // standard page sizes
27protected $DefPageSize; // default page size
28protected $CurPageSize; // current page size
29protected $CurRotation; // current page rotation
30protected $PageInfo; // page-related data
31protected $wPt, $hPt; // dimensions of current page in points
32protected $w, $h; // dimensions of current page in user unit
33protected $lMargin; // left margin
34protected $tMargin; // top margin
35protected $rMargin; // right margin
36protected $bMargin; // page break margin
37protected $cMargin; // cell margin
38protected $x, $y; // current position in user unit
39protected $lasth; // height of last printed cell
40protected $LineWidth; // line width in user unit
41protected $fontpath; // path containing fonts
42protected $CoreFonts; // array of core font names
43protected $fonts; // array of used fonts
44protected $FontFiles; // array of font files
45protected $encodings; // array of encodings
46protected $cmaps; // array of ToUnicode CMaps
47protected $FontFamily; // current font family
48protected $FontStyle; // current font style
49protected $underline; // underlining flag
50protected $CurrentFont; // current font info
51protected $FontSizePt; // current font size in points
52protected $FontSize; // current font size in user unit
53protected $DrawColor; // commands for drawing color
54protected $FillColor; // commands for filling color
55protected $TextColor; // commands for text color
56protected $ColorFlag; // indicates whether fill and text colors are different
57protected $WithAlpha; // indicates whether alpha channel is used
58protected $ws; // word spacing
59protected $images; // array of used images
60protected $PageLinks; // array of links in pages
61protected $links; // array of internal links
62protected $AutoPageBreak; // automatic page breaking
63protected $PageBreakTrigger; // threshold used to trigger page breaks
64protected $InHeader; // flag set when processing header
65protected $InFooter; // flag set when processing footer
66protected $AliasNbPages; // alias for total number of pages
67protected $ZoomMode; // zoom display mode
68protected $LayoutMode; // layout display mode
69protected $metadata; // document properties
70protected $CreationDate; // document creation date
71protected $PDFVersion; // PDF version number
72
73/*******************************************************************************
74* Public methods *
75*******************************************************************************/
76
77function __construct($orientation='P', $unit='mm', $size='A4')
78{
79 // Some checks
80 $this->_dochecks();
81 // Initialization of properties
82 $this->state = 0;
83 $this->page = 0;
84 $this->n = 2;
85 $this->buffer = '';
86 $this->pages = array();
87 $this->PageInfo = array();
88 $this->fonts = array();
89 $this->FontFiles = array();
90 $this->encodings = array();
91 $this->cmaps = array();
92 $this->images = array();
93 $this->links = array();
94 $this->InHeader = false;
95 $this->InFooter = false;
96 $this->lasth = 0;
97 $this->FontFamily = '';
98 $this->FontStyle = '';
99 $this->FontSizePt = 12;
100 $this->underline = false;
101 $this->DrawColor = '0 G';
102 $this->FillColor = '0 g';
103 $this->TextColor = '0 g';
104 $this->ColorFlag = false;
105 $this->WithAlpha = false;
106 $this->ws = 0;
107 // Font path
108 if(defined('FPDF_FONTPATH'))
109 {
110 $this->fontpath = FPDF_FONTPATH;
111 if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\')
112 $this->fontpath .= '/';
113 }
114 elseif(is_dir(dirname(__FILE__).'/font'))
115 $this->fontpath = dirname(__FILE__).'/font/';
116 else
117 $this->fontpath = '';
118 // Core fonts
119 $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats');
120 // Scale factor
121 if($unit=='pt')
122 $this->k = 1;
123 elseif($unit=='mm')
124 $this->k = 72/25.4;
125 elseif($unit=='cm')
126 $this->k = 72/2.54;
127 elseif($unit=='in')
128 $this->k = 72;
129 else
130 $this->Error('Incorrect unit: '.$unit);
131 // Page sizes
132 $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
133 'letter'=>array(612,792), 'legal'=>array(612,1008));
134 $size = $this->_getpagesize($size);
135 $this->DefPageSize = $size;
136 $this->CurPageSize = $size;
137 // Page orientation
138 $orientation = strtolower($orientation);
139 if($orientation=='p' || $orientation=='portrait')
140 {
141 $this->DefOrientation = 'P';
142 $this->w = $size[0];
143 $this->h = $size[1];
144 }
145 elseif($orientation=='l' || $orientation=='landscape')
146 {
147 $this->DefOrientation = 'L';
148 $this->w = $size[1];
149 $this->h = $size[0];
150 }
151 else
152 $this->Error('Incorrect orientation: '.$orientation);
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 // Metadata
172 $this->metadata = array('Producer'=>'tFPDF '.self::VERSION);
173 // Set default PDF version number
174 $this->PDFVersion = '1.3';
175}
176
177function SetMargins($left, $top, $right=null)
178{
179 // Set left, top and right margins
180 $this->lMargin = $left;
181 $this->tMargin = $top;
182 if($right===null)
183 $right = $left;
184 $this->rMargin = $right;
185}
186
187function SetLeftMargin($margin)
188{
189 // Set left margin
190 $this->lMargin = $margin;
191 if($this->page>0 && $this->x<$margin)
192 $this->x = $margin;
193}
194
195function SetTopMargin($margin)
196{
197 // Set top margin
198 $this->tMargin = $margin;
199}
200
201function SetRightMargin($margin)
202{
203 // Set right margin
204 $this->rMargin = $margin;
205}
206
207function 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
215function SetDisplayMode($zoom, $layout='default')
216{
217 // Set display mode in viewer
218 if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
219 $this->ZoomMode = $zoom;
220 else
221 $this->Error('Incorrect zoom display mode: '.$zoom);
222 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
223 $this->LayoutMode = $layout;
224 else
225 $this->Error('Incorrect layout display mode: '.$layout);
226}
227
228function SetCompression($compress)
229{
230 // Set page compression
231 if(function_exists('gzcompress'))
232 $this->compress = $compress;
233 else
234 $this->compress = false;
235}
236
237function SetTitle($title, $isUTF8=false)
238{
239 // Title of document
240 $this->metadata['Title'] = $isUTF8 ? $title : $this->_UTF8encode($title);
241}
242
243function SetAuthor($author, $isUTF8=false)
244{
245 // Author of document
246 $this->metadata['Author'] = $isUTF8 ? $author : $this->_UTF8encode($author);
247}
248
249function SetSubject($subject, $isUTF8=false)
250{
251 // Subject of document
252 $this->metadata['Subject'] = $isUTF8 ? $subject : $this->_UTF8encode($subject);
253}
254
255function SetKeywords($keywords, $isUTF8=false)
256{
257 // Keywords of document
258 $this->metadata['Keywords'] = $isUTF8 ? $keywords : $this->_UTF8encode($keywords);
259}
260
261function SetCreator($creator, $isUTF8=false)
262{
263 // Creator of document
264 $this->metadata['Creator'] = $isUTF8 ? $creator : $this->_UTF8encode($creator);
265}
266
267function AliasNbPages($alias='{nb}')
268{
269 // Define an alias for total number of pages
270 $this->AliasNbPages = $alias;
271}
272
273function Error($msg)
274{
275 // Fatal error
276 throw new Exception('tFPDF error: '.$msg);
277}
278
279function Close()
280{
281 // Terminate document
282 if($this->state==3)
283 return;
284 if($this->page==0)
285 $this->AddPage();
286 // Page footer
287 $this->InFooter = true;
288 $this->Footer();
289 $this->InFooter = false;
290 // Close page
291 $this->_endpage();
292 // Close document
293 $this->_enddoc();
294}
295
296function AddPage($orientation='', $size='', $rotation=0)
297{
298 // Start a new page
299 if($this->state==3)
300 $this->Error('The document is closed');
301 $family = $this->FontFamily;
302 $style = $this->FontStyle.($this->underline ? 'U' : '');
303 $fontsize = $this->FontSizePt;
304 $lw = $this->LineWidth;
305 $dc = $this->DrawColor;
306 $fc = $this->FillColor;
307 $tc = $this->TextColor;
308 $cf = $this->ColorFlag;
309 if($this->page>0)
310 {
311 // Page footer
312 $this->InFooter = true;
313 $this->Footer();
314 $this->InFooter = false;
315 // Close page
316 $this->_endpage();
317 }
318 // Start new page
319 $this->_beginpage($orientation,$size,$rotation);
320 // Set line cap style to square
321 $this->_out('2 J');
322 // Set line width
323 $this->LineWidth = $lw;
324 $this->_out(sprintf('%.2F w',$lw*$this->k));
325 // Set font
326 if($family)
327 $this->SetFont($family,$style,$fontsize);
328 // Set colors
329 $this->DrawColor = $dc;
330 if($dc!='0 G')
331 $this->_out($dc);
332 $this->FillColor = $fc;
333 if($fc!='0 g')
334 $this->_out($fc);
335 $this->TextColor = $tc;
336 $this->ColorFlag = $cf;
337 // Page header
338 $this->InHeader = true;
339 $this->Header();
340 $this->InHeader = false;
341 // Restore line width
342 if($this->LineWidth!=$lw)
343 {
344 $this->LineWidth = $lw;
345 $this->_out(sprintf('%.2F w',$lw*$this->k));
346 }
347 // Restore font
348 if($family)
349 $this->SetFont($family,$style,$fontsize);
350 // Restore colors
351 if($this->DrawColor!=$dc)
352 {
353 $this->DrawColor = $dc;
354 $this->_out($dc);
355 }
356 if($this->FillColor!=$fc)
357 {
358 $this->FillColor = $fc;
359 $this->_out($fc);
360 }
361 $this->TextColor = $tc;
362 $this->ColorFlag = $cf;
363}
364
365function Header()
366{
367 // To be implemented in your own inherited class
368}
369
370function Footer()
371{
372 // To be implemented in your own inherited class
373}
374
375function PageNo()
376{
377 // Get current page number
378 return $this->page;
379}
380
381function SetDrawColor($r, $g=null, $b=null)
382{
383 // Set color for all stroking operations
384 if(($r==0 && $g==0 && $b==0) || $g===null)
385 $this->DrawColor = sprintf('%.3F G',$r/255);
386 else
387 $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255);
388 if($this->page>0)
389 $this->_out($this->DrawColor);
390}
391
392function SetFillColor($r, $g=null, $b=null)
393{
394 // Set color for all filling operations
395 if(($r==0 && $g==0 && $b==0) || $g===null)
396 $this->FillColor = sprintf('%.3F g',$r/255);
397 else
398 $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
399 $this->ColorFlag = ($this->FillColor!=$this->TextColor);
400 if($this->page>0)
401 $this->_out($this->FillColor);
402}
403
404function SetTextColor($r, $g=null, $b=null)
405{
406 // Set color for text
407 if(($r==0 && $g==0 && $b==0) || $g===null)
408 $this->TextColor = sprintf('%.3F g',$r/255);
409 else
410 $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
411 $this->ColorFlag = ($this->FillColor!=$this->TextColor);
412}
413
414function GetStringWidth($s)
415{
416 // Get width of a string in the current font
417 $s = (string)$s;
418 $cw = $this->CurrentFont['cw'];
419 $w=0;
420 if ($this->unifontSubset) {
421 $unicode = $this->UTF8StringToArray($s);
422 foreach($unicode as $char) {
423 if (isset($cw[2*$char])) { $w += (ord($cw[2*$char])<<8) + ord($cw[2*$char+1]); }
424 else if($char>0 && $char<128 && isset($cw[chr($char)])) { $w += $cw[chr($char)]; }
425 else if(isset($this->CurrentFont['desc']['MissingWidth'])) { $w += $this->CurrentFont['desc']['MissingWidth']; }
426 else if(isset($this->CurrentFont['MissingWidth'])) { $w += $this->CurrentFont['MissingWidth']; }
427 else { $w += 500; }
428 }
429 }
430 else {
431 $l = strlen($s);
432 for($i=0;$i<$l;$i++)
433 $w += $cw[$s[$i]];
434 }
435 return $w*$this->FontSize/1000;
436}
437
438function SetLineWidth($width)
439{
440 // Set line width
441 $this->LineWidth = $width;
442 if($this->page>0)
443 $this->_out(sprintf('%.2F w',$width*$this->k));
444}
445
446function Line($x1, $y1, $x2, $y2)
447{
448 // Draw a line
449 $this->_out(sprintf('%.2F %.2F m %.2F %.2F l S',$x1*$this->k,($this->h-$y1)*$this->k,$x2*$this->k,($this->h-$y2)*$this->k));
450}
451
452function Rect($x, $y, $w, $h, $style='')
453{
454 // Draw a rectangle
455 if($style=='F')
456 $op = 'f';
457 elseif($style=='FD' || $style=='DF')
458 $op = 'B';
459 else
460 $op = 'S';
461 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
462}
463
464function AddFont($family, $style='', $file='', $uni=false)
465{
466 // Add a TrueType, OpenType or Type1 font
467 $family = strtolower($family);
468 $style = strtoupper($style);
469 if($style=='IB')
470 $style = 'BI';
471 if($file=='') {
472 if ($uni) {
473 $file = str_replace(' ','',$family).strtolower($style).'.ttf';
474 }
475 else {
476 $file = str_replace(' ','',$family).strtolower($style).'.php';
477 }
478 }
479 $fontkey = $family.$style;
480 if(isset($this->fonts[$fontkey]))
481 return;
482 if ($uni) {
483 if (defined("_SYSTEM_TTFONTS") && file_exists(_SYSTEM_TTFONTS.$file )) { $ttffilename = _SYSTEM_TTFONTS.$file ; }
484 else { $ttffilename = $this->fontpath.'unifont/'.$file ; }
485 $unifilename = $this->fontpath.'unifont/'.strtolower(substr($file ,0,(strpos($file ,'.'))));
486 $name = '';
487 $originalsize = 0;
488 $ttfstat = stat($ttffilename);
489 if (file_exists($unifilename.'.mtx.php')) {
490 include($unifilename.'.mtx.php');
491 }
492 if (!isset($type) || !isset($name) || $originalsize != $ttfstat['size']) {
493 $ttffile = $ttffilename;
494 require_once($this->fontpath.'unifont/ttfonts.php');
495 $ttf = new TTFontFile();
496 $ttf->getMetrics($ttffile);
497 $cw = $ttf->charWidths;
498 $name = preg_replace('/[ ()]/','',$ttf->fullName);
499
500 $desc= array('Ascent'=>round($ttf->ascent),
501 'Descent'=>round($ttf->descent),
502 'CapHeight'=>round($ttf->capHeight),
503 'Flags'=>$ttf->flags,
504 'FontBBox'=>'['.round($ttf->bbox[0])." ".round($ttf->bbox[1])." ".round($ttf->bbox[2])." ".round($ttf->bbox[3]).']',
505 'ItalicAngle'=>$ttf->italicAngle,
506 'StemV'=>round($ttf->stemV),
507 'MissingWidth'=>round($ttf->defaultWidth));
508 $up = round($ttf->underlinePosition);
509 $ut = round($ttf->underlineThickness);
510 $originalsize = $ttfstat['size']+0;
511 $type = 'TTF';
512 // Generate metrics .php file
513 $s='<?php'."\n";
514 $s.='$name=\''.$name."';\n";
515 $s.='$type=\''.$type."';\n";
516 $s.='$desc='.var_export($desc,true).";\n";
517 $s.='$up='.$up.";\n";
518 $s.='$ut='.$ut.";\n";
519 $s.='$ttffile=\''.$ttffile."';\n";
520 $s.='$originalsize='.$originalsize.";\n";
521 $s.='$fontkey=\''.$fontkey."';\n";
522 $s.="?>";
523 if (is_writable(dirname($this->fontpath.'unifont/'.'x'))) {
524 $fh = fopen($unifilename.'.mtx.php',"w");
525 fwrite($fh,$s,strlen($s));
526 fclose($fh);
527 $fh = fopen($unifilename.'.cw.dat',"wb");
528 fwrite($fh,$cw,strlen($cw));
529 fclose($fh);
530 @unlink($unifilename.'.cw127.php');
531 }
532 unset($ttf);
533 }
534 else {
535 $cw = @file_get_contents($unifilename.'.cw.dat');
536 }
537 $i = count($this->fonts)+1;
538 if(!empty($this->AliasNbPages))
539 $sbarr = range(0,57);
540 else
541 $sbarr = range(0,32);
542 $this->fonts[$fontkey] = array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'ttffile'=>$ttffile, 'fontkey'=>$fontkey, 'subset'=>$sbarr, 'unifilename'=>$unifilename);
543
544 $this->FontFiles[$fontkey]=array('length1'=>$originalsize, 'type'=>"TTF", 'ttffile'=>$ttffile);
545 $this->FontFiles[$file]=array('type'=>"TTF");
546 unset($cw);
547 }
548 else {
549 $info = $this->_loadfont($file);
550 $info['i'] = count($this->fonts)+1;
551 if(!empty($info['file']))
552 {
553 // Embedded font
554 if($info['type']=='TrueType')
555 $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']);
556 else
557 $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']);
558 }
559 $this->fonts[$fontkey] = $info;
560 }
561}
562
563function SetFont($family, $style='', $size=0)
564{
565 // Select a font; size given in points
566 if($family=='')
567 $family = $this->FontFamily;
568 else
569 $family = strtolower($family);
570 $style = strtoupper($style);
571 if(strpos($style,'U')!==false)
572 {
573 $this->underline = true;
574 $style = str_replace('U','',$style);
575 }
576 else
577 $this->underline = false;
578 if($style=='IB')
579 $style = 'BI';
580 if($size==0)
581 $size = $this->FontSizePt;
582 // Test if font is already selected
583 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
584 return;
585
586 // Test if font is already loaded
587 $fontkey = $family.$style;
588 if(!isset($this->fonts[$fontkey]))
589 {
590 // Test if one of the core fonts
591 if($family=='arial')
592 $family = 'helvetica';
593 if(in_array($family,$this->CoreFonts))
594 {
595 if($family=='symbol' || $family=='zapfdingbats')
596 $style = '';
597 $fontkey = $family.$style;
598 if(!isset($this->fonts[$fontkey]))
599 $this->AddFont($family,$style);
600 }
601 else
602 $this->Error('Undefined font: '.$family.' '.$style);
603 }
604 // Select it
605 $this->FontFamily = $family;
606 $this->FontStyle = $style;
607 $this->FontSizePt = $size;
608 $this->FontSize = $size/$this->k;
609 $this->CurrentFont = &$this->fonts[$fontkey];
610 if ($this->fonts[$fontkey]['type']=='TTF') { $this->unifontSubset = true; }
611 else { $this->unifontSubset = false; }
612 if($this->page>0)
613 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
614}
615
616function SetFontSize($size)
617{
618 // Set font size in points
619 if($this->FontSizePt==$size)
620 return;
621 $this->FontSizePt = $size;
622 $this->FontSize = $size/$this->k;
623 if($this->page>0)
624 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
625}
626
627function AddLink()
628{
629 // Create a new internal link
630 $n = count($this->links)+1;
631 $this->links[$n] = array(0, 0);
632 return $n;
633}
634
635function SetLink($link, $y=0, $page=-1)
636{
637 // Set destination of internal link
638 if($y==-1)
639 $y = $this->y;
640 if($page==-1)
642 $this->links[$link] = array($page, $y);
643}
644
645function Link($x, $y, $w, $h, $link)
646{
647 // Put a link on the page
648 $this->PageLinks[$this->page][] = array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link);
649}
650
651function Text($x, $y, $txt)
652{
653 // Output a string
654 $txt = (string)$txt;
655 if(!isset($this->CurrentFont))
656 $this->Error('No font has been set');
657 if ($this->unifontSubset)
658 {
659 $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')';
660 foreach($this->UTF8StringToArray($txt) as $uni)
661 $this->CurrentFont['subset'][$uni] = $uni;
662 }
663 else
664 $txt2 = '('.$this->_escape($txt).')';
665 $s = sprintf('BT %.2F %.2F Td %s Tj ET',$x*$this->k,($this->h-$y)*$this->k,$txt2);
666 if($this->underline && $txt!='')
667 $s .= ' '.$this->_dounderline($x,$y,$txt);
668 if($this->ColorFlag)
669 $s = 'q '.$this->TextColor.' '.$s.' Q';
670 $this->_out($s);
671}
672
673function AcceptPageBreak()
674{
675 // Accept automatic page break or not
676 return $this->AutoPageBreak;
677}
678
679function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
680{
681 // Output a cell
682 $txt = (string)$txt;
683 $k = $this->k;
684 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
685 {
686 // Automatic page break
687 $x = $this->x;
688 $ws = $this->ws;
689 if($ws>0)
690 {
691 $this->ws = 0;
692 $this->_out('0 Tw');
693 }
694 $this->AddPage($this->CurOrientation,$this->CurPageSize,$this->CurRotation);
695 $this->x = $x;
696 if($ws>0)
697 {
698 $this->ws = $ws;
699 $this->_out(sprintf('%.3F Tw',$ws*$k));
700 }
701 }
702 if($w==0)
703 $w = $this->w-$this->rMargin-$this->x;
704 $s = '';
705 if($fill || $border==1)
706 {
707 if($fill)
708 $op = ($border==1) ? 'B' : 'f';
709 else
710 $op = 'S';
711 $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
712 }
713 if(is_string($border))
714 {
715 $x = $this->x;
716 $y = $this->y;
717 if(strpos($border,'L')!==false)
718 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
719 if(strpos($border,'T')!==false)
720 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
721 if(strpos($border,'R')!==false)
722 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
723 if(strpos($border,'B')!==false)
724 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
725 }
726 if($txt!=='')
727 {
728 if(!isset($this->CurrentFont))
729 $this->Error('No font has been set');
730 if($align=='R')
731 $dx = $w-$this->cMargin-$this->GetStringWidth($txt);
732 elseif($align=='C')
733 $dx = ($w-$this->GetStringWidth($txt))/2;
734 else
735 $dx = $this->cMargin;
736 if($this->ColorFlag)
737 $s .= 'q '.$this->TextColor.' ';
738 // If multibyte, Tw has no effect - do word spacing using an adjustment before each space
739 if ($this->ws && $this->unifontSubset) {
740 foreach($this->UTF8StringToArray($txt) as $uni)
741 $this->CurrentFont['subset'][$uni] = $uni;
742 $space = $this->_escape($this->UTF8ToUTF16BE(' ', false));
743 $s .= sprintf('BT 0 Tw %.2F %.2F Td [',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k);
744 $t = explode(' ',$txt);
745 $numt = count($t);
746 for($i=0;$i<$numt;$i++) {
747 $tx = $t[$i];
748 $tx = '('.$this->_escape($this->UTF8ToUTF16BE($tx, false)).')';
749 $s .= sprintf('%s ',$tx);
750 if (($i+1)<$numt) {
751 $adj = -($this->ws*$this->k)*1000/$this->FontSizePt;
752 $s .= sprintf('%d(%s) ',$adj,$space);
753 }
754 }
755 $s .= '] TJ';
756 $s .= ' ET';
757 }
758 else {
759 if ($this->unifontSubset)
760 {
761 $txt2 = '('.$this->_escape($this->UTF8ToUTF16BE($txt, false)).')';
762 foreach($this->UTF8StringToArray($txt) as $uni)
763 $this->CurrentFont['subset'][$uni] = $uni;
764 }
765 else
766 $txt2='('.$this->_escape($txt).')';
767 $s .= sprintf('BT %.2F %.2F Td %s Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$txt2);
768 }
769 if($this->underline)
770 $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
771 if($this->ColorFlag)
772 $s .= ' Q';
773 if($link)
774 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
775 }
776 if($s)
777 $this->_out($s);
778 $this->lasth = $h;
779 if($ln>0)
780 {
781 // Go to next line
782 $this->y += $h;
783 if($ln==1)
784 $this->x = $this->lMargin;
785 }
786 else
787 $this->x += $w;
788}
789
790function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
791{
792 // Output text with automatic or explicit line breaks
793 if(!isset($this->CurrentFont))
794 $this->Error('No font has been set');
795 $cw = $this->CurrentFont['cw'];
796 if($w==0)
797 $w = $this->w-$this->rMargin-$this->x;
798 $wmax = ($w-2*$this->cMargin);
799 //$wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
800 $s = str_replace("\r",'',(string)$txt);
801 if ($this->unifontSubset) {
802 $nb=mb_strlen($s, 'utf-8');
803 while($nb>0 && mb_substr($s,$nb-1,1,'utf-8')=="\n") $nb--;
804 }
805 else {
806 $nb = strlen($s);
807 if($nb>0 && $s[$nb-1]=="\n")
808 $nb--;
809 }
810 $b = 0;
811 if($border)
812 {
813 if($border==1)
814 {
815 $border = 'LTRB';
816 $b = 'LRT';
817 $b2 = 'LR';
818 }
819 else
820 {
821 $b2 = '';
822 if(strpos($border,'L')!==false)
823 $b2 .= 'L';
824 if(strpos($border,'R')!==false)
825 $b2 .= 'R';
826 $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2;
827 }
828 }
829 $sep = -1;
830 $i = 0;
831 $j = 0;
832 $l = 0;
833 $ns = 0;
834 $nl = 1;
835 while($i<$nb)
836 {
837 // Get next character
838 if ($this->unifontSubset) {
839 $c = mb_substr($s,$i,1,'UTF-8');
840 }
841 else {
842 $c=$s[$i];
843 }
844 if($c=="\n")
845 {
846 // Explicit line break
847 if($this->ws>0)
848 {
849 $this->ws = 0;
850 $this->_out('0 Tw');
851 }
852 if ($this->unifontSubset) {
853 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
854 }
855 else {
856 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
857 }
858 $i++;
859 $sep = -1;
860 $j = $i;
861 $l = 0;
862 $ns = 0;
863 $nl++;
864 if($border && $nl==2)
865 $b = $b2;
866 continue;
867 }
868 if($c==' ')
869 {
870 $sep = $i;
871 $ls = $l;
872 $ns++;
873 }
874
875 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
876 else { $l += $cw[$c]*$this->FontSize/1000; }
877
878 if($l>$wmax)
879 {
880 // Automatic line break
881 if($sep==-1)
882 {
883 if($i==$j)
884 $i++;
885 if($this->ws>0)
886 {
887 $this->ws = 0;
888 $this->_out('0 Tw');
889 }
890 if ($this->unifontSubset) {
891 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
892 }
893 else {
894 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
895 }
896 }
897 else
898 {
899 if($align=='J')
900 {
901 $this->ws = ($ns>1) ? ($wmax-$ls)/($ns-1) : 0;
902 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
903 }
904 if ($this->unifontSubset) {
905 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),$b,2,$align,$fill);
906 }
907 else {
908 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
909 }
910 $i = $sep+1;
911 }
912 $sep = -1;
913 $j = $i;
914 $l = 0;
915 $ns = 0;
916 $nl++;
917 if($border && $nl==2)
918 $b = $b2;
919 }
920 else
921 $i++;
922 }
923 // Last chunk
924 if($this->ws>0)
925 {
926 $this->ws = 0;
927 $this->_out('0 Tw');
928 }
929 if($border && strpos($border,'B')!==false)
930 $b .= 'B';
931 if ($this->unifontSubset) {
932 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),$b,2,$align,$fill);
933 }
934 else {
935 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
936 }
937 $this->x = $this->lMargin;
938}
939
940function Write($h, $txt, $link='')
941{
942 // Output text in flowing mode
943 if(!isset($this->CurrentFont))
944 $this->Error('No font has been set');
945 $cw = $this->CurrentFont['cw'];
946 $w = $this->w-$this->rMargin-$this->x;
947 $wmax = ($w-2*$this->cMargin);
948 $s = str_replace("\r",'',(string)$txt);
949 if ($this->unifontSubset) {
950 $nb = mb_strlen($s, 'UTF-8');
951 if($nb==1 && $s==" ") {
952 $this->x += $this->GetStringWidth($s);
953 return;
954 }
955 }
956 else {
957 $nb = strlen($s);
958 }
959 $sep = -1;
960 $i = 0;
961 $j = 0;
962 $l = 0;
963 $nl = 1;
964 while($i<$nb)
965 {
966 // Get next character
967 if ($this->unifontSubset) {
968 $c = mb_substr($s,$i,1,'UTF-8');
969 }
970 else {
971 $c = $s[$i];
972 }
973 if($c=="\n")
974 {
975 // Explicit line break
976 if ($this->unifontSubset) {
977 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',false,$link);
978 }
979 else {
980 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',false,$link);
981 }
982 $i++;
983 $sep = -1;
984 $j = $i;
985 $l = 0;
986 if($nl==1)
987 {
988 $this->x = $this->lMargin;
989 $w = $this->w-$this->rMargin-$this->x;
990 $wmax = ($w-2*$this->cMargin);
991 }
992 $nl++;
993 continue;
994 }
995 if($c==' ')
996 $sep = $i;
997
998 if ($this->unifontSubset) { $l += $this->GetStringWidth($c); }
999 else { $l += $cw[$c]*$this->FontSize/1000; }
1000
1001 if($l>$wmax)
1002 {
1003 // Automatic line break
1004 if($sep==-1)
1005 {
1006 if($this->x>$this->lMargin)
1007 {
1008 // Move to next line
1009 $this->x = $this->lMargin;
1010 $this->y += $h;
1011 $w = $this->w-$this->rMargin-$this->x;
1012 $wmax = ($w-2*$this->cMargin);
1013 $i++;
1014 $nl++;
1015 continue;
1016 }
1017 if($i==$j)
1018 $i++;
1019 if ($this->unifontSubset) {
1020 $this->Cell($w,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,2,'',false,$link);
1021 }
1022 else {
1023 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',false,$link);
1024 }
1025 }
1026 else
1027 {
1028 if ($this->unifontSubset) {
1029 $this->Cell($w,$h,mb_substr($s,$j,$sep-$j,'UTF-8'),0,2,'',false,$link);
1030 }
1031 else {
1032 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',false,$link);
1033 }
1034 $i = $sep+1;
1035 }
1036 $sep = -1;
1037 $j = $i;
1038 $l = 0;
1039 if($nl==1)
1040 {
1041 $this->x = $this->lMargin;
1042 $w = $this->w-$this->rMargin-$this->x;
1043 $wmax = ($w-2*$this->cMargin);
1044 }
1045 $nl++;
1046 }
1047 else
1048 $i++;
1049 }
1050 // Last chunk
1051 if($i!=$j) {
1052 if ($this->unifontSubset) {
1053 $this->Cell($l,$h,mb_substr($s,$j,$i-$j,'UTF-8'),0,0,'',false,$link);
1054 }
1055 else {
1056 $this->Cell($l,$h,substr($s,$j),0,0,'',false,$link);
1057 }
1058 }
1059}
1060
1061function Ln($h=null)
1062{
1063 // Line feed; default value is the last cell height
1064 $this->x = $this->lMargin;
1065 if($h===null)
1066 $this->y += $this->lasth;
1067 else
1068 $this->y += $h;
1069}
1070
1071function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
1072{
1073 // Put an image on the page
1074 if($file=='')
1075 $this->Error('Image file name is empty');
1076 if(!isset($this->images[$file]))
1077 {
1078 // First use of this image, get info
1079 if($type=='')
1080 {
1081 $pos = strrpos($file,'.');
1082 if(!$pos)
1083 $this->Error('Image file has no extension and no type was specified: '.$file);
1084 $type = substr($file,$pos+1);
1085 }
1086 $type = strtolower($type);
1087 if($type=='jpeg')
1088 $type = 'jpg';
1089 $mtd = '_parse'.$type;
1090 if(!method_exists($this,$mtd))
1091 $this->Error('Unsupported image type: '.$type);
1092 $info = $this->$mtd($file);
1093 $info['i'] = count($this->images)+1;
1094 $this->images[$file] = $info;
1095 }
1096 else
1097 $info = $this->images[$file];
1098
1099 // Automatic width and height calculation if needed
1100 if($w==0 && $h==0)
1101 {
1102 // Put image at 96 dpi
1103 $w = -96;
1104 $h = -96;
1105 }
1106 if($w<0)
1107 $w = -$info['w']*72/$w/$this->k;
1108 if($h<0)
1109 $h = -$info['h']*72/$h/$this->k;
1110 if($w==0)
1111 $w = $h*$info['w']/$info['h'];
1112 if($h==0)
1113 $h = $w*$info['h']/$info['w'];
1114
1115 // Flowing mode
1116 if($y===null)
1117 {
1118 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
1119 {
1120 // Automatic page break
1121 $x2 = $this->x;
1122 $this->AddPage($this->CurOrientation,$this->CurPageSize,$this->CurRotation);
1123 $this->x = $x2;
1124 }
1125 $y = $this->y;
1126 $this->y += $h;
1127 }
1128
1129 if($x===null)
1130 $x = $this->x;
1131 $this->_out(sprintf('q %.2F 0 0 %.2F %.2F %.2F cm /I%d Do Q',$w*$this->k,$h*$this->k,$x*$this->k,($this->h-($y+$h))*$this->k,$info['i']));
1132 if($link)
1133 $this->Link($x,$y,$w,$h,$link);
1134}
1135
1136function GetPageWidth()
1137{
1138 // Get current page width
1139 return $this->w;
1140}
1141
1142function GetPageHeight()
1143{
1144 // Get current page height
1145 return $this->h;
1146}
1147
1148function GetX()
1149{
1150 // Get x position
1151 return $this->x;
1152}
1153
1154function SetX($x)
1155{
1156 // Set x position
1157 if($x>=0)
1158 $this->x = $x;
1159 else
1160 $this->x = $this->w+$x;
1161}
1162
1163function GetY()
1164{
1165 // Get y position
1166 return $this->y;
1167}
1168
1169function SetY($y, $resetX=true)
1170{
1171 // Set y position and optionally reset x
1172 if($y>=0)
1173 $this->y = $y;
1174 else
1175 $this->y = $this->h+$y;
1176 if($resetX)
1177 $this->x = $this->lMargin;
1178}
1179
1180function SetXY($x, $y)
1181{
1182 // Set x and y positions
1183 $this->SetX($x);
1184 $this->SetY($y,false);
1185}
1186
1187function Output($dest='', $name='', $isUTF8=false)
1188{
1189 // Output PDF to some destination
1190 $this->Close();
1191 if(strlen($name)==1 && strlen($dest)!=1)
1192 {
1193 // Fix parameter order
1194 $tmp = $dest;
1195 $dest = $name;
1196 $name = $tmp;
1197 }
1198 if($dest=='')
1199 $dest = 'I';
1200 if($name=='')
1201 $name = 'doc.pdf';
1202 switch(strtoupper($dest))
1203 {
1204 case 'I':
1205 // Send to standard output
1206 $this->_checkoutput();
1207 if(PHP_SAPI!='cli')
1208 {
1209 // We send to a browser
1210 header('Content-Type: application/pdf');
1211 header('Content-Disposition: inline; '.$this->_httpencode('filename',$name,$isUTF8));
1212 header('Cache-Control: private, max-age=0, must-revalidate');
1213 header('Pragma: public');
1214 }
1215 echo $this->buffer;
1216 break;
1217 case 'D':
1218 // Download file
1219 $this->_checkoutput();
1220 header('Content-Type: application/pdf');
1221 header('Content-Disposition: attachment; '.$this->_httpencode('filename',$name,$isUTF8));
1222 header('Cache-Control: private, max-age=0, must-revalidate');
1223 header('Pragma: public');
1224 echo $this->buffer;
1225 break;
1226 case 'F':
1227 // Save to local file
1228 if(!file_put_contents($name,$this->buffer))
1229 $this->Error('Unable to create output file: '.$name);
1230 break;
1231 case 'S':
1232 // Return as a string
1233 return $this->buffer;
1234 default:
1235 $this->Error('Incorrect output destination: '.$dest);
1236 }
1237 return '';
1238}
1239
1240/*******************************************************************************
1241* Protected methods *
1242*******************************************************************************/
1243
1244protected function _dochecks()
1245{
1246 // Check availability of mbstring
1247 if(!function_exists('mb_strlen'))
1248 $this->Error('mbstring extension is not available');
1249}
1250
1251protected function _checkoutput()
1252{
1253 if(PHP_SAPI!='cli')
1254 {
1255 if(headers_sent($file,$line))
1256 $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)");
1257 }
1258 if(ob_get_length())
1259 {
1260 // The output buffer is not empty
1261 if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents()))
1262 {
1263 // It contains only a UTF-8 BOM and/or whitespace, let's clean it
1264 ob_clean();
1265 }
1266 else
1267 $this->Error("Some data has already been output, can't send PDF file");
1268 }
1269}
1270
1271protected function _getpagesize($size)
1272{
1273 if(is_string($size))
1274 {
1275 $size = strtolower($size);
1276 if(!isset($this->StdPageSizes[$size]))
1277 $this->Error('Unknown page size: '.$size);
1278 $a = $this->StdPageSizes[$size];
1279 return array($a[0]/$this->k, $a[1]/$this->k);
1280 }
1281 else
1282 {
1283 if($size[0]>$size[1])
1284 return array($size[1], $size[0]);
1285 else
1286 return $size;
1287 }
1288}
1289
1290protected function _beginpage($orientation, $size, $rotation)
1291{
1292 $this->page++;
1293 $this->pages[$this->page] = '';
1294 $this->PageLinks[$this->page] = array();
1295 $this->state = 2;
1296 $this->x = $this->lMargin;
1297 $this->y = $this->tMargin;
1298 $this->FontFamily = '';
1299 // Check page size and orientation
1300 if($orientation=='')
1301 $orientation = $this->DefOrientation;
1302 else
1303 $orientation = strtoupper($orientation[0]);
1304 if($size=='')
1305 $size = $this->DefPageSize;
1306 else
1307 $size = $this->_getpagesize($size);
1308 if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])
1309 {
1310 // New size or orientation
1311 if($orientation=='P')
1312 {
1313 $this->w = $size[0];
1314 $this->h = $size[1];
1315 }
1316 else
1317 {
1318 $this->w = $size[1];
1319 $this->h = $size[0];
1320 }
1321 $this->wPt = $this->w*$this->k;
1322 $this->hPt = $this->h*$this->k;
1323 $this->PageBreakTrigger = $this->h-$this->bMargin;
1324 $this->CurOrientation = $orientation;
1325 $this->CurPageSize = $size;
1326 }
1327 if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1])
1328 $this->PageInfo[$this->page]['size'] = array($this->wPt, $this->hPt);
1329 if($rotation!=0)
1330 {
1331 if($rotation%90!=0)
1332 $this->Error('Incorrect rotation value: '.$rotation);
1333 $this->PageInfo[$this->page]['rotation'] = $rotation;
1334 }
1335 $this->CurRotation = $rotation;
1336}
1337
1338protected function _endpage()
1339{
1340 $this->state = 1;
1341}
1342
1343protected function _loadfont($font)
1344{
1345 // Load a font definition file from the font directory
1346 if(strpos($font,'/')!==false || strpos($font,"\\")!==false)
1347 $this->Error('Incorrect font definition file name: '.$font);
1348 include($this->fontpath.$font);
1349 if(!isset($name))
1350 $this->Error('Could not include font definition file');
1351 if(isset($enc))
1352 $enc = strtolower($enc);
1353 if(!isset($subsetted))
1354 $subsetted = false;
1355 return get_defined_vars();
1356}
1357
1358protected function _isascii($s)
1359{
1360 // Test if string is ASCII
1361 $nb = strlen($s);
1362 for($i=0;$i<$nb;$i++)
1363 {
1364 if(ord($s[$i])>127)
1365 return false;
1366 }
1367 return true;
1368}
1369
1370protected function _httpencode($param, $value, $isUTF8)
1371{
1372 // Encode HTTP header field parameter
1373 if($this->_isascii($value))
1374 return $param.'="'.$value.'"';
1375 if(!$isUTF8)
1376 $value = $this->_UTF8encode($value);
1377 return $param."*=UTF-8''".rawurlencode($value);
1378}
1379
1380protected function _UTF8encode($s)
1381{
1382 // Convert ISO-8859-1 to UTF-8
1383 return mb_convert_encoding($s,'UTF-8','ISO-8859-1');
1384}
1385
1386protected function _UTF8toUTF16($s)
1387{
1388 // Convert UTF-8 to UTF-16BE with BOM
1389 return "\xFE\xFF".mb_convert_encoding($s,'UTF-16BE','UTF-8');
1390}
1391
1392protected function _escape($s)
1393{
1394 // Escape special characters
1395 if(strpos($s,'(')!==false || strpos($s,')')!==false || strpos($s,'\\')!==false || strpos($s,"\r")!==false)
1396 return str_replace(array('\\','(',')',"\r"), array('\\\\','\\(','\\)','\\r'), $s);
1397 else
1398 return $s;
1399}
1400
1401protected function _textstring($s)
1402{
1403 // Format a text string
1404 if(!$this->_isascii($s))
1405 $s = $this->_UTF8toUTF16($s);
1406 return '('.$this->_escape($s).')';
1407}
1408
1409protected function _dounderline($x, $y, $txt)
1410{
1411 // Underline text
1412 $up = $this->CurrentFont['up'];
1413 $ut = $this->CurrentFont['ut'];
1414 $w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1415 return sprintf('%.2F %.2F %.2F %.2F re f',$x*$this->k,($this->h-($y-$up/1000*$this->FontSize))*$this->k,$w*$this->k,-$ut/1000*$this->FontSizePt);
1416}
1417
1418protected function _parsejpg($file)
1419{
1420 // Extract info from a JPEG file
1421 $a = getimagesize($file);
1422 if(!$a)
1423 $this->Error('Missing or incorrect image file: '.$file);
1424 if($a[2]!=2)
1425 $this->Error('Not a JPEG file: '.$file);
1426 if(!isset($a['channels']) || $a['channels']==3)
1427 $colspace = 'DeviceRGB';
1428 elseif($a['channels']==4)
1429 $colspace = 'DeviceCMYK';
1430 else
1431 $colspace = 'DeviceGray';
1432 $bpc = isset($a['bits']) ? $a['bits'] : 8;
1433 $data = file_get_contents($file);
1434 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1435}
1436
1437protected function _parsepng($file)
1438{
1439 // Extract info from a PNG file
1440 $f = fopen($file,'rb');
1441 if(!$f)
1442 $this->Error('Can\'t open image file: '.$file);
1443 $info = $this->_parsepngstream($f,$file);
1444 fclose($f);
1445 return $info;
1446}
1447
1448protected function _parsepngstream($f, $file)
1449{
1450 // Check signature
1451 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1452 $this->Error('Not a PNG file: '.$file);
1453
1454 // Read header chunk
1455 $this->_readstream($f,4);
1456 if($this->_readstream($f,4)!='IHDR')
1457 $this->Error('Incorrect PNG file: '.$file);
1458 $w = $this->_readint($f);
1459 $h = $this->_readint($f);
1460 $bpc = ord($this->_readstream($f,1));
1461 if($bpc>8)
1462 $this->Error('16-bit depth not supported: '.$file);
1463 $ct = ord($this->_readstream($f,1));
1464 if($ct==0 || $ct==4)
1465 $colspace = 'DeviceGray';
1466 elseif($ct==2 || $ct==6)
1467 $colspace = 'DeviceRGB';
1468 elseif($ct==3)
1469 $colspace = 'Indexed';
1470 else
1471 $this->Error('Unknown color type: '.$file);
1472 if(ord($this->_readstream($f,1))!=0)
1473 $this->Error('Unknown compression method: '.$file);
1474 if(ord($this->_readstream($f,1))!=0)
1475 $this->Error('Unknown filter method: '.$file);
1476 if(ord($this->_readstream($f,1))!=0)
1477 $this->Error('Interlacing not supported: '.$file);
1478 $this->_readstream($f,4);
1479 $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w;
1480
1481 // Scan chunks looking for palette, transparency and image data
1482 $pal = '';
1483 $trns = '';
1484 $data = '';
1485 do
1486 {
1487 $n = $this->_readint($f);
1488 $type = $this->_readstream($f,4);
1489 if($type=='PLTE')
1490 {
1491 // Read palette
1492 $pal = $this->_readstream($f,$n);
1493 $this->_readstream($f,4);
1494 }
1495 elseif($type=='tRNS')
1496 {
1497 // Read transparency info
1498 $t = $this->_readstream($f,$n);
1499 if($ct==0)
1500 $trns = array(ord(substr($t,1,1)));
1501 elseif($ct==2)
1502 $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
1503 else
1504 {
1505 $pos = strpos($t,chr(0));
1506 if($pos!==false)
1507 $trns = array($pos);
1508 }
1509 $this->_readstream($f,4);
1510 }
1511 elseif($type=='IDAT')
1512 {
1513 // Read image data block
1514 $data .= $this->_readstream($f,$n);
1515 $this->_readstream($f,4);
1516 }
1517 elseif($type=='IEND')
1518 break;
1519 else
1520 $this->_readstream($f,$n+4);
1521 }
1522 while($n);
1523
1524 if($colspace=='Indexed' && empty($pal))
1525 $this->Error('Missing palette in '.$file);
1526 $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns);
1527 if($ct>=4)
1528 {
1529 // Extract alpha channel
1530 if(!function_exists('gzuncompress'))
1531 $this->Error('Zlib not available, can\'t handle alpha channel: '.$file);
1532 $data = gzuncompress($data);
1533 $color = '';
1534 $alpha = '';
1535 if($ct==4)
1536 {
1537 // Gray image
1538 $len = 2*$w;
1539 for($i=0;$i<$h;$i++)
1540 {
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 }
1549 else
1550 {
1551 // RGB image
1552 $len = 4*$w;
1553 for($i=0;$i<$h;$i++)
1554 {
1555 $pos = (1+$len)*$i;
1556 $color .= $data[$pos];
1557 $alpha .= $data[$pos];
1558 $line = substr($data,$pos+1,$len);
1559 $color .= preg_replace('/(.{3})./s','$1',$line);
1560 $alpha .= preg_replace('/.{3}(.)/s','$1',$line);
1561 }
1562 }
1563 unset($data);
1564 $data = gzcompress($color);
1565 $info['smask'] = gzcompress($alpha);
1566 $this->WithAlpha = true;
1567 if($this->PDFVersion<'1.4')
1568 $this->PDFVersion = '1.4';
1569 }
1570 $info['data'] = $data;
1571 return $info;
1572}
1573
1574protected function _readstream($f, $n)
1575{
1576 // Read n bytes from stream
1577 $res = '';
1578 while($n>0 && !feof($f))
1579 {
1580 $s = fread($f,$n);
1581 if($s===false)
1582 $this->Error('Error while reading stream');
1583 $n -= strlen($s);
1584 $res .= $s;
1585 }
1586 if($n>0)
1587 $this->Error('Unexpected end of stream');
1588 return $res;
1589}
1590
1591protected function _readint($f)
1592{
1593 // Read a 4-byte integer from stream
1594 $a = unpack('Ni',$this->_readstream($f,4));
1595 return $a['i'];
1596}
1597
1598protected function _parsegif($file)
1599{
1600 // Extract info from a GIF file (via PNG conversion)
1601 if(!function_exists('imagepng'))
1602 $this->Error('GD extension is required for GIF support');
1603 if(!function_exists('imagecreatefromgif'))
1604 $this->Error('GD has no GIF read support');
1605 $im = imagecreatefromgif($file);
1606 if(!$im)
1607 $this->Error('Missing or incorrect image file: '.$file);
1608 imageinterlace($im,0);
1609 ob_start();
1610 imagepng($im);
1611 $data = ob_get_clean();
1612 imagedestroy($im);
1613 $f = fopen('php://temp','rb+');
1614 if(!$f)
1615 $this->Error('Unable to create memory stream');
1616 fwrite($f,$data);
1617 rewind($f);
1618 $info = $this->_parsepngstream($f,$file);
1619 fclose($f);
1620 return $info;
1621}
1622
1623protected function _out($s)
1624{
1625 // Add a line to the document
1626 if($this->state==2)
1627 $this->pages[$this->page] .= $s."\n";
1628 elseif($this->state==1)
1629 $this->_put($s);
1630 elseif($this->state==0)
1631 $this->Error('No page has been added yet');
1632 elseif($this->state==3)
1633 $this->Error('The document is closed');
1634}
1635
1636protected function _put($s)
1637{
1638 $this->buffer .= $s."\n";
1639}
1640
1641protected function _getoffset()
1642{
1643 return strlen($this->buffer);
1644}
1645
1646protected function _newobj($n=null)
1647{
1648 // Begin a new object
1649 if($n===null)
1650 $n = ++$this->n;
1651 $this->offsets[$n] = $this->_getoffset();
1652 $this->_put($n.' 0 obj');
1653}
1654
1655protected function _putstream($data)
1656{
1657 $this->_put('stream');
1658 $this->_put($data);
1659 $this->_put('endstream');
1660}
1661
1662protected function _putstreamobject($data)
1663{
1664 if($this->compress)
1665 {
1666 $entries = '/Filter /FlateDecode ';
1667 $data = gzcompress($data);
1668 }
1669 else
1670 $entries = '';
1671 $entries .= '/Length '.strlen($data);
1672 $this->_newobj();
1673 $this->_put('<<'.$entries.'>>');
1674 $this->_putstream($data);
1675 $this->_put('endobj');
1676}
1677
1678protected function _putlinks($n)
1679{
1680 foreach($this->PageLinks[$n] as $pl)
1681 {
1682 $this->_newobj();
1683 $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1684 $s = '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1685 if(is_string($pl[4]))
1686 $s .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1687 else
1688 {
1689 $l = $this->links[$pl[4]];
1690 if(isset($this->PageInfo[$l[0]]['size']))
1691 $h = $this->PageInfo[$l[0]]['size'][1];
1692 else
1693 $h = ($this->DefOrientation=='P') ? $this->DefPageSize[1]*$this->k : $this->DefPageSize[0]*$this->k;
1694 $s .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',$this->PageInfo[$l[0]]['n'],$h-$l[1]*$this->k);
1695 }
1696 $this->_put($s);
1697 $this->_put('endobj');
1698 }
1699}
1700
1701protected function _putpage($n)
1702{
1703 $this->_newobj();
1704 $this->_put('<</Type /Page');
1705 $this->_put('/Parent 1 0 R');
1706 if(isset($this->PageInfo[$n]['size']))
1707 $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageInfo[$n]['size'][0],$this->PageInfo[$n]['size'][1]));
1708 if(isset($this->PageInfo[$n]['rotation']))
1709 $this->_put('/Rotate '.$this->PageInfo[$n]['rotation']);
1710 $this->_put('/Resources 2 0 R');
1711 if(!empty($this->PageLinks[$n]))
1712 {
1713 $s = '/Annots [';
1714 foreach($this->PageLinks[$n] as $pl)
1715 $s .= $pl[5].' 0 R ';
1716 $s .= ']';
1717 $this->_put($s);
1718 }
1719 if($this->WithAlpha)
1720 $this->_put('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');
1721 $this->_put('/Contents '.($this->n+1).' 0 R>>');
1722 $this->_put('endobj');
1723 // Page content
1724 if(!empty($this->AliasNbPages)) {
1725 $alias = $this->UTF8ToUTF16BE($this->AliasNbPages, false);
1726 $r = $this->UTF8ToUTF16BE($this->page, false);
1727 $this->pages[$n] = str_replace($alias,$r,$this->pages[$n]);
1728 // Now repeat for no pages in non-subset fonts
1729 $this->pages[$n] = str_replace($this->AliasNbPages,$this->page,$this->pages[$n]);
1730 }
1731 $this->_putstreamobject($this->pages[$n]);
1732 // Link annotations
1733 $this->_putlinks($n);
1734}
1735
1736protected function _putpages()
1737{
1738 $nb = $this->page;
1739 $n = $this->n;
1740 for($i=1;$i<=$nb;$i++)
1741 {
1742 $this->PageInfo[$i]['n'] = ++$n;
1743 $n++;
1744 foreach($this->PageLinks[$i] as &$pl)
1745 $pl[5] = ++$n;
1746 unset($pl);
1747 }
1748 for($i=1;$i<=$nb;$i++)
1749 $this->_putpage($i);
1750 // Pages root
1751 $this->_newobj(1);
1752 $this->_put('<</Type /Pages');
1753 $kids = '/Kids [';
1754 for($i=1;$i<=$nb;$i++)
1755 $kids .= $this->PageInfo[$i]['n'].' 0 R ';
1756 $kids .= ']';
1757 $this->_put($kids);
1758 $this->_put('/Count '.$nb);
1759 if($this->DefOrientation=='P')
1760 {
1761 $w = $this->DefPageSize[0];
1762 $h = $this->DefPageSize[1];
1763 }
1764 else
1765 {
1766 $w = $this->DefPageSize[1];
1767 $h = $this->DefPageSize[0];
1768 }
1769 $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$w*$this->k,$h*$this->k));
1770 $this->_put('>>');
1771 $this->_put('endobj');
1772}
1773
1774protected function _putfonts()
1775{
1776 foreach($this->FontFiles as $file=>$info)
1777 {
1778 if (!isset($info['type']) || $info['type']!='TTF') {
1779 // Font file embedding
1780 $this->_newobj();
1781 $this->FontFiles[$file]['n'] = $this->n;
1782 $font = file_get_contents($this->fontpath.$file,true);
1783 if(!$font)
1784 $this->Error('Font file not found: '.$file);
1785 $compressed = (substr($file,-2)=='.z');
1786 if(!$compressed && isset($info['length2']))
1787 $font = substr($font,6,$info['length1']).substr($font,6+$info['length1']+6,$info['length2']);
1788 $this->_put('<</Length '.strlen($font));
1789 if($compressed)
1790 $this->_put('/Filter /FlateDecode');
1791 $this->_put('/Length1 '.$info['length1']);
1792 if(isset($info['length2']))
1793 $this->_put('/Length2 '.$info['length2'].' /Length3 0');
1794 $this->_put('>>');
1795 $this->_putstream($font);
1796 $this->_put('endobj');
1797 }
1798 }
1799 foreach($this->fonts as $k=>$font)
1800 {
1801 // Encoding
1802 if(isset($font['diff']))
1803 {
1804 if(!isset($this->encodings[$font['enc']]))
1805 {
1806 $this->_newobj();
1807 $this->_put('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$font['diff'].']>>');
1808 $this->_put('endobj');
1809 $this->encodings[$font['enc']] = $this->n;
1810 }
1811 }
1812 // ToUnicode CMap
1813 if(isset($font['uv']))
1814 {
1815 if(isset($font['enc']))
1816 $cmapkey = $font['enc'];
1817 else
1818 $cmapkey = $font['name'];
1819 if(!isset($this->cmaps[$cmapkey]))
1820 {
1821 $cmap = $this->_tounicodecmap($font['uv']);
1822 $this->_putstreamobject($cmap);
1823 $this->cmaps[$cmapkey] = $this->n;
1824 }
1825 }
1826 // Font object
1827 $type = $font['type'];
1828 $name = $font['name'];
1829 if($type=='Core')
1830 {
1831 // Core font
1832 $this->fonts[$k]['n'] = $this->n+1;
1833 $this->_newobj();
1834 $this->_put('<</Type /Font');
1835 $this->_put('/BaseFont /'.$name);
1836 $this->_put('/Subtype /Type1');
1837 if($name!='Symbol' && $name!='ZapfDingbats')
1838 $this->_put('/Encoding /WinAnsiEncoding');
1839 if(isset($font['uv']))
1840 $this->_put('/ToUnicode '.$this->cmaps[$cmapkey].' 0 R');
1841 $this->_put('>>');
1842 $this->_put('endobj');
1843 }
1844 elseif($type=='Type1' || $type=='TrueType')
1845 {
1846 // Additional Type1 or TrueType/OpenType font
1847 if(isset($font['subsetted']) && $font['subsetted'])
1848 $name = 'AAAAAA+'.$name;
1849 $this->fonts[$k]['n'] = $this->n+1;
1850 $this->_newobj();
1851 $this->_put('<</Type /Font');
1852 $this->_put('/BaseFont /'.$name);
1853 $this->_put('/Subtype /'.$type);
1854 $this->_put('/FirstChar 32 /LastChar 255');
1855 $this->_put('/Widths '.($this->n+1).' 0 R');
1856 $this->_put('/FontDescriptor '.($this->n+2).' 0 R');
1857
1858 if($font['enc'])
1859 {
1860 if(isset($font['diff']))
1861 $this->_put('/Encoding '.$this->encodings[$font['enc']].' 0 R');
1862 else
1863 $this->_put('/Encoding /WinAnsiEncoding');
1864 }
1865
1866 if(isset($font['uv']))
1867 $this->_put('/ToUnicode '.$this->cmaps[$cmapkey].' 0 R');
1868 $this->_put('>>');
1869 $this->_put('endobj');
1870 // Widths
1871 $this->_newobj();
1872 $cw = $font['cw'];
1873 $s = '[';
1874 for($i=32;$i<=255;$i++)
1875 $s .= $cw[chr($i)].' ';
1876 $this->_put($s.']');
1877 $this->_put('endobj');
1878 // Descriptor
1879 $this->_newobj();
1880 $s = '<</Type /FontDescriptor /FontName /'.$name;
1881 foreach($font['desc'] as $k=>$v)
1882 $s .= ' /'.$k.' '.$v;
1883
1884 if(!empty($font['file']))
1885 $s .= ' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
1886 $this->_put($s.'>>');
1887 $this->_put('endobj');
1888 }
1889 // TrueType embedded SUBSETS or FULL
1890 else if ($type=='TTF') {
1891 $this->fonts[$k]['n']=$this->n+1;
1892 require_once($this->fontpath.'unifont/ttfonts.php');
1893 $ttf = new TTFontFile();
1894 $fontname = 'MPDFAA'.'+'.$font['name'];
1895 $subset = $font['subset'];
1896 unset($subset[0]);
1897 $ttfontstream = $ttf->makeSubset($font['ttffile'], $subset);
1898 $ttfontsize = strlen($ttfontstream);
1899 $fontstream = gzcompress($ttfontstream);
1900 $codeToGlyph = $ttf->codeToGlyph;
1901 unset($codeToGlyph[0]);
1902
1903 // Type0 Font
1904 // A composite font - a font composed of other fonts, organized hierarchically
1905 $this->_newobj();
1906 $this->_put('<</Type /Font');
1907 $this->_put('/Subtype /Type0');
1908 $this->_put('/BaseFont /'.$fontname.'');
1909 $this->_put('/Encoding /Identity-H');
1910 $this->_put('/DescendantFonts ['.($this->n + 1).' 0 R]');
1911 $this->_put('/ToUnicode '.($this->n + 2).' 0 R');
1912 $this->_put('>>');
1913 $this->_put('endobj');
1914
1915 // CIDFontType2
1916 // A CIDFont whose glyph descriptions are based on TrueType font technology
1917 $this->_newobj();
1918 $this->_put('<</Type /Font');
1919 $this->_put('/Subtype /CIDFontType2');
1920 $this->_put('/BaseFont /'.$fontname.'');
1921 $this->_put('/CIDSystemInfo '.($this->n + 2).' 0 R');
1922 $this->_put('/FontDescriptor '.($this->n + 3).' 0 R');
1923 if (isset($font['desc']['MissingWidth'])){
1924 $this->_out('/DW '.$font['desc']['MissingWidth'].'');
1925 }
1926
1927 $this->_putTTfontwidths($font, $ttf->maxUni);
1928
1929 $this->_put('/CIDToGIDMap '.($this->n + 4).' 0 R');
1930 $this->_put('>>');
1931 $this->_put('endobj');
1932
1933 // ToUnicode
1934 $this->_newobj();
1935 $toUni = "/CIDInit /ProcSet findresource begin\n";
1936 $toUni .= "12 dict begin\n";
1937 $toUni .= "begincmap\n";
1938 $toUni .= "/CIDSystemInfo\n";
1939 $toUni .= "<</Registry (Adobe)\n";
1940 $toUni .= "/Ordering (UCS)\n";
1941 $toUni .= "/Supplement 0\n";
1942 $toUni .= ">> def\n";
1943 $toUni .= "/CMapName /Adobe-Identity-UCS def\n";
1944 $toUni .= "/CMapType 2 def\n";
1945 $toUni .= "1 begincodespacerange\n";
1946 $toUni .= "<0000> <FFFF>\n";
1947 $toUni .= "endcodespacerange\n";
1948 $toUni .= "1 beginbfrange\n";
1949 $toUni .= "<0000> <FFFF> <0000>\n";
1950 $toUni .= "endbfrange\n";
1951 $toUni .= "endcmap\n";
1952 $toUni .= "CMapName currentdict /CMap defineresource pop\n";
1953 $toUni .= "end\n";
1954 $toUni .= "end";
1955 $this->_put('<</Length '.(strlen($toUni)).'>>');
1956 $this->_putstream($toUni);
1957 $this->_put('endobj');
1958
1959 // CIDSystemInfo dictionary
1960 $this->_newobj();
1961 $this->_put('<</Registry (Adobe)');
1962 $this->_put('/Ordering (UCS)');
1963 $this->_put('/Supplement 0');
1964 $this->_put('>>');
1965 $this->_put('endobj');
1966
1967 // Font descriptor
1968 $this->_newobj();
1969 $this->_put('<</Type /FontDescriptor');
1970 $this->_put('/FontName /'.$fontname);
1971 foreach($font['desc'] as $kd=>$v) {
1972 if ($kd == 'Flags') { $v = $v | 4; $v = $v & ~32; } // SYMBOLIC font flag
1973 $this->_out(' /'.$kd.' '.$v);
1974 }
1975 $this->_put('/FontFile2 '.($this->n + 2).' 0 R');
1976 $this->_put('>>');
1977 $this->_put('endobj');
1978
1979 // Embed CIDToGIDMap
1980 // A specification of the mapping from CIDs to glyph indices
1981 $cidtogidmap = '';
1982 $cidtogidmap = str_pad('', 256*256*2, "\x00");
1983 foreach($codeToGlyph as $cc=>$glyph) {
1984 $cidtogidmap[$cc*2] = chr($glyph >> 8);
1985 $cidtogidmap[$cc*2 + 1] = chr($glyph & 0xFF);
1986 }
1987 $cidtogidmap = gzcompress($cidtogidmap);
1988 $this->_newobj();
1989 $this->_put('<</Length '.strlen($cidtogidmap).'');
1990 $this->_put('/Filter /FlateDecode');
1991 $this->_put('>>');
1992 $this->_putstream($cidtogidmap);
1993 $this->_put('endobj');
1994
1995 //Font file
1996 $this->_newobj();
1997 $this->_put('<</Length '.strlen($fontstream));
1998 $this->_put('/Filter /FlateDecode');
1999 $this->_put('/Length1 '.$ttfontsize);
2000 $this->_put('>>');
2001 $this->_putstream($fontstream);
2002 $this->_put('endobj');
2003 unset($ttf);
2004 }
2005 else
2006 {
2007 // Allow for additional types
2008 $this->fonts[$k]['n'] = $this->n+1;
2009 $mtd = '_put'.strtolower($type);
2010 if(!method_exists($this,$mtd))
2011 $this->Error('Unsupported font type: '.$type);
2012 $this->$mtd($font);
2013 }
2014 }
2015}
2016
2017protected function _putTTfontwidths($font, $maxUni) {
2018 if (file_exists($font['unifilename'].'.cw127.php')) {
2019 include($font['unifilename'].'.cw127.php') ;
2020 $startcid = 128;
2021 }
2022 else {
2023 $rangeid = 0;
2024 $range = array();
2025 $prevcid = -2;
2026 $prevwidth = -1;
2027 $interval = false;
2028 $startcid = 1;
2029 }
2030 $cwlen = $maxUni + 1;
2031
2032 // for each character
2033 for ($cid=$startcid; $cid<$cwlen; $cid++) {
2034 if ($cid==128 && (!file_exists($font['unifilename'].'.cw127.php'))) {
2035 if (is_writable(dirname($this->fontpath.'unifont/x'))) {
2036 $fh = fopen($font['unifilename'].'.cw127.php',"wb");
2037 $cw127='<?php'."\n";
2038 $cw127.='$rangeid='.$rangeid.";\n";
2039 $cw127.='$prevcid='.$prevcid.";\n";
2040 $cw127.='$prevwidth='.$prevwidth.";\n";
2041 if ($interval) { $cw127.='$interval=true'.";\n"; }
2042 else { $cw127.='$interval=false'.";\n"; }
2043 $cw127.='$range='.var_export($range,true).";\n";
2044 $cw127.="?>";
2045 fwrite($fh,$cw127,strlen($cw127));
2046 fclose($fh);
2047 }
2048 }
2049 if ((!isset($font['cw'][$cid*2]) || !isset($font['cw'][$cid*2+1])) ||
2050 ($font['cw'][$cid*2] == "\00" && $font['cw'][$cid*2+1] == "\00")) { continue; }
2051
2052 $width = (ord($font['cw'][$cid*2]) << 8) + ord($font['cw'][$cid*2+1]);
2053 if ($width == 65535) { $width = 0; }
2054 if ($cid > 255 && (!isset($font['subset'][$cid]) || !$font['subset'][$cid])) { continue; }
2055 if (!isset($font['dw']) || (isset($font['dw']) && $width != $font['dw'])) {
2056 if ($cid == ($prevcid + 1)) {
2057 if ($width == $prevwidth) {
2058 if ($width == $range[$rangeid][0]) {
2059 $range[$rangeid][] = $width;
2060 }
2061 else {
2062 array_pop($range[$rangeid]);
2063 // new range
2065 $range[$rangeid] = array();
2067 $range[$rangeid][] = $width;
2068 }
2069 $interval = true;
2070 $range[$rangeid]['interval'] = true;
2071 } else {
2072 if ($interval) {
2073 // new range
2074 $rangeid = $cid;
2075 $range[$rangeid] = array();
2076 $range[$rangeid][] = $width;
2077 }
2078 else { $range[$rangeid][] = $width; }
2079 $interval = false;
2080 }
2081 } else {
2082 $rangeid = $cid;
2083 $range[$rangeid] = array();
2084 $range[$rangeid][] = $width;
2085 $interval = false;
2086 }
2087 $prevcid = $cid;
2089 }
2090 }
2091 $prevk = -1;
2092 $nextk = -1;
2093 $prevint = false;
2094 foreach ($range as $k => $ws) {
2095 $cws = count($ws);
2096 if (($k == $nextk) AND (!$prevint) AND ((!isset($ws['interval'])) OR ($cws < 4))) {
2097 if (isset($range[$k]['interval'])) { unset($range[$k]['interval']); }
2098 $range[$prevk] = array_merge($range[$prevk], $range[$k]);
2099 unset($range[$k]);
2100 }
2101 else { $prevk = $k; }
2102 $nextk = $k + $cws;
2103 if (isset($ws['interval'])) {
2104 if ($cws > 3) { $prevint = true; }
2105 else { $prevint = false; }
2106 unset($range[$k]['interval']);
2107 --$nextk;
2108 }
2109 else { $prevint = false; }
2110 }
2111 $w = '';
2112 foreach ($range as $k => $ws) {
2113 if (count(array_count_values($ws)) == 1) { $w .= ' '.$k.' '.($k + count($ws) - 1).' '.$ws[0]; }
2114 else { $w .= ' '.$k.' [ '.implode(' ', $ws).' ]' . "\n"; }
2115 }
2116 $this->_out('/W ['.$w.' ]');
2117}
2118
2119protected function _tounicodecmap($uv)
2120{
2121 $ranges = '';
2122 $nbr = 0;
2123 $chars = '';
2124 $nbc = 0;
2125 foreach($uv as $c=>$v)
2126 {
2127 if(is_array($v))
2128 {
2129 $ranges .= sprintf("<%02X> <%02X> <%04X>\n",$c,$c+$v[1]-1,$v[0]);
2130 $nbr++;
2131 }
2132 else
2133 {
2134 $chars .= sprintf("<%02X> <%04X>\n",$c,$v);
2135 $nbc++;
2136 }
2137 }
2138 $s = "/CIDInit /ProcSet findresource begin\n";
2139 $s .= "12 dict begin\n";
2140 $s .= "begincmap\n";
2141 $s .= "/CIDSystemInfo\n";
2142 $s .= "<</Registry (Adobe)\n";
2143 $s .= "/Ordering (UCS)\n";
2144 $s .= "/Supplement 0\n";
2145 $s .= ">> def\n";
2146 $s .= "/CMapName /Adobe-Identity-UCS def\n";
2147 $s .= "/CMapType 2 def\n";
2148 $s .= "1 begincodespacerange\n";
2149 $s .= "<00> <FF>\n";
2150 $s .= "endcodespacerange\n";
2151 if($nbr>0)
2152 {
2153 $s .= "$nbr beginbfrange\n";
2154 $s .= $ranges;
2155 $s .= "endbfrange\n";
2156 }
2157 if($nbc>0)
2158 {
2159 $s .= "$nbc beginbfchar\n";
2160 $s .= $chars;
2161 $s .= "endbfchar\n";
2162 }
2163 $s .= "endcmap\n";
2164 $s .= "CMapName currentdict /CMap defineresource pop\n";
2165 $s .= "end\n";
2166 $s .= "end";
2167 return $s;
2168}
2169
2170protected function _putimages()
2171{
2172 foreach(array_keys($this->images) as $file)
2173 {
2174 $this->_putimage($this->images[$file]);
2175 unset($this->images[$file]['data']);
2176 unset($this->images[$file]['smask']);
2177 }
2178}
2179
2180protected function _putimage(&$info)
2181{
2182 $this->_newobj();
2183 $info['n'] = $this->n;
2184 $this->_put('<</Type /XObject');
2185 $this->_put('/Subtype /Image');
2186 $this->_put('/Width '.$info['w']);
2187 $this->_put('/Height '.$info['h']);
2188 if($info['cs']=='Indexed')
2189 $this->_put('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
2190 else
2191 {
2192 $this->_put('/ColorSpace /'.$info['cs']);
2193 if($info['cs']=='DeviceCMYK')
2194 $this->_put('/Decode [1 0 1 0 1 0 1 0]');
2195 }
2196 $this->_put('/BitsPerComponent '.$info['bpc']);
2197 if(isset($info['f']))
2198 $this->_put('/Filter /'.$info['f']);
2199 if(isset($info['dp']))
2200 $this->_put('/DecodeParms <<'.$info['dp'].'>>');
2201 if(isset($info['trns']) && is_array($info['trns']))
2202 {
2203 $trns = '';
2204 for($i=0;$i<count($info['trns']);$i++)
2205 $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
2206 $this->_put('/Mask ['.$trns.']');
2207 }
2208 if(isset($info['smask']))
2209 $this->_put('/SMask '.($this->n+1).' 0 R');
2210 $this->_put('/Length '.strlen($info['data']).'>>');
2211 $this->_putstream($info['data']);
2212 $this->_put('endobj');
2213 // Soft mask
2214 if(isset($info['smask']))
2215 {
2216 $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w'];
2217 $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']);
2218 $this->_putimage($smask);
2219 }
2220 // Palette
2221 if($info['cs']=='Indexed')
2222 $this->_putstreamobject($info['pal']);
2223}
2224
2225protected function _putxobjectdict()
2226{
2227 foreach($this->images as $image)
2228 $this->_put('/I'.$image['i'].' '.$image['n'].' 0 R');
2229}
2230
2231protected function _putresourcedict()
2232{
2233 $this->_put('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
2234 $this->_put('/Font <<');
2235 foreach($this->fonts as $font)
2236 $this->_put('/F'.$font['i'].' '.$font['n'].' 0 R');
2237 $this->_put('>>');
2238 $this->_put('/XObject <<');
2239 $this->_putxobjectdict();
2240 $this->_put('>>');
2241}
2242
2243protected function _putresources()
2244{
2245 $this->_putfonts();
2246 $this->_putimages();
2247 // Resource dictionary
2248 $this->_newobj(2);
2249 $this->_put('<<');
2250 $this->_putresourcedict();
2251 $this->_put('>>');
2252 $this->_put('endobj');
2253}
2254
2255protected function _putinfo()
2256{
2257 $date = @date('YmdHisO',$this->CreationDate);
2258 $this->metadata['CreationDate'] = 'D:'.substr($date,0,-2)."'".substr($date,-2)."'";
2259 foreach($this->metadata as $key=>$value)
2260 $this->_put('/'.$key.' '.$this->_textstring($value));
2261}
2262
2263protected function _putcatalog()
2264{
2265 $n = $this->PageInfo[1]['n'];
2266 $this->_put('/Type /Catalog');
2267 $this->_put('/Pages 1 0 R');
2268 if($this->ZoomMode=='fullpage')
2269 $this->_put('/OpenAction ['.$n.' 0 R /Fit]');
2270 elseif($this->ZoomMode=='fullwidth')
2271 $this->_put('/OpenAction ['.$n.' 0 R /FitH null]');
2272 elseif($this->ZoomMode=='real')
2273 $this->_put('/OpenAction ['.$n.' 0 R /XYZ null null 1]');
2274 elseif(!is_string($this->ZoomMode))
2275 $this->_put('/OpenAction ['.$n.' 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']');
2276 if($this->LayoutMode=='single')
2277 $this->_put('/PageLayout /SinglePage');
2278 elseif($this->LayoutMode=='continuous')
2279 $this->_put('/PageLayout /OneColumn');
2280 elseif($this->LayoutMode=='two')
2281 $this->_put('/PageLayout /TwoColumnLeft');
2282}
2283
2284protected function _putheader()
2285{
2286 $this->_put('%PDF-'.$this->PDFVersion);
2287}
2288
2289protected function _puttrailer()
2290{
2291 $this->_put('/Size '.($this->n+1));
2292 $this->_put('/Root '.$this->n.' 0 R');
2293 $this->_put('/Info '.($this->n-1).' 0 R');
2294}
2295
2296protected function _enddoc()
2297{
2298 $this->CreationDate = time();
2299 $this->_putheader();
2300 $this->_putpages();
2301 $this->_putresources();
2302 // Info
2303 $this->_newobj();
2304 $this->_put('<<');
2305 $this->_putinfo();
2306 $this->_put('>>');
2307 $this->_put('endobj');
2308 // Catalog
2309 $this->_newobj();
2310 $this->_put('<<');
2311 $this->_putcatalog();
2312 $this->_put('>>');
2313 $this->_put('endobj');
2314 // Cross-ref
2315 $offset = $this->_getoffset();
2316 $this->_put('xref');
2317 $this->_put('0 '.($this->n+1));
2318 $this->_put('0000000000 65535 f ');
2319 for($i=1;$i<=$this->n;$i++)
2320 $this->_put(sprintf('%010d 00000 n ',$this->offsets[$i]));
2321 // Trailer
2322 $this->_put('trailer');
2323 $this->_put('<<');
2324 $this->_puttrailer();
2325 $this->_put('>>');
2326 $this->_put('startxref');
2327 $this->_put($offset);
2328 $this->_put('%%EOF');
2329 $this->state = 3;
2330}
2331
2332// ********* NEW FUNCTIONS *********
2333// Converts UTF-8 strings to UTF16-BE.
2334protected function UTF8ToUTF16BE($str, $setbom=true) {
2335 $outstr = "";
2336 if ($setbom) {
2337 $outstr .= "\xFE\xFF"; // Byte Order Mark (BOM)
2338 }
2339 $outstr .= mb_convert_encoding($str, 'UTF-16BE', 'UTF-8');
2340 return $outstr;
2341}
2342
2343// Converts UTF-8 strings to codepoints array
2344protected function UTF8StringToArray($str) {
2345 $out = array();
2346 $len = strlen($str);
2347 for ($i = 0; $i < $len; $i++) {
2348 $uni = -1;
2349 $h = ord($str[$i]);
2350 if ( $h <= 0x7F )
2351 $uni = $h;
2352 elseif ( $h >= 0xC2 ) {
2353 if ( ($h <= 0xDF) && ($i < $len -1) )
2354 $uni = ($h & 0x1F) << 6 | (ord($str[++$i]) & 0x3F);
2355 elseif ( ($h <= 0xEF) && ($i < $len -2) )
2356 $uni = ($h & 0x0F) << 12 | (ord($str[++$i]) & 0x3F) << 6
2357 | (ord($str[++$i]) & 0x3F);
2358 elseif ( ($h <= 0xF4) && ($i < $len -3) )
2359 $uni = ($h & 0x0F) << 18 | (ord($str[++$i]) & 0x3F) << 12
2360 | (ord($str[++$i]) & 0x3F) << 6
2361 | (ord($str[++$i]) & 0x3F);
2362 }
2363 if ($uni >= 0) {
2364 $out[] = $uni;
2365 }
2366 }
2367 return $out;
2368}
2369
2370}
2371?>
p($p_string)
Definition: ac_common.php:39
if(!headers_sent()) else
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$op
Definition: ajax_admin.php:38
h( $row[ 'oa_description'])
if(!headers_sent())
– pour utiliser unoconv démarrer un server libreoffice commande libreoffice –headless –accept="socket...
foreach($array as $idx=> $m) $w
$input_from type
Definition: balance.inc.php:65
$out
Definition: cfgtags.inc.php:91
$up
Definition: courier.php:4
$ut
Definition: courier.php:5
while(!feof( $file)) echo fread( $file
if(count($array)==0) $size
$width
$str
Definition: fiche.inc.php:91
$cw
Definition: helvetica.php:6
if( $delta< 0) elseif( $delta==0)
vous n
Definition: modele.inc.php:398