Interim autoloaded library/classes via composer classmap, take 4. (#422)
[openemr.git] / library / classes / fpdf / fpdf.php
blobbf92be1fbbffa383b73ff214c789d72bf2d9be18
1 <?php
2 /*******************************************************************************
3 * FPDF *
4 * *
5 * Version: 1.81 *
6 * Date: 2015-12-20 *
7 * Author: Olivier PLATHEY *
8 *******************************************************************************/
10 // Modified by OpenEMR to work with the Barcode class
11 // Needed to make $k, $w, and $h var.
12 // Thus was unable to bring this in via composer since modified.
14 define('FPDF_VERSION','1.81');
16 class FPDF
18 protected $page; // current page number
19 protected $n; // current object number
20 protected $offsets; // array of object offsets
21 protected $buffer; // buffer holding in-memory PDF
22 protected $pages; // array containing pages
23 protected $state; // current document state
24 protected $compress; // compression flag
25 var $k; // scale factor (number of points in user unit)
26 protected $DefOrientation; // default orientation
27 protected $CurOrientation; // current orientation
28 protected $StdPageSizes; // standard page sizes
29 protected $DefPageSize; // default page size
30 protected $CurPageSize; // current page size
31 protected $CurRotation; // current page rotation
32 protected $PageInfo; // page-related data
33 protected $wPt, $hPt; // dimensions of current page in points
34 var $w, $h; // dimensions of current page in user unit
35 protected $lMargin; // left margin
36 protected $tMargin; // top margin
37 protected $rMargin; // right margin
38 protected $bMargin; // page break margin
39 protected $cMargin; // cell margin
40 protected $x, $y; // current position in user unit
41 protected $lasth; // height of last printed cell
42 protected $LineWidth; // line width in user unit
43 protected $fontpath; // path containing fonts
44 protected $CoreFonts; // array of core font names
45 protected $fonts; // array of used fonts
46 protected $FontFiles; // array of font files
47 protected $encodings; // array of encodings
48 protected $cmaps; // array of ToUnicode CMaps
49 protected $FontFamily; // current font family
50 protected $FontStyle; // current font style
51 protected $underline; // underlining flag
52 protected $CurrentFont; // current font info
53 protected $FontSizePt; // current font size in points
54 protected $FontSize; // current font size in user unit
55 protected $DrawColor; // commands for drawing color
56 protected $FillColor; // commands for filling color
57 protected $TextColor; // commands for text color
58 protected $ColorFlag; // indicates whether fill and text colors are different
59 protected $WithAlpha; // indicates whether alpha channel is used
60 protected $ws; // word spacing
61 protected $images; // array of used images
62 protected $PageLinks; // array of links in pages
63 protected $links; // array of internal links
64 protected $AutoPageBreak; // automatic page breaking
65 protected $PageBreakTrigger; // threshold used to trigger page breaks
66 protected $InHeader; // flag set when processing header
67 protected $InFooter; // flag set when processing footer
68 protected $AliasNbPages; // alias for total number of pages
69 protected $ZoomMode; // zoom display mode
70 protected $LayoutMode; // layout display mode
71 protected $metadata; // document properties
72 protected $PDFVersion; // PDF version number
74 /*******************************************************************************
75 * Public methods *
76 *******************************************************************************/
78 function __construct($orientation='P', $unit='mm', $size='A4')
80 // Some checks
81 $this->_dochecks();
82 // Initialization of properties
83 $this->state = 0;
84 $this->page = 0;
85 $this->n = 2;
86 $this->buffer = '';
87 $this->pages = array();
88 $this->PageInfo = array();
89 $this->fonts = array();
90 $this->FontFiles = array();
91 $this->encodings = array();
92 $this->cmaps = array();
93 $this->images = array();
94 $this->links = array();
95 $this->InHeader = false;
96 $this->InFooter = false;
97 $this->lasth = 0;
98 $this->FontFamily = '';
99 $this->FontStyle = '';
100 $this->FontSizePt = 12;
101 $this->underline = false;
102 $this->DrawColor = '0 G';
103 $this->FillColor = '0 g';
104 $this->TextColor = '0 g';
105 $this->ColorFlag = false;
106 $this->WithAlpha = false;
107 $this->ws = 0;
108 // Font path
109 if(defined('FPDF_FONTPATH'))
111 $this->fontpath = FPDF_FONTPATH;
112 if(substr($this->fontpath,-1)!='/' && substr($this->fontpath,-1)!='\\')
113 $this->fontpath .= '/';
115 elseif(is_dir(dirname(__FILE__).'/font'))
116 $this->fontpath = dirname(__FILE__).'/font/';
117 else
118 $this->fontpath = '';
119 // Core fonts
120 $this->CoreFonts = array('courier', 'helvetica', 'times', 'symbol', 'zapfdingbats');
121 // Scale factor
122 if($unit=='pt')
123 $this->k = 1;
124 elseif($unit=='mm')
125 $this->k = 72/25.4;
126 elseif($unit=='cm')
127 $this->k = 72/2.54;
128 elseif($unit=='in')
129 $this->k = 72;
130 else
131 $this->Error('Incorrect unit: '.$unit);
132 // Page sizes
133 $this->StdPageSizes = array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
134 'letter'=>array(612,792), 'legal'=>array(612,1008));
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')
142 $this->DefOrientation = 'P';
143 $this->w = $size[0];
144 $this->h = $size[1];
146 elseif($orientation=='l' || $orientation=='landscape')
148 $this->DefOrientation = 'L';
149 $this->w = $size[1];
150 $this->h = $size[0];
152 else
153 $this->Error('Incorrect orientation: '.$orientation);
154 $this->CurOrientation = $this->DefOrientation;
155 $this->wPt = $this->w*$this->k;
156 $this->hPt = $this->h*$this->k;
157 // Page rotation
158 $this->CurRotation = 0;
159 // Page margins (1 cm)
160 $margin = 28.35/$this->k;
161 $this->SetMargins($margin,$margin);
162 // Interior cell margin (1 mm)
163 $this->cMargin = $margin/10;
164 // Line width (0.2 mm)
165 $this->LineWidth = .567/$this->k;
166 // Automatic page break
167 $this->SetAutoPageBreak(true,2*$margin);
168 // Default display mode
169 $this->SetDisplayMode('default');
170 // Enable compression
171 $this->SetCompression(true);
172 // Set default PDF version number
173 $this->PDFVersion = '1.3';
176 function SetMargins($left, $top, $right=null)
178 // Set left, top and right margins
179 $this->lMargin = $left;
180 $this->tMargin = $top;
181 if($right===null)
182 $right = $left;
183 $this->rMargin = $right;
186 function SetLeftMargin($margin)
188 // Set left margin
189 $this->lMargin = $margin;
190 if($this->page>0 && $this->x<$margin)
191 $this->x = $margin;
194 function SetTopMargin($margin)
196 // Set top margin
197 $this->tMargin = $margin;
200 function SetRightMargin($margin)
202 // Set right margin
203 $this->rMargin = $margin;
206 function SetAutoPageBreak($auto, $margin=0)
208 // Set auto page break mode and triggering margin
209 $this->AutoPageBreak = $auto;
210 $this->bMargin = $margin;
211 $this->PageBreakTrigger = $this->h-$margin;
214 function SetDisplayMode($zoom, $layout='default')
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->Error('Incorrect zoom display mode: '.$zoom);
221 if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
222 $this->LayoutMode = $layout;
223 else
224 $this->Error('Incorrect layout display mode: '.$layout);
227 function SetCompression($compress)
229 // Set page compression
230 if(function_exists('gzcompress'))
231 $this->compress = $compress;
232 else
233 $this->compress = false;
236 function SetTitle($title, $isUTF8=false)
238 // Title of document
239 $this->metadata['Title'] = $isUTF8 ? $title : utf8_encode($title);
242 function SetAuthor($author, $isUTF8=false)
244 // Author of document
245 $this->metadata['Author'] = $isUTF8 ? $author : utf8_encode($author);
248 function SetSubject($subject, $isUTF8=false)
250 // Subject of document
251 $this->metadata['Subject'] = $isUTF8 ? $subject : utf8_encode($subject);
254 function SetKeywords($keywords, $isUTF8=false)
256 // Keywords of document
257 $this->metadata['Keywords'] = $isUTF8 ? $keywords : utf8_encode($keywords);
260 function SetCreator($creator, $isUTF8=false)
262 // Creator of document
263 $this->metadata['Creator'] = $isUTF8 ? $creator : utf8_encode($creator);
266 function AliasNbPages($alias='{nb}')
268 // Define an alias for total number of pages
269 $this->AliasNbPages = $alias;
272 function Error($msg)
274 // Fatal error
275 throw new Exception('FPDF error: '.$msg);
278 function Close()
280 // Terminate document
281 if($this->state==3)
282 return;
283 if($this->page==0)
284 $this->AddPage();
285 // Page footer
286 $this->InFooter = true;
287 $this->Footer();
288 $this->InFooter = false;
289 // Close page
290 $this->_endpage();
291 // Close document
292 $this->_enddoc();
295 function AddPage($orientation='', $size='', $rotation=0)
297 // Start a new page
298 if($this->state==3)
299 $this->Error('The document is closed');
300 $family = $this->FontFamily;
301 $style = $this->FontStyle.($this->underline ? 'U' : '');
302 $fontsize = $this->FontSizePt;
303 $lw = $this->LineWidth;
304 $dc = $this->DrawColor;
305 $fc = $this->FillColor;
306 $tc = $this->TextColor;
307 $cf = $this->ColorFlag;
308 if($this->page>0)
310 // Page footer
311 $this->InFooter = true;
312 $this->Footer();
313 $this->InFooter = false;
314 // Close page
315 $this->_endpage();
317 // Start new page
318 $this->_beginpage($orientation,$size,$rotation);
319 // Set line cap style to square
320 $this->_out('2 J');
321 // Set line width
322 $this->LineWidth = $lw;
323 $this->_out(sprintf('%.2F w',$lw*$this->k));
324 // Set font
325 if($family)
326 $this->SetFont($family,$style,$fontsize);
327 // Set colors
328 $this->DrawColor = $dc;
329 if($dc!='0 G')
330 $this->_out($dc);
331 $this->FillColor = $fc;
332 if($fc!='0 g')
333 $this->_out($fc);
334 $this->TextColor = $tc;
335 $this->ColorFlag = $cf;
336 // Page header
337 $this->InHeader = true;
338 $this->Header();
339 $this->InHeader = false;
340 // Restore line width
341 if($this->LineWidth!=$lw)
343 $this->LineWidth = $lw;
344 $this->_out(sprintf('%.2F w',$lw*$this->k));
346 // Restore font
347 if($family)
348 $this->SetFont($family,$style,$fontsize);
349 // Restore colors
350 if($this->DrawColor!=$dc)
352 $this->DrawColor = $dc;
353 $this->_out($dc);
355 if($this->FillColor!=$fc)
357 $this->FillColor = $fc;
358 $this->_out($fc);
360 $this->TextColor = $tc;
361 $this->ColorFlag = $cf;
364 function Header()
366 // To be implemented in your own inherited class
369 function Footer()
371 // To be implemented in your own inherited class
374 function PageNo()
376 // Get current page number
377 return $this->page;
380 function SetDrawColor($r, $g=null, $b=null)
382 // Set color for all stroking operations
383 if(($r==0 && $g==0 && $b==0) || $g===null)
384 $this->DrawColor = sprintf('%.3F G',$r/255);
385 else
386 $this->DrawColor = sprintf('%.3F %.3F %.3F RG',$r/255,$g/255,$b/255);
387 if($this->page>0)
388 $this->_out($this->DrawColor);
391 function SetFillColor($r, $g=null, $b=null)
393 // Set color for all filling operations
394 if(($r==0 && $g==0 && $b==0) || $g===null)
395 $this->FillColor = sprintf('%.3F g',$r/255);
396 else
397 $this->FillColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
398 $this->ColorFlag = ($this->FillColor!=$this->TextColor);
399 if($this->page>0)
400 $this->_out($this->FillColor);
403 function SetTextColor($r, $g=null, $b=null)
405 // Set color for text
406 if(($r==0 && $g==0 && $b==0) || $g===null)
407 $this->TextColor = sprintf('%.3F g',$r/255);
408 else
409 $this->TextColor = sprintf('%.3F %.3F %.3F rg',$r/255,$g/255,$b/255);
410 $this->ColorFlag = ($this->FillColor!=$this->TextColor);
413 function GetStringWidth($s)
415 // Get width of a string in the current font
416 $s = (string)$s;
417 $cw = &$this->CurrentFont['cw'];
418 $w = 0;
419 $l = strlen($s);
420 for($i=0;$i<$l;$i++)
421 $w += $cw[$s[$i]];
422 return $w*$this->FontSize/1000;
425 function SetLineWidth($width)
427 // Set line width
428 $this->LineWidth = $width;
429 if($this->page>0)
430 $this->_out(sprintf('%.2F w',$width*$this->k));
433 function Line($x1, $y1, $x2, $y2)
435 // Draw a line
436 $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));
439 function Rect($x, $y, $w, $h, $style='')
441 // Draw a rectangle
442 if($style=='F')
443 $op = 'f';
444 elseif($style=='FD' || $style=='DF')
445 $op = 'B';
446 else
447 $op = 'S';
448 $this->_out(sprintf('%.2F %.2F %.2F %.2F re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
451 function AddFont($family, $style='', $file='')
453 // Add a TrueType, OpenType or Type1 font
454 $family = strtolower($family);
455 if($file=='')
456 $file = str_replace(' ','',$family).strtolower($style).'.php';
457 $style = strtoupper($style);
458 if($style=='IB')
459 $style = 'BI';
460 $fontkey = $family.$style;
461 if(isset($this->fonts[$fontkey]))
462 return;
463 $info = $this->_loadfont($file);
464 $info['i'] = count($this->fonts)+1;
465 if(!empty($info['file']))
467 // Embedded font
468 if($info['type']=='TrueType')
469 $this->FontFiles[$info['file']] = array('length1'=>$info['originalsize']);
470 else
471 $this->FontFiles[$info['file']] = array('length1'=>$info['size1'], 'length2'=>$info['size2']);
473 $this->fonts[$fontkey] = $info;
476 function SetFont($family, $style='', $size=0)
478 // Select a font; size given in points
479 if($family=='')
480 $family = $this->FontFamily;
481 else
482 $family = strtolower($family);
483 $style = strtoupper($style);
484 if(strpos($style,'U')!==false)
486 $this->underline = true;
487 $style = str_replace('U','',$style);
489 else
490 $this->underline = false;
491 if($style=='IB')
492 $style = 'BI';
493 if($size==0)
494 $size = $this->FontSizePt;
495 // Test if font is already selected
496 if($this->FontFamily==$family && $this->FontStyle==$style && $this->FontSizePt==$size)
497 return;
498 // Test if font is already loaded
499 $fontkey = $family.$style;
500 if(!isset($this->fonts[$fontkey]))
502 // Test if one of the core fonts
503 if($family=='arial')
504 $family = 'helvetica';
505 if(in_array($family,$this->CoreFonts))
507 if($family=='symbol' || $family=='zapfdingbats')
508 $style = '';
509 $fontkey = $family.$style;
510 if(!isset($this->fonts[$fontkey]))
511 $this->AddFont($family,$style);
513 else
514 $this->Error('Undefined font: '.$family.' '.$style);
516 // Select it
517 $this->FontFamily = $family;
518 $this->FontStyle = $style;
519 $this->FontSizePt = $size;
520 $this->FontSize = $size/$this->k;
521 $this->CurrentFont = &$this->fonts[$fontkey];
522 if($this->page>0)
523 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
526 function SetFontSize($size)
528 // Set font size in points
529 if($this->FontSizePt==$size)
530 return;
531 $this->FontSizePt = $size;
532 $this->FontSize = $size/$this->k;
533 if($this->page>0)
534 $this->_out(sprintf('BT /F%d %.2F Tf ET',$this->CurrentFont['i'],$this->FontSizePt));
537 function AddLink()
539 // Create a new internal link
540 $n = count($this->links)+1;
541 $this->links[$n] = array(0, 0);
542 return $n;
545 function SetLink($link, $y=0, $page=-1)
547 // Set destination of internal link
548 if($y==-1)
549 $y = $this->y;
550 if($page==-1)
551 $page = $this->page;
552 $this->links[$link] = array($page, $y);
555 function Link($x, $y, $w, $h, $link)
557 // Put a link on the page
558 $this->PageLinks[$this->page][] = array($x*$this->k, $this->hPt-$y*$this->k, $w*$this->k, $h*$this->k, $link);
561 function Text($x, $y, $txt)
563 // Output a string
564 if(!isset($this->CurrentFont))
565 $this->Error('No font has been set');
566 $s = sprintf('BT %.2F %.2F Td (%s) Tj ET',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
567 if($this->underline && $txt!='')
568 $s .= ' '.$this->_dounderline($x,$y,$txt);
569 if($this->ColorFlag)
570 $s = 'q '.$this->TextColor.' '.$s.' Q';
571 $this->_out($s);
574 function AcceptPageBreak()
576 // Accept automatic page break or not
577 return $this->AutoPageBreak;
580 function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
582 // Output a cell
583 $k = $this->k;
584 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
586 // Automatic page break
587 $x = $this->x;
588 $ws = $this->ws;
589 if($ws>0)
591 $this->ws = 0;
592 $this->_out('0 Tw');
594 $this->AddPage($this->CurOrientation,$this->CurPageSize,$this->CurRotation);
595 $this->x = $x;
596 if($ws>0)
598 $this->ws = $ws;
599 $this->_out(sprintf('%.3F Tw',$ws*$k));
602 if($w==0)
603 $w = $this->w-$this->rMargin-$this->x;
604 $s = '';
605 if($fill || $border==1)
607 if($fill)
608 $op = ($border==1) ? 'B' : 'f';
609 else
610 $op = 'S';
611 $s = sprintf('%.2F %.2F %.2F %.2F re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
613 if(is_string($border))
615 $x = $this->x;
616 $y = $this->y;
617 if(strpos($border,'L')!==false)
618 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
619 if(strpos($border,'T')!==false)
620 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
621 if(strpos($border,'R')!==false)
622 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
623 if(strpos($border,'B')!==false)
624 $s .= sprintf('%.2F %.2F m %.2F %.2F l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
626 if($txt!=='')
628 if(!isset($this->CurrentFont))
629 $this->Error('No font has been set');
630 if($align=='R')
631 $dx = $w-$this->cMargin-$this->GetStringWidth($txt);
632 elseif($align=='C')
633 $dx = ($w-$this->GetStringWidth($txt))/2;
634 else
635 $dx = $this->cMargin;
636 if($this->ColorFlag)
637 $s .= 'q '.$this->TextColor.' ';
638 $s .= sprintf('BT %.2F %.2F Td (%s) Tj ET',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,$this->_escape($txt));
639 if($this->underline)
640 $s .= ' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
641 if($this->ColorFlag)
642 $s .= ' Q';
643 if($link)
644 $this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$this->GetStringWidth($txt),$this->FontSize,$link);
646 if($s)
647 $this->_out($s);
648 $this->lasth = $h;
649 if($ln>0)
651 // Go to next line
652 $this->y += $h;
653 if($ln==1)
654 $this->x = $this->lMargin;
656 else
657 $this->x += $w;
660 function MultiCell($w, $h, $txt, $border=0, $align='J', $fill=false)
662 // Output text with automatic or explicit line breaks
663 if(!isset($this->CurrentFont))
664 $this->Error('No font has been set');
665 $cw = &$this->CurrentFont['cw'];
666 if($w==0)
667 $w = $this->w-$this->rMargin-$this->x;
668 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
669 $s = str_replace("\r",'',$txt);
670 $nb = strlen($s);
671 if($nb>0 && $s[$nb-1]=="\n")
672 $nb--;
673 $b = 0;
674 if($border)
676 if($border==1)
678 $border = 'LTRB';
679 $b = 'LRT';
680 $b2 = 'LR';
682 else
684 $b2 = '';
685 if(strpos($border,'L')!==false)
686 $b2 .= 'L';
687 if(strpos($border,'R')!==false)
688 $b2 .= 'R';
689 $b = (strpos($border,'T')!==false) ? $b2.'T' : $b2;
692 $sep = -1;
693 $i = 0;
694 $j = 0;
695 $l = 0;
696 $ns = 0;
697 $nl = 1;
698 while($i<$nb)
700 // Get next character
701 $c = $s[$i];
702 if($c=="\n")
704 // Explicit line break
705 if($this->ws>0)
707 $this->ws = 0;
708 $this->_out('0 Tw');
710 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
711 $i++;
712 $sep = -1;
713 $j = $i;
714 $l = 0;
715 $ns = 0;
716 $nl++;
717 if($border && $nl==2)
718 $b = $b2;
719 continue;
721 if($c==' ')
723 $sep = $i;
724 $ls = $l;
725 $ns++;
727 $l += $cw[$c];
728 if($l>$wmax)
730 // Automatic line break
731 if($sep==-1)
733 if($i==$j)
734 $i++;
735 if($this->ws>0)
737 $this->ws = 0;
738 $this->_out('0 Tw');
740 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
742 else
744 if($align=='J')
746 $this->ws = ($ns>1) ? ($wmax-$ls)/1000*$this->FontSize/($ns-1) : 0;
747 $this->_out(sprintf('%.3F Tw',$this->ws*$this->k));
749 $this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
750 $i = $sep+1;
752 $sep = -1;
753 $j = $i;
754 $l = 0;
755 $ns = 0;
756 $nl++;
757 if($border && $nl==2)
758 $b = $b2;
760 else
761 $i++;
763 // Last chunk
764 if($this->ws>0)
766 $this->ws = 0;
767 $this->_out('0 Tw');
769 if($border && strpos($border,'B')!==false)
770 $b .= 'B';
771 $this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
772 $this->x = $this->lMargin;
775 function Write($h, $txt, $link='')
777 // Output text in flowing mode
778 if(!isset($this->CurrentFont))
779 $this->Error('No font has been set');
780 $cw = &$this->CurrentFont['cw'];
781 $w = $this->w-$this->rMargin-$this->x;
782 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
783 $s = str_replace("\r",'',$txt);
784 $nb = strlen($s);
785 $sep = -1;
786 $i = 0;
787 $j = 0;
788 $l = 0;
789 $nl = 1;
790 while($i<$nb)
792 // Get next character
793 $c = $s[$i];
794 if($c=="\n")
796 // Explicit line break
797 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',false,$link);
798 $i++;
799 $sep = -1;
800 $j = $i;
801 $l = 0;
802 if($nl==1)
804 $this->x = $this->lMargin;
805 $w = $this->w-$this->rMargin-$this->x;
806 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
808 $nl++;
809 continue;
811 if($c==' ')
812 $sep = $i;
813 $l += $cw[$c];
814 if($l>$wmax)
816 // Automatic line break
817 if($sep==-1)
819 if($this->x>$this->lMargin)
821 // Move to next line
822 $this->x = $this->lMargin;
823 $this->y += $h;
824 $w = $this->w-$this->rMargin-$this->x;
825 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
826 $i++;
827 $nl++;
828 continue;
830 if($i==$j)
831 $i++;
832 $this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',false,$link);
834 else
836 $this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',false,$link);
837 $i = $sep+1;
839 $sep = -1;
840 $j = $i;
841 $l = 0;
842 if($nl==1)
844 $this->x = $this->lMargin;
845 $w = $this->w-$this->rMargin-$this->x;
846 $wmax = ($w-2*$this->cMargin)*1000/$this->FontSize;
848 $nl++;
850 else
851 $i++;
853 // Last chunk
854 if($i!=$j)
855 $this->Cell($l/1000*$this->FontSize,$h,substr($s,$j),0,0,'',false,$link);
858 function Ln($h=null)
860 // Line feed; default value is the last cell height
861 $this->x = $this->lMargin;
862 if($h===null)
863 $this->y += $this->lasth;
864 else
865 $this->y += $h;
868 function Image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
870 // Put an image on the page
871 if($file=='')
872 $this->Error('Image file name is empty');
873 if(!isset($this->images[$file]))
875 // First use of this image, get info
876 if($type=='')
878 $pos = strrpos($file,'.');
879 if(!$pos)
880 $this->Error('Image file has no extension and no type was specified: '.$file);
881 $type = substr($file,$pos+1);
883 $type = strtolower($type);
884 if($type=='jpeg')
885 $type = 'jpg';
886 $mtd = '_parse'.$type;
887 if(!method_exists($this,$mtd))
888 $this->Error('Unsupported image type: '.$type);
889 $info = $this->$mtd($file);
890 $info['i'] = count($this->images)+1;
891 $this->images[$file] = $info;
893 else
894 $info = $this->images[$file];
896 // Automatic width and height calculation if needed
897 if($w==0 && $h==0)
899 // Put image at 96 dpi
900 $w = -96;
901 $h = -96;
903 if($w<0)
904 $w = -$info['w']*72/$w/$this->k;
905 if($h<0)
906 $h = -$info['h']*72/$h/$this->k;
907 if($w==0)
908 $w = $h*$info['w']/$info['h'];
909 if($h==0)
910 $h = $w*$info['h']/$info['w'];
912 // Flowing mode
913 if($y===null)
915 if($this->y+$h>$this->PageBreakTrigger && !$this->InHeader && !$this->InFooter && $this->AcceptPageBreak())
917 // Automatic page break
918 $x2 = $this->x;
919 $this->AddPage($this->CurOrientation,$this->CurPageSize,$this->CurRotation);
920 $this->x = $x2;
922 $y = $this->y;
923 $this->y += $h;
926 if($x===null)
927 $x = $this->x;
928 $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']));
929 if($link)
930 $this->Link($x,$y,$w,$h,$link);
933 function GetPageWidth()
935 // Get current page width
936 return $this->w;
939 function GetPageHeight()
941 // Get current page height
942 return $this->h;
945 function GetX()
947 // Get x position
948 return $this->x;
951 function SetX($x)
953 // Set x position
954 if($x>=0)
955 $this->x = $x;
956 else
957 $this->x = $this->w+$x;
960 function GetY()
962 // Get y position
963 return $this->y;
966 function SetY($y, $resetX=true)
968 // Set y position and optionally reset x
969 if($y>=0)
970 $this->y = $y;
971 else
972 $this->y = $this->h+$y;
973 if($resetX)
974 $this->x = $this->lMargin;
977 function SetXY($x, $y)
979 // Set x and y positions
980 $this->SetX($x);
981 $this->SetY($y,false);
984 function Output($dest='', $name='', $isUTF8=false)
986 // Output PDF to some destination
987 $this->Close();
988 if(strlen($name)==1 && strlen($dest)!=1)
990 // Fix parameter order
991 $tmp = $dest;
992 $dest = $name;
993 $name = $tmp;
995 if($dest=='')
996 $dest = 'I';
997 if($name=='')
998 $name = 'doc.pdf';
999 switch(strtoupper($dest))
1001 case 'I':
1002 // Send to standard output
1003 $this->_checkoutput();
1004 if(PHP_SAPI!='cli')
1006 // We send to a browser
1007 header('Content-Type: application/pdf');
1008 header('Content-Disposition: inline; '.$this->_httpencode('filename',$name,$isUTF8));
1009 header('Cache-Control: private, max-age=0, must-revalidate');
1010 header('Pragma: public');
1012 echo $this->buffer;
1013 break;
1014 case 'D':
1015 // Download file
1016 $this->_checkoutput();
1017 header('Content-Type: application/x-download');
1018 header('Content-Disposition: attachment; '.$this->_httpencode('filename',$name,$isUTF8));
1019 header('Cache-Control: private, max-age=0, must-revalidate');
1020 header('Pragma: public');
1021 echo $this->buffer;
1022 break;
1023 case 'F':
1024 // Save to local file
1025 if(!file_put_contents($name,$this->buffer))
1026 $this->Error('Unable to create output file: '.$name);
1027 break;
1028 case 'S':
1029 // Return as a string
1030 return $this->buffer;
1031 default:
1032 $this->Error('Incorrect output destination: '.$dest);
1034 return '';
1037 /*******************************************************************************
1038 * Protected methods *
1039 *******************************************************************************/
1041 protected function _dochecks()
1043 // Check mbstring overloading
1044 if(ini_get('mbstring.func_overload') & 2)
1045 $this->Error('mbstring overloading must be disabled');
1046 // Ensure runtime magic quotes are disabled
1047 if(get_magic_quotes_runtime())
1048 @set_magic_quotes_runtime(0);
1051 protected function _checkoutput()
1053 if(PHP_SAPI!='cli')
1055 if(headers_sent($file,$line))
1056 $this->Error("Some data has already been output, can't send PDF file (output started at $file:$line)");
1058 if(ob_get_length())
1060 // The output buffer is not empty
1061 if(preg_match('/^(\xEF\xBB\xBF)?\s*$/',ob_get_contents()))
1063 // It contains only a UTF-8 BOM and/or whitespace, let's clean it
1064 ob_clean();
1066 else
1067 $this->Error("Some data has already been output, can't send PDF file");
1071 protected function _getpagesize($size)
1073 if(is_string($size))
1075 $size = strtolower($size);
1076 if(!isset($this->StdPageSizes[$size]))
1077 $this->Error('Unknown page size: '.$size);
1078 $a = $this->StdPageSizes[$size];
1079 return array($a[0]/$this->k, $a[1]/$this->k);
1081 else
1083 if($size[0]>$size[1])
1084 return array($size[1], $size[0]);
1085 else
1086 return $size;
1090 protected function _beginpage($orientation, $size, $rotation)
1092 $this->page++;
1093 $this->pages[$this->page] = '';
1094 $this->state = 2;
1095 $this->x = $this->lMargin;
1096 $this->y = $this->tMargin;
1097 $this->FontFamily = '';
1098 // Check page size and orientation
1099 if($orientation=='')
1100 $orientation = $this->DefOrientation;
1101 else
1102 $orientation = strtoupper($orientation[0]);
1103 if($size=='')
1104 $size = $this->DefPageSize;
1105 else
1106 $size = $this->_getpagesize($size);
1107 if($orientation!=$this->CurOrientation || $size[0]!=$this->CurPageSize[0] || $size[1]!=$this->CurPageSize[1])
1109 // New size or orientation
1110 if($orientation=='P')
1112 $this->w = $size[0];
1113 $this->h = $size[1];
1115 else
1117 $this->w = $size[1];
1118 $this->h = $size[0];
1120 $this->wPt = $this->w*$this->k;
1121 $this->hPt = $this->h*$this->k;
1122 $this->PageBreakTrigger = $this->h-$this->bMargin;
1123 $this->CurOrientation = $orientation;
1124 $this->CurPageSize = $size;
1126 if($orientation!=$this->DefOrientation || $size[0]!=$this->DefPageSize[0] || $size[1]!=$this->DefPageSize[1])
1127 $this->PageInfo[$this->page]['size'] = array($this->wPt, $this->hPt);
1128 if($rotation!=0)
1130 if($rotation%90!=0)
1131 $this->Error('Incorrect rotation value: '.$rotation);
1132 $this->CurRotation = $rotation;
1133 $this->PageInfo[$this->page]['rotation'] = $rotation;
1137 protected function _endpage()
1139 $this->state = 1;
1142 protected function _loadfont($font)
1144 // Load a font definition file from the font directory
1145 if(strpos($font,'/')!==false || strpos($font,"\\")!==false)
1146 $this->Error('Incorrect font definition file name: '.$font);
1147 include($this->fontpath.$font);
1148 if(!isset($name))
1149 $this->Error('Could not include font definition file');
1150 if(isset($enc))
1151 $enc = strtolower($enc);
1152 if(!isset($subsetted))
1153 $subsetted = false;
1154 return get_defined_vars();
1157 protected function _isascii($s)
1159 // Test if string is ASCII
1160 $nb = strlen($s);
1161 for($i=0;$i<$nb;$i++)
1163 if(ord($s[$i])>127)
1164 return false;
1166 return true;
1169 protected function _httpencode($param, $value, $isUTF8)
1171 // Encode HTTP header field parameter
1172 if($this->_isascii($value))
1173 return $param.'="'.$value.'"';
1174 if(!$isUTF8)
1175 $value = utf8_encode($value);
1176 if(strpos($_SERVER['HTTP_USER_AGENT'],'MSIE')!==false)
1177 return $param.'="'.rawurlencode($value).'"';
1178 else
1179 return $param."*=UTF-8''".rawurlencode($value);
1182 protected function _UTF8toUTF16($s)
1184 // Convert UTF-8 to UTF-16BE with BOM
1185 $res = "\xFE\xFF";
1186 $nb = strlen($s);
1187 $i = 0;
1188 while($i<$nb)
1190 $c1 = ord($s[$i++]);
1191 if($c1>=224)
1193 // 3-byte character
1194 $c2 = ord($s[$i++]);
1195 $c3 = ord($s[$i++]);
1196 $res .= chr((($c1 & 0x0F)<<4) + (($c2 & 0x3C)>>2));
1197 $res .= chr((($c2 & 0x03)<<6) + ($c3 & 0x3F));
1199 elseif($c1>=192)
1201 // 2-byte character
1202 $c2 = ord($s[$i++]);
1203 $res .= chr(($c1 & 0x1C)>>2);
1204 $res .= chr((($c1 & 0x03)<<6) + ($c2 & 0x3F));
1206 else
1208 // Single-byte character
1209 $res .= "\0".chr($c1);
1212 return $res;
1215 protected function _escape($s)
1217 // Escape special characters
1218 if(strpos($s,'(')!==false || strpos($s,')')!==false || strpos($s,'\\')!==false || strpos($s,"\r")!==false)
1219 return str_replace(array('\\','(',')',"\r"), array('\\\\','\\(','\\)','\\r'), $s);
1220 else
1221 return $s;
1224 protected function _textstring($s)
1226 // Format a text string
1227 if(!$this->_isascii($s))
1228 $s = $this->_UTF8toUTF16($s);
1229 return '('.$this->_escape($s).')';
1232 protected function _dounderline($x, $y, $txt)
1234 // Underline text
1235 $up = $this->CurrentFont['up'];
1236 $ut = $this->CurrentFont['ut'];
1237 $w = $this->GetStringWidth($txt)+$this->ws*substr_count($txt,' ');
1238 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);
1241 protected function _parsejpg($file)
1243 // Extract info from a JPEG file
1244 $a = getimagesize($file);
1245 if(!$a)
1246 $this->Error('Missing or incorrect image file: '.$file);
1247 if($a[2]!=2)
1248 $this->Error('Not a JPEG file: '.$file);
1249 if(!isset($a['channels']) || $a['channels']==3)
1250 $colspace = 'DeviceRGB';
1251 elseif($a['channels']==4)
1252 $colspace = 'DeviceCMYK';
1253 else
1254 $colspace = 'DeviceGray';
1255 $bpc = isset($a['bits']) ? $a['bits'] : 8;
1256 $data = file_get_contents($file);
1257 return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'DCTDecode', 'data'=>$data);
1260 protected function _parsepng($file)
1262 // Extract info from a PNG file
1263 $f = fopen($file,'rb');
1264 if(!$f)
1265 $this->Error('Can\'t open image file: '.$file);
1266 $info = $this->_parsepngstream($f,$file);
1267 fclose($f);
1268 return $info;
1271 protected function _parsepngstream($f, $file)
1273 // Check signature
1274 if($this->_readstream($f,8)!=chr(137).'PNG'.chr(13).chr(10).chr(26).chr(10))
1275 $this->Error('Not a PNG file: '.$file);
1277 // Read header chunk
1278 $this->_readstream($f,4);
1279 if($this->_readstream($f,4)!='IHDR')
1280 $this->Error('Incorrect PNG file: '.$file);
1281 $w = $this->_readint($f);
1282 $h = $this->_readint($f);
1283 $bpc = ord($this->_readstream($f,1));
1284 if($bpc>8)
1285 $this->Error('16-bit depth not supported: '.$file);
1286 $ct = ord($this->_readstream($f,1));
1287 if($ct==0 || $ct==4)
1288 $colspace = 'DeviceGray';
1289 elseif($ct==2 || $ct==6)
1290 $colspace = 'DeviceRGB';
1291 elseif($ct==3)
1292 $colspace = 'Indexed';
1293 else
1294 $this->Error('Unknown color type: '.$file);
1295 if(ord($this->_readstream($f,1))!=0)
1296 $this->Error('Unknown compression method: '.$file);
1297 if(ord($this->_readstream($f,1))!=0)
1298 $this->Error('Unknown filter method: '.$file);
1299 if(ord($this->_readstream($f,1))!=0)
1300 $this->Error('Interlacing not supported: '.$file);
1301 $this->_readstream($f,4);
1302 $dp = '/Predictor 15 /Colors '.($colspace=='DeviceRGB' ? 3 : 1).' /BitsPerComponent '.$bpc.' /Columns '.$w;
1304 // Scan chunks looking for palette, transparency and image data
1305 $pal = '';
1306 $trns = '';
1307 $data = '';
1310 $n = $this->_readint($f);
1311 $type = $this->_readstream($f,4);
1312 if($type=='PLTE')
1314 // Read palette
1315 $pal = $this->_readstream($f,$n);
1316 $this->_readstream($f,4);
1318 elseif($type=='tRNS')
1320 // Read transparency info
1321 $t = $this->_readstream($f,$n);
1322 if($ct==0)
1323 $trns = array(ord(substr($t,1,1)));
1324 elseif($ct==2)
1325 $trns = array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
1326 else
1328 $pos = strpos($t,chr(0));
1329 if($pos!==false)
1330 $trns = array($pos);
1332 $this->_readstream($f,4);
1334 elseif($type=='IDAT')
1336 // Read image data block
1337 $data .= $this->_readstream($f,$n);
1338 $this->_readstream($f,4);
1340 elseif($type=='IEND')
1341 break;
1342 else
1343 $this->_readstream($f,$n+4);
1345 while($n);
1347 if($colspace=='Indexed' && empty($pal))
1348 $this->Error('Missing palette in '.$file);
1349 $info = array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'FlateDecode', 'dp'=>$dp, 'pal'=>$pal, 'trns'=>$trns);
1350 if($ct>=4)
1352 // Extract alpha channel
1353 if(!function_exists('gzuncompress'))
1354 $this->Error('Zlib not available, can\'t handle alpha channel: '.$file);
1355 $data = gzuncompress($data);
1356 $color = '';
1357 $alpha = '';
1358 if($ct==4)
1360 // Gray image
1361 $len = 2*$w;
1362 for($i=0;$i<$h;$i++)
1364 $pos = (1+$len)*$i;
1365 $color .= $data[$pos];
1366 $alpha .= $data[$pos];
1367 $line = substr($data,$pos+1,$len);
1368 $color .= preg_replace('/(.)./s','$1',$line);
1369 $alpha .= preg_replace('/.(.)/s','$1',$line);
1372 else
1374 // RGB image
1375 $len = 4*$w;
1376 for($i=0;$i<$h;$i++)
1378 $pos = (1+$len)*$i;
1379 $color .= $data[$pos];
1380 $alpha .= $data[$pos];
1381 $line = substr($data,$pos+1,$len);
1382 $color .= preg_replace('/(.{3})./s','$1',$line);
1383 $alpha .= preg_replace('/.{3}(.)/s','$1',$line);
1386 unset($data);
1387 $data = gzcompress($color);
1388 $info['smask'] = gzcompress($alpha);
1389 $this->WithAlpha = true;
1390 if($this->PDFVersion<'1.4')
1391 $this->PDFVersion = '1.4';
1393 $info['data'] = $data;
1394 return $info;
1397 protected function _readstream($f, $n)
1399 // Read n bytes from stream
1400 $res = '';
1401 while($n>0 && !feof($f))
1403 $s = fread($f,$n);
1404 if($s===false)
1405 $this->Error('Error while reading stream');
1406 $n -= strlen($s);
1407 $res .= $s;
1409 if($n>0)
1410 $this->Error('Unexpected end of stream');
1411 return $res;
1414 protected function _readint($f)
1416 // Read a 4-byte integer from stream
1417 $a = unpack('Ni',$this->_readstream($f,4));
1418 return $a['i'];
1421 protected function _parsegif($file)
1423 // Extract info from a GIF file (via PNG conversion)
1424 if(!function_exists('imagepng'))
1425 $this->Error('GD extension is required for GIF support');
1426 if(!function_exists('imagecreatefromgif'))
1427 $this->Error('GD has no GIF read support');
1428 $im = imagecreatefromgif($file);
1429 if(!$im)
1430 $this->Error('Missing or incorrect image file: '.$file);
1431 imageinterlace($im,0);
1432 ob_start();
1433 imagepng($im);
1434 $data = ob_get_clean();
1435 imagedestroy($im);
1436 $f = fopen('php://temp','rb+');
1437 if(!$f)
1438 $this->Error('Unable to create memory stream');
1439 fwrite($f,$data);
1440 rewind($f);
1441 $info = $this->_parsepngstream($f,$file);
1442 fclose($f);
1443 return $info;
1446 function _out($s)
1448 // Add a line to the document
1449 if($this->state==2)
1450 $this->pages[$this->page] .= $s."\n";
1451 elseif($this->state==1)
1452 $this->_put($s);
1453 elseif($this->state==0)
1454 $this->Error('No page has been added yet');
1455 elseif($this->state==3)
1456 $this->Error('The document is closed');
1459 protected function _put($s)
1461 $this->buffer .= $s."\n";
1464 protected function _getoffset()
1466 return strlen($this->buffer);
1469 protected function _newobj($n=null)
1471 // Begin a new object
1472 if($n===null)
1473 $n = ++$this->n;
1474 $this->offsets[$n] = $this->_getoffset();
1475 $this->_put($n.' 0 obj');
1478 protected function _putstream($data)
1480 $this->_put('stream');
1481 $this->_put($data);
1482 $this->_put('endstream');
1485 protected function _putstreamobject($data)
1487 if($this->compress)
1489 $entries = '/Filter /FlateDecode ';
1490 $data = gzcompress($data);
1492 else
1493 $entries = '';
1494 $entries .= '/Length '.strlen($data);
1495 $this->_newobj();
1496 $this->_put('<<'.$entries.'>>');
1497 $this->_putstream($data);
1498 $this->_put('endobj');
1501 protected function _putpage($n)
1503 $this->_newobj();
1504 $this->_put('<</Type /Page');
1505 $this->_put('/Parent 1 0 R');
1506 if(isset($this->PageInfo[$n]['size']))
1507 $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$this->PageInfo[$n]['size'][0],$this->PageInfo[$n]['size'][1]));
1508 if(isset($this->PageInfo[$n]['rotation']))
1509 $this->_put('/Rotate '.$this->PageInfo[$n]['rotation']);
1510 $this->_put('/Resources 2 0 R');
1511 if(isset($this->PageLinks[$n]))
1513 // Links
1514 $annots = '/Annots [';
1515 foreach($this->PageLinks[$n] as $pl)
1517 $rect = sprintf('%.2F %.2F %.2F %.2F',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
1518 $annots .= '<</Type /Annot /Subtype /Link /Rect ['.$rect.'] /Border [0 0 0] ';
1519 if(is_string($pl[4]))
1520 $annots .= '/A <</S /URI /URI '.$this->_textstring($pl[4]).'>>>>';
1521 else
1523 $l = $this->links[$pl[4]];
1524 if(isset($this->PageInfo[$l[0]]['size']))
1525 $h = $this->PageInfo[$l[0]]['size'][1];
1526 else
1527 $h = ($this->DefOrientation=='P') ? $this->DefPageSize[1]*$this->k : $this->DefPageSize[0]*$this->k;
1528 $annots .= sprintf('/Dest [%d 0 R /XYZ 0 %.2F null]>>',$this->PageInfo[$l[0]]['n'],$h-$l[1]*$this->k);
1531 $this->_put($annots.']');
1533 if($this->WithAlpha)
1534 $this->_put('/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>');
1535 $this->_put('/Contents '.($this->n+1).' 0 R>>');
1536 $this->_put('endobj');
1537 // Page content
1538 if(!empty($this->AliasNbPages))
1539 $this->pages[$n] = str_replace($this->AliasNbPages,$this->page,$this->pages[$n]);
1540 $this->_putstreamobject($this->pages[$n]);
1543 protected function _putpages()
1545 $nb = $this->page;
1546 for($n=1;$n<=$nb;$n++)
1547 $this->PageInfo[$n]['n'] = $this->n+1+2*($n-1);
1548 for($n=1;$n<=$nb;$n++)
1549 $this->_putpage($n);
1550 // Pages root
1551 $this->_newobj(1);
1552 $this->_put('<</Type /Pages');
1553 $kids = '/Kids [';
1554 for($n=1;$n<=$nb;$n++)
1555 $kids .= $this->PageInfo[$n]['n'].' 0 R ';
1556 $this->_put($kids.']');
1557 $this->_put('/Count '.$nb);
1558 if($this->DefOrientation=='P')
1560 $w = $this->DefPageSize[0];
1561 $h = $this->DefPageSize[1];
1563 else
1565 $w = $this->DefPageSize[1];
1566 $h = $this->DefPageSize[0];
1568 $this->_put(sprintf('/MediaBox [0 0 %.2F %.2F]',$w*$this->k,$h*$this->k));
1569 $this->_put('>>');
1570 $this->_put('endobj');
1573 protected function _putfonts()
1575 foreach($this->FontFiles as $file=>$info)
1577 // Font file embedding
1578 $this->_newobj();
1579 $this->FontFiles[$file]['n'] = $this->n;
1580 $font = file_get_contents($this->fontpath.$file,true);
1581 if(!$font)
1582 $this->Error('Font file not found: '.$file);
1583 $compressed = (substr($file,-2)=='.z');
1584 if(!$compressed && isset($info['length2']))
1585 $font = substr($font,6,$info['length1']).substr($font,6+$info['length1']+6,$info['length2']);
1586 $this->_put('<</Length '.strlen($font));
1587 if($compressed)
1588 $this->_put('/Filter /FlateDecode');
1589 $this->_put('/Length1 '.$info['length1']);
1590 if(isset($info['length2']))
1591 $this->_put('/Length2 '.$info['length2'].' /Length3 0');
1592 $this->_put('>>');
1593 $this->_putstream($font);
1594 $this->_put('endobj');
1596 foreach($this->fonts as $k=>$font)
1598 // Encoding
1599 if(isset($font['diff']))
1601 if(!isset($this->encodings[$font['enc']]))
1603 $this->_newobj();
1604 $this->_put('<</Type /Encoding /BaseEncoding /WinAnsiEncoding /Differences ['.$font['diff'].']>>');
1605 $this->_put('endobj');
1606 $this->encodings[$font['enc']] = $this->n;
1609 // ToUnicode CMap
1610 if(isset($font['uv']))
1612 if(isset($font['enc']))
1613 $cmapkey = $font['enc'];
1614 else
1615 $cmapkey = $font['name'];
1616 if(!isset($this->cmaps[$cmapkey]))
1618 $cmap = $this->_tounicodecmap($font['uv']);
1619 $this->_putstreamobject($cmap);
1620 $this->cmaps[$cmapkey] = $this->n;
1623 // Font object
1624 $this->fonts[$k]['n'] = $this->n+1;
1625 $type = $font['type'];
1626 $name = $font['name'];
1627 if($font['subsetted'])
1628 $name = 'AAAAAA+'.$name;
1629 if($type=='Core')
1631 // Core font
1632 $this->_newobj();
1633 $this->_put('<</Type /Font');
1634 $this->_put('/BaseFont /'.$name);
1635 $this->_put('/Subtype /Type1');
1636 if($name!='Symbol' && $name!='ZapfDingbats')
1637 $this->_put('/Encoding /WinAnsiEncoding');
1638 if(isset($font['uv']))
1639 $this->_put('/ToUnicode '.$this->cmaps[$cmapkey].' 0 R');
1640 $this->_put('>>');
1641 $this->_put('endobj');
1643 elseif($type=='Type1' || $type=='TrueType')
1645 // Additional Type1 or TrueType/OpenType font
1646 $this->_newobj();
1647 $this->_put('<</Type /Font');
1648 $this->_put('/BaseFont /'.$name);
1649 $this->_put('/Subtype /'.$type);
1650 $this->_put('/FirstChar 32 /LastChar 255');
1651 $this->_put('/Widths '.($this->n+1).' 0 R');
1652 $this->_put('/FontDescriptor '.($this->n+2).' 0 R');
1653 if(isset($font['diff']))
1654 $this->_put('/Encoding '.$this->encodings[$font['enc']].' 0 R');
1655 else
1656 $this->_put('/Encoding /WinAnsiEncoding');
1657 if(isset($font['uv']))
1658 $this->_put('/ToUnicode '.$this->cmaps[$cmapkey].' 0 R');
1659 $this->_put('>>');
1660 $this->_put('endobj');
1661 // Widths
1662 $this->_newobj();
1663 $cw = &$font['cw'];
1664 $s = '[';
1665 for($i=32;$i<=255;$i++)
1666 $s .= $cw[chr($i)].' ';
1667 $this->_put($s.']');
1668 $this->_put('endobj');
1669 // Descriptor
1670 $this->_newobj();
1671 $s = '<</Type /FontDescriptor /FontName /'.$name;
1672 foreach($font['desc'] as $k=>$v)
1673 $s .= ' /'.$k.' '.$v;
1674 if(!empty($font['file']))
1675 $s .= ' /FontFile'.($type=='Type1' ? '' : '2').' '.$this->FontFiles[$font['file']]['n'].' 0 R';
1676 $this->_put($s.'>>');
1677 $this->_put('endobj');
1679 else
1681 // Allow for additional types
1682 $mtd = '_put'.strtolower($type);
1683 if(!method_exists($this,$mtd))
1684 $this->Error('Unsupported font type: '.$type);
1685 $this->$mtd($font);
1690 protected function _tounicodecmap($uv)
1692 $ranges = '';
1693 $nbr = 0;
1694 $chars = '';
1695 $nbc = 0;
1696 foreach($uv as $c=>$v)
1698 if(is_array($v))
1700 $ranges .= sprintf("<%02X> <%02X> <%04X>\n",$c,$c+$v[1]-1,$v[0]);
1701 $nbr++;
1703 else
1705 $chars .= sprintf("<%02X> <%04X>\n",$c,$v);
1706 $nbc++;
1709 $s = "/CIDInit /ProcSet findresource begin\n";
1710 $s .= "12 dict begin\n";
1711 $s .= "begincmap\n";
1712 $s .= "/CIDSystemInfo\n";
1713 $s .= "<</Registry (Adobe)\n";
1714 $s .= "/Ordering (UCS)\n";
1715 $s .= "/Supplement 0\n";
1716 $s .= ">> def\n";
1717 $s .= "/CMapName /Adobe-Identity-UCS def\n";
1718 $s .= "/CMapType 2 def\n";
1719 $s .= "1 begincodespacerange\n";
1720 $s .= "<00> <FF>\n";
1721 $s .= "endcodespacerange\n";
1722 if($nbr>0)
1724 $s .= "$nbr beginbfrange\n";
1725 $s .= $ranges;
1726 $s .= "endbfrange\n";
1728 if($nbc>0)
1730 $s .= "$nbc beginbfchar\n";
1731 $s .= $chars;
1732 $s .= "endbfchar\n";
1734 $s .= "endcmap\n";
1735 $s .= "CMapName currentdict /CMap defineresource pop\n";
1736 $s .= "end\n";
1737 $s .= "end";
1738 return $s;
1741 protected function _putimages()
1743 foreach(array_keys($this->images) as $file)
1745 $this->_putimage($this->images[$file]);
1746 unset($this->images[$file]['data']);
1747 unset($this->images[$file]['smask']);
1751 protected function _putimage(&$info)
1753 $this->_newobj();
1754 $info['n'] = $this->n;
1755 $this->_put('<</Type /XObject');
1756 $this->_put('/Subtype /Image');
1757 $this->_put('/Width '.$info['w']);
1758 $this->_put('/Height '.$info['h']);
1759 if($info['cs']=='Indexed')
1760 $this->_put('/ColorSpace [/Indexed /DeviceRGB '.(strlen($info['pal'])/3-1).' '.($this->n+1).' 0 R]');
1761 else
1763 $this->_put('/ColorSpace /'.$info['cs']);
1764 if($info['cs']=='DeviceCMYK')
1765 $this->_put('/Decode [1 0 1 0 1 0 1 0]');
1767 $this->_put('/BitsPerComponent '.$info['bpc']);
1768 if(isset($info['f']))
1769 $this->_put('/Filter /'.$info['f']);
1770 if(isset($info['dp']))
1771 $this->_put('/DecodeParms <<'.$info['dp'].'>>');
1772 if(isset($info['trns']) && is_array($info['trns']))
1774 $trns = '';
1775 for($i=0;$i<count($info['trns']);$i++)
1776 $trns .= $info['trns'][$i].' '.$info['trns'][$i].' ';
1777 $this->_put('/Mask ['.$trns.']');
1779 if(isset($info['smask']))
1780 $this->_put('/SMask '.($this->n+1).' 0 R');
1781 $this->_put('/Length '.strlen($info['data']).'>>');
1782 $this->_putstream($info['data']);
1783 $this->_put('endobj');
1784 // Soft mask
1785 if(isset($info['smask']))
1787 $dp = '/Predictor 15 /Colors 1 /BitsPerComponent 8 /Columns '.$info['w'];
1788 $smask = array('w'=>$info['w'], 'h'=>$info['h'], 'cs'=>'DeviceGray', 'bpc'=>8, 'f'=>$info['f'], 'dp'=>$dp, 'data'=>$info['smask']);
1789 $this->_putimage($smask);
1791 // Palette
1792 if($info['cs']=='Indexed')
1793 $this->_putstreamobject($info['pal']);
1796 protected function _putxobjectdict()
1798 foreach($this->images as $image)
1799 $this->_put('/I'.$image['i'].' '.$image['n'].' 0 R');
1802 protected function _putresourcedict()
1804 $this->_put('/ProcSet [/PDF /Text /ImageB /ImageC /ImageI]');
1805 $this->_put('/Font <<');
1806 foreach($this->fonts as $font)
1807 $this->_put('/F'.$font['i'].' '.$font['n'].' 0 R');
1808 $this->_put('>>');
1809 $this->_put('/XObject <<');
1810 $this->_putxobjectdict();
1811 $this->_put('>>');
1814 protected function _putresources()
1816 $this->_putfonts();
1817 $this->_putimages();
1818 // Resource dictionary
1819 $this->_newobj(2);
1820 $this->_put('<<');
1821 $this->_putresourcedict();
1822 $this->_put('>>');
1823 $this->_put('endobj');
1826 protected function _putinfo()
1828 $this->metadata['Producer'] = 'FPDF '.FPDF_VERSION;
1829 $this->metadata['CreationDate'] = 'D:'.@date('YmdHis');
1830 foreach($this->metadata as $key=>$value)
1831 $this->_put('/'.$key.' '.$this->_textstring($value));
1834 protected function _putcatalog()
1836 $n = $this->PageInfo[1]['n'];
1837 $this->_put('/Type /Catalog');
1838 $this->_put('/Pages 1 0 R');
1839 if($this->ZoomMode=='fullpage')
1840 $this->_put('/OpenAction ['.$n.' 0 R /Fit]');
1841 elseif($this->ZoomMode=='fullwidth')
1842 $this->_put('/OpenAction ['.$n.' 0 R /FitH null]');
1843 elseif($this->ZoomMode=='real')
1844 $this->_put('/OpenAction ['.$n.' 0 R /XYZ null null 1]');
1845 elseif(!is_string($this->ZoomMode))
1846 $this->_put('/OpenAction ['.$n.' 0 R /XYZ null null '.sprintf('%.2F',$this->ZoomMode/100).']');
1847 if($this->LayoutMode=='single')
1848 $this->_put('/PageLayout /SinglePage');
1849 elseif($this->LayoutMode=='continuous')
1850 $this->_put('/PageLayout /OneColumn');
1851 elseif($this->LayoutMode=='two')
1852 $this->_put('/PageLayout /TwoColumnLeft');
1855 protected function _putheader()
1857 $this->_put('%PDF-'.$this->PDFVersion);
1860 protected function _puttrailer()
1862 $this->_put('/Size '.($this->n+1));
1863 $this->_put('/Root '.$this->n.' 0 R');
1864 $this->_put('/Info '.($this->n-1).' 0 R');
1867 protected function _enddoc()
1869 $this->_putheader();
1870 $this->_putpages();
1871 $this->_putresources();
1872 // Info
1873 $this->_newobj();
1874 $this->_put('<<');
1875 $this->_putinfo();
1876 $this->_put('>>');
1877 $this->_put('endobj');
1878 // Catalog
1879 $this->_newobj();
1880 $this->_put('<<');
1881 $this->_putcatalog();
1882 $this->_put('>>');
1883 $this->_put('endobj');
1884 // Cross-ref
1885 $offset = $this->_getoffset();
1886 $this->_put('xref');
1887 $this->_put('0 '.($this->n+1));
1888 $this->_put('0000000000 65535 f ');
1889 for($i=1;$i<=$this->n;$i++)
1890 $this->_put(sprintf('%010d 00000 n ',$this->offsets[$i]));
1891 // Trailer
1892 $this->_put('trailer');
1893 $this->_put('<<');
1894 $this->_puttrailer();
1895 $this->_put('>>');
1896 $this->_put('startxref');
1897 $this->_put($offset);
1898 $this->_put('%%EOF');
1899 $this->state = 3;