最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
php生成pdf文件代码
时间:2022-06-24 18:30:17 编辑:袖梨 来源:一聚教程网
//php教程生成pdf文件代码
/*
生成pdf文件如果真的生成很好,是很复杂的,下面我们来看看一款完整理的php生成pdf文件代码吧,他可以把文本文件生成pdf文件也可以利用图片文件生成pdf文件哦。
这款生成pdf文件用到文件有
fpdf.php
chinese.php
picpdf.php
*/
//fpdf.php代码
define('fpdf_version','1.6');
class fpdf
{
var $page; //current page number
var $n; //current object number
var $offsets; //array of object offsets
var $buffer; //buffer holding in-memory pdf
var $pages; //array containing pages
var $state; //current document state
var $compress; //compression flag
var $k; //scale factor (number of points in user unit)
var $deforientation; //default orientation
var $curorientation; //current orientation
var $pageformats; //available page formats
var $defpageformat; //default page format
var $curpageformat; //current page format
var $pagesizes; //array storing non-default page sizes
var $wpt,$hpt; //dimensions of current page in points
var $w,$h; //dimensions of current page in user unit
var $lmargin; //left margin
var $tmargin; //top margin
var $rmargin; //right margin
var $bmargin; //page break margin
var $cmargin; //cell margin
var $x,$y; //current position in user unit
var $lasth; //height of last printed cell
var $linewidth; //line width in user unit
var $corefonts; //array of standard font names
var $fonts; //array of used fonts
var $fontfiles; //array of font files
var $diffs; //array of encoding differences
var $fontfamily; //current font family
var $fontstyle; //current font style
var $underline; //underlining flag
var $currentfont; //current font info
var $fontsizept; //current font size in points
var $fontsize; //current font size in user unit
var $drawcolor; //commands for drawing color
var $fillcolor; //commands for filling color
var $textcolor; //commands for text color
var $colorflag; //indicates whether fill and text colors are different
var $ws; //word spacing
var $images; //array of used images
var $pagelinks; //array of links in pages
var $links; //array of internal links
var $autopagebreak; //automatic page breaking
var $pagebreaktrigger; //threshold used to trigger page breaks
var $inheader; //flag set when processing header
var $infooter; //flag set when processing footer
var $zoommode; //zoom display mode
var $layoutmode; //layout display mode
var $title; //title
var $subject; //subject
var $author; //author
var $keywords; //keywords
var $creator; //creator
var $aliasnbpages; //alias for total number of pages
var $pdfversion; //pdf version number
/*******************************************************************************
* *
* public methods *
* *
*******************************************************************************/
function fpdf($orientation='p', $unit='mm', $format='a4')
{
//some checks
$this->_dochecks();
//initialization of properties
$this->page=0;
$this->n=2;
$this->buffer='';
$this->pages=array();
$this->pagesizes=array();
$this->state=0;
$this->fonts=array();
$this->fontfiles=array();
$this->diffs=array();
$this->images=array();
$this->links=array();
$this->inheader=false;
$this->infooter=false;
$this->lasth=0;
$this->fontfamily='';
$this->fontstyle='';
$this->fontsizept=12;
$this->underline=false;
$this->drawcolor='0 g';
$this->fillcolor='0 g';
$this->textcolor='0 g';
$this->colorflag=false;
$this->ws=0;
//standard fonts
$this->corefonts=array('courier'=>'courier', 'courierb'=>'courier-bold', 'courieri'=>'courier-oblique', 'courierbi'=>'courier-boldoblique',
'helvetica'=>'helvetica', 'helveticab'=>'helvetica-bold', 'helveticai'=>'helvetica-oblique', 'helveticabi'=>'helvetica-boldoblique',
'times'=>'times-roman', 'timesb'=>'times-bold', 'timesi'=>'times-italic', 'timesbi'=>'times-bolditalic',
'symbol'=>'symbol', 'zapfdingbats'=>'zapfdingbats');
//scale factor
if($unit=='pt')
$this->k=1;
elseif($unit=='mm')
$this->k=72/25.4;
elseif($unit=='cm')
$this->k=72/2.54;
elseif($unit=='in')
$this->k=72;
else
$this->error('incorrect unit: '.$unit);
//page format
$this->pageformats=array('a3'=>array(841.89,1190.55), 'a4'=>array(595.28,841.89), 'a5'=>array(420.94,595.28),
'letter'=>array(612,792), 'legal'=>array(612,1008));
if(is_string($format))
$format=$this->_getpageformat($format);
$this->defpageformat=$format;
$this->curpageformat=$format;
//page orientation
$orientation=strtolower($orientation);
if($orientation=='p' || $orientation=='portrait')
{
$this->deforientation='p';
$this->w=$this->defpageformat[0];
$this->h=$this->defpageformat[1];
}
elseif($orientation=='l' || $orientation=='landscape')
{
$this->deforientation='l';
$this->w=$this->defpageformat[1];
$this->h=$this->defpageformat[0];
}
else
$this->error('incorrect orientation: '.$orientation);
$this->curorientation=$this->deforientation;
$this->wpt=$this->w*$this->k;
$this->hpt=$this->h*$this->k;
//page margins (1 cm)
$margin=28.35/$this->k;
$this->setmargins($margin,$margin);
//interior cell margin (1 mm)
$this->cmargin=$margin/10;
//line width (0.2 mm)
$this->linewidth=.567/$this->k;
//automatic page break
$this->setautopagebreak(true,2*$margin);
//full width display mode
$this->setdisplaymode('fullwidth');
//enable compression
$this->setcompression(true);
//set default pdf version number
$this->pdfversion='1.3';
}
function setmargins($left, $top, $right=null)
{
//set left, top and right margins
$this->lmargin=$left;
$this->tmargin=$top;
if($right===null)
$right=$left;
$this->rmargin=$right;
}
function setleftmargin($margin)
{
//set left margin
$this->lmargin=$margin;
if($this->page>0 && $this->x<$margin)
$this->x=$margin;
}
function settopmargin($margin)
{
//set top margin
$this->tmargin=$margin;
}
function setrightmargin($margin)
{
//set right margin
$this->rmargin=$margin;
}
function setautopagebreak($auto, $margin=0)
{
//set auto page break mode and triggering margin
$this->autopagebreak=$auto;
$this->bmargin=$margin;
$this->pagebreaktrigger=$this->h-$margin;
}
function setdisplaymode($zoom, $layout='continuous')
{
//set display mode in viewer
if($zoom=='fullpage' || $zoom=='fullwidth' || $zoom=='real' || $zoom=='default' || !is_string($zoom))
$this->zoommode=$zoom;
else
$this->error('incorrect zoom display mode: '.$zoom);
if($layout=='single' || $layout=='continuous' || $layout=='two' || $layout=='default')
$this->layoutmode=$layout;
else
$this->error('incorrect layout display mode: '.$layout);
}
function setcompression($compress)
{
//set page compression
if(function_exists('gzcompress'))
$this->compress=$compress;
else
$this->compress=false;
}
function settitle($title, $isutf8=false)
{
//title of document
if($isutf8)
$title=$this->_utf8toutf16($title);
$this->title=$title;
}
function setsubject($subject, $isutf8=false)
{
//subject of document
if($isutf8)
$subject=$this->_utf8toutf16($subject);
$this->subject=$subject;
}
function setauthor($author, $isutf8=false)
{
//author of document
if($isutf8)
$author=$this->_utf8toutf16($author);
$this->author=$author;
}
function setkeywords($keywords, $isutf8=false)
{
//keywords of document
if($isutf8)
$keywords=$this->_utf8toutf16($keywords);
$this->keywords=$keywords;
}
function setcreator($creator, $isutf8=false)
{
//creator of document
if($isutf8)
$creator=$this->_utf8toutf16($creator);
$this->creator=$creator;
}
function aliasnbpages($alias='{nb}')
{
//define an alias for total number of pages
$this->aliasnbpages=$alias;
}
function error($msg)
{
//fatal error
die('fpdf error: '.$msg);
}
function open()
{
//begin document
$this->state=1;
}
function close()
{
//terminate document
if($this->state==3)
return;
if($this->page==0)
$this->addpage();
//page footer
$this->infooter=true;
$this->footer();
$this->infooter=false;
//close page
$this->_endpage();
//close document
$this->_enddoc();
}
function addpage($orientation='', $format='')
{
//start a new page
if($this->state==0)
$this->open();
$family=$this->fontfamily;
$style=$this->fontstyle.($this->underline ? 'u' : '');
$size=$this->fontsizept;
$lw=$this->linewidth;
$dc=$this->drawcolor;
$fc=$this->fillcolor;
$tc=$this->textcolor;
$cf=$this->colorflag;
if($this->page>0)
{
//page footer
$this->infooter=true;
$this->footer();
$this->infooter=false;
//close page
$this->_endpage();
}
//start new page
$this->_beginpage($orientation,$format);
//set line cap style to square
$this->_out('2 j');
//set line width
$this->linewidth=$lw;
$this->_out(sprintf('%.2f w',$lw*$this->k));
//set font
if($family)
$this->setfont($family,$style,$size);
//set colors
$this->drawcolor=$dc;
if($dc!='0 g')
$this->_out($dc);
$this->fillcolor=$fc;
if($fc!='0 g')
$this->_out($fc);
$this->textcolor=$tc;
$this->colorflag=$cf;
//page header
$this->inheader=true;
$this->header();
$this->inheader=false;
//restore line width
if($this->linewidth!=$lw)
{
$this->linewidth=$lw;
$this->_out(sprintf('%.2f w',$lw*$this->k));
}
//restore font
if($family)
$this->setfont($family,$style,$size);
//restore colors
if($this->drawcolor!=$dc)
{
$this->drawcolor=$dc;
$this->_out($dc);
}
if($this->fillcolor!=$fc)
{
$this->fillcolor=$fc;
$this->_out($fc);
}
$this->textcolor=$tc;
$this->colorflag=$cf;
}
function header()
{
//to be implemented in your own inherited class
}
function footer()
{
//to be implemented in your own inherited class
}
function pageno()
{
//get current page number
return $this->page;
}
function setdrawcolor($r, $g=null, $b=null)
{
//set color for all stroking operations
if(($r==0 && $g==0 && $b==0) || $g===null)
$this->drawcolor=sprintf('%.3f g',$r/255);
else
$this->drawcolor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
if($this->page>0)
$this->_out($this->drawcolor);
}
function setfillcolor($r, $g=null, $b=null)
{
//set color for all filling operations
if(($r==0 && $g==0 && $b==0) || $g===null)
$this->fillcolor=sprintf('%.3f g',$r/255);
else
$this->fillcolor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
$this->colorflag=($this->fillcolor!=$this->textcolor);
if($this->page>0)
$this->_out($this->fillcolor);
}
function settextcolor($r, $g=null, $b=null)
{
//set color for text
if(($r==0 && $g==0 && $b==0) || $g===null)
$this->textcolor=sprintf('%.3f g',$r/255);
else
$this->textcolor=sprintf('%.3f %.3f %.3f rg',$r/255,$g/255,$b/255);
$this->colorflag=($this->fillcolor!=$this->textcolor);
}
function getstringwidth($s)
{
//get width of a string in the current font
$s=(string)$s;
$cw=&$this->currentfont['cw'];
$w=0;
$l=strlen($s);
for($i=0;$i<$l;$i++)
$w+=$cw[$s[$i]];
return $w*$this->fontsize/1000;
}
function setlinewidth($width)
{
//set line width
$this->linewidth=$width;
if($this->page>0)
$this->_out(sprintf('%.2f w',$width*$this->k));
}
function line($x1, $y1, $x2, $y2)
{
//draw a line
$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));
}
function rect($x, $y, $w, $h, $style='')
{
//draw a rectangle
if($style=='f')
$op='f';
elseif($style=='fd' || $style=='df')
$op='b';
else
$op='s';
$this->_out(sprintf('%.2f %.2f %.2f %.2f re %s',$x*$this->k,($this->h-$y)*$this->k,$w*$this->k,-$h*$this->k,$op));
}
function addfont($family, $style='', $file='')
{
//add a truetype or type1 font
$family=strtolower($family);
if($file=='')
$file=str_replace(' ','',$family).strtolower($style).'.php';
if($family=='arial')
$family='helvetica';
$style=strtoupper($style);
if($style=='ib')
$style='bi';
$fontkey=$family.$style;
if(isset($this->fonts[$fontkey]))
return;
include($this->_getfontpath().$file);
if(!isset($name))
$this->error('could not include font definition file');
$i=count($this->fonts)+1;
$this->fonts[$fontkey]=array('i'=>$i, 'type'=>$type, 'name'=>$name, 'desc'=>$desc, 'up'=>$up, 'ut'=>$ut, 'cw'=>$cw, 'enc'=>$enc, 'file'=>$file);
if($diff)
{
//search existing encodings
$d=0;
$nb=count($this->diffs);
for($i=1;$i<=$nb;$i++)
{
if($this->diffs[$i]==$diff)
{
$d=$i;
break;
}
}
if($d==0)
{
$d=$nb+1;
$this->diffs[$d]=$diff;
}
$this->fonts[$fontkey]['diff']=$d;
}
if($file)
{
if($type=='truetype')
$this->fontfiles[$file]=array('length1'=>$originalsize);
else
$this->fontfiles[$file]=array('length1'=>$size1, 'length2'=>$size2);
}
}
function setfont($family, $style='', $size=0)
{
//select a font; size given in points
global $fpdf_charwidths;
$family=strtolower($family);
if($family=='')
$family=$this->fontfamily;
if($family=='arial')
$family='helvetica';
elseif($family=='symbol' || $family=='zapfdingbats')
$style='';
$style=strtoupper($style);
if(strpos($style,'u')!==false)
{
$this->underline=true;
$style=str_replace('u','',$style);
}
else
$this->underline=false;
if($style=='ib')
$style='bi';
if($size==0)
$size=$this->fontsizept;
//test if font is already selected
if($this->fontfamily==$family && $this->fontstyle==$style && $this->fontsizept==$size)
return;
//test if used for the first time
$fontkey=$family.$style;
if(!isset($this->fonts[$fontkey]))
{
//check if one of the standard fonts
if(isset($this->corefonts[$fontkey]))
{
if(!isset($fpdf_charwidths[$fontkey]))
{
//load metric file
$file=$family;
if($family=='times' || $family=='helvetica')
$file.=strtolower($style);
include($this->_getfontpath().$file.'.php');
if(!isset($fpdf_charwidths[$fontkey]))
$this->error('could not include font metric file');
}
$i=count($this->fonts)+1;
$name=$this->corefonts[$fontkey];
$cw=$fpdf_charwidths[$fontkey];
$this->fonts[$fontkey]=array('i'=>$i, 'type'=>'core', 'name'=>$name, 'up'=>-100, 'ut'=>50, 'cw'=>$cw);
}
else
$this->error('undefined font: '.$family.' '.$style);
}
//select it
$this->fontfamily=$family;
$this->fontstyle=$style;
$this->fontsizept=$size;
$this->fontsize=$size/$this->k;
$this->currentfont=&$this->fonts[$fontkey];
if($this->page>0)
$this->_out(sprintf('bt /f%d %.2f tf et',$this->currentfont['i'],$this->fontsizept));
}
function setfontsize($size)
{
//set font size in points
if($this->fontsizept==$size)
return;
$this->fontsizept=$size;
$this->fontsize=$size/$this->k;
if($this->page>0)
$this->_out(sprintf('bt /f%d %.2f tf et',$this->currentfont['i'],$this->fontsizept));
}
function addlink()
{
//create a new internal link
$n=count($this->links)+1;
$this->links[$n]=array(0, 0);
return $n;
}
function setlink($link, $y=0, $page=-1)
{
//set destination of internal link
if($y==-1)
$y=$this->y;
if($page==-1)
$page=$this->page;
$this->links[$link]=array($page, $y);
}
function link($x, $y, $w, $h, $link)
{
//put a link on the page
$this->pagelinks[$this->page][]=array($x*$this->k, $this->hpt-$y*$this->k, $w*$this->k, $h*$this->k, $link);
}
function text($x, $y, $txt)
{
//output a string
$s=sprintf('bt %.2f %.2f td (%s) tj et',$x*$this->k,($this->h-$y)*$this->k,$this->_escape($txt));
if($this->underline && $txt!='')
$s.=' '.$this->_dounderline($x,$y,$txt);
if($this->colorflag)
$s='q '.$this->textcolor.' '.$s.' q';
$this->_out($s);
}
function acceptpagebreak()
{
//accept automatic page break or not
return $this->autopagebreak;
}
function cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='')
{
//output a cell
$k=$this->k;
if($this->y+$h>$this->pagebreaktrigger && !$this->inheader && !$this->infooter && $this->acceptpagebreak())
{
//automatic page break
$x=$this->x;
$ws=$this->ws;
if($ws>0)
{
$this->ws=0;
$this->_out('0 tw');
}
$this->addpage($this->curorientation,$this->curpageformat);
$this->x=$x;
if($ws>0)
{
$this->ws=$ws;
$this->_out(sprintf('%.3f tw',$ws*$k));
}
}
if($w==0)
$w=$this->w-$this->rmargin-$this->x;
$s='';
if($fill || $border==1)
{
if($fill)
$op=($border==1) ? 'b' : 'f';
else
$op='s';
$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
}
if(is_string($border))
{
$x=$this->x;
$y=$this->y;
if(strpos($border,'l')!==false)
$s.=sprintf('%.2f %.2f m %.2f %.2f l s ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
if(strpos($border,'t')!==false)
$s.=sprintf('%.2f %.2f m %.2f %.2f l s ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
if(strpos($border,'r')!==false)
$s.=sprintf('%.2f %.2f m %.2f %.2f l s ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
if(strpos($border,'b')!==false)
$s.=sprintf('%.2f %.2f m %.2f %.2f l s ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
}
if($txt!=='')
{
if($align=='r')
$dx=$w-$this->cmargin-$this->getstringwidth($txt);
elseif($align=='c')
$dx=($w-$this->getstringwidth($txt))/2;
else
$dx=$this->cmargin;
if($this->colorflag)
$s.='q '.$this->textcolor.' ';
$txt2=str_replace(')',')',str_replace('(','(',str_replace('','\',$txt)));
$s.=sprintf('bt %.2f %.2f td (%s) tj et',($this->x+$dx)*$k,($this->h-($this->y+.5*$h+.3*$this->fontsize))*$k,$txt2);
if($this->underline)
$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->fontsize,$txt);
if($this->colorflag)
$s.=' q';
if($link)
$this->link($this->x+$dx,$this->y+.5*$h-.5*$this->fontsize,$this->getstringwidth($txt),$this->fontsize,$link);
}
if($s)
$this->_out($s);
$this->lasth=$h;
if($ln>0)
{
//go to next line
$this->y+=$h;
if($ln==1)
$this->x=$this->lmargin;
}
else
$this->x+=$w;
}
function multicell($w, $h, $txt, $border=0, $align='j', $fill=false)
{
//output text with automatic or explicit line breaks
$cw=&$this->currentfont['cw'];
if($w==0)
$w=$this->w-$this->rmargin-$this->x;
$wmax=($w-2*$this->cmargin)*1000/$this->fontsize;
$s=str_replace("r",'',$txt);
$nb=strlen($s);
if($nb>0 && $s[$nb-1]=="n")
$nb--;
$b=0;
if($border)
{
if($border==1)
{
$border='ltrb';
$b='lrt';
$b2='lr';
}
else
{
$b2='';
if(strpos($border,'l')!==false)
$b2.='l';
if(strpos($border,'r')!==false)
$b2.='r';
$b=(strpos($border,'t')!==false) ? $b2.'t' : $b2;
}
}
$sep=-1;
$i=0;
$j=0;
$l=0;
$ns=0;
$nl=1;
while($i<$nb)
{
//get next character
$c=$s[$i];
if($c=="n")
{
//explicit line break
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 tw');
}
$this->cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$i++;
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border && $nl==2)
$b=$b2;
continue;
}
if($c==' ')
{
$sep=$i;
$ls=$l;
$ns++;
}
$l+=$cw[$c];
if($l>$wmax)
{
//automatic line break
if($sep==-1)
{
if($i==$j)
$i++;
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 tw');
}
$this->cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
}
else
{
if($align=='j')
{
$this->ws=($ns>1) ? ($wmax-$ls)/1000*$this->fontsize/($ns-1) : 0;
$this->_out(sprintf('%.3f tw',$this->ws*$this->k));
}
$this->cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$i=$sep+1;
}
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border && $nl==2)
$b=$b2;
}
else
$i++;
}
//last chunk
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 tw');
}
if($border && strpos($border,'b')!==false)
$b.='b';
$this->cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->x=$this->lmargin;
}
function write($h, $txt, $link='')
{
//output text in flowing mode
$cw=&$this->currentfont['cw'];
$w=$this->w-$this->rmargin-$this->x;
$wmax=($w-2*$this->cmargin)*1000/$this->fontsize;
$s=str_replace("r",'',$txt);
$nb=strlen($s);
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
//get next character
$c=$s[$i];
if($c=="n")
{
//explicit line break
$this->cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
$i++;
$sep=-1;
$j=$i;
$l=0;
if($nl==1)
{
$this->x=$this->lmargin;
$w=$this->w-$this->rmargin-$this->x;
$wmax=($w-2*$this->cmargin)*1000/$this->fontsize;
}
$nl++;
continue;
}
if($c==' ')
$sep=$i;
$l+=$cw[$c];
if($l>$wmax)
{
//automatic line break
if($sep==-1)
{
if($this->x>$this->lmargin)
{
//move to next line
$this->x=$this->lmargin;
$this->y+=$h;
$w=$this->w-$this->rmargin-$this->x;
$wmax=($w-2*$this->cmargin)*1000/$this->fontsize;
$i++;
$nl++;
continue;
}
if($i==$j)
$i++;
$this->cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
}
else
{
$this->cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);
$i=$sep+1;
}
$sep=-1;
$j=$i;
$l=0;
if($nl==1)
{
$this->x=$this->lmargin;
$w=$this->w-$this->rmargin-$this->x;
$wmax=($w-2*$this->cmargin)*1000/$this->fontsize;
}
$nl++;
}
else
$i++;
}
//last chunk
if($i!=$j)
$this->cell($l/1000*$this->fontsize,$h,substr($s,$j),0,0,'',0,$link);
}
function ln($h=null)
{
//line feed; default value is last cell height
$this->x=$this->lmargin;
if($h===null)
$this->y+=$this->lasth;
else
$this->y+=$h;
}
function image($file, $x=null, $y=null, $w=0, $h=0, $type='', $link='')
{
//put an image on the page
if(!isset($this->images[$file]))
{
//first use of this image, get info
if($type=='')
{
$pos=strrpos($file,'.');
if(!$pos)
$this->error('image file has no extension and no type was specified: '.$file);
$type=substr($file,$pos+1);
}
$type=strtolower($type);
if($type=='jpeg')
$type='jpg';
$mtd='_parse'.$type;
if(!method_exists($this,$mtd))
$this->error('unsupported image type: '.$type);
$info=$this->$mtd($file);
$info['i']=count($this->images)+1;
$this->images[$file]=$info;
}
else
$info=$this->images[$file];
//automatic width and height calculation if needed
if($w==0 && $h==0)
{
//put image at 72 dpi
$w=$info['w']/$this->k;
$h=$info['h']/$this->k;
}
elseif($w==0)
$w=$h*$info['w']/$info['h'];
elseif($h==0)
$h=$w*$info['h']/$info['w'];
//flowing mode
if($y===null)
{
if($this->y+$h>$this->pagebreaktrigger && !$this->inheader && !$this->infooter && $this->acceptpagebreak())
{
//automatic page break
$x2=$this->x;
$this->addpage($this->curorientation,$this->curpageformat);
$this->x=$x2;
}
$y=$this->y;
$this->y+=$h;
}
if($x===null)
$x=$this->x;
$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']));
if($link)
$this->link($x,$y,$w,$h,$link);
}
function getx()
{
//get x position
return $this->x;
}
function setx($x)
{
//set x position
if($x>=0)
$this->x=$x;
else
$this->x=$this->w+$x;
}
function gety()
{
//get y position
return $this->y;
}
function sety($y)
{
//set y position and reset x
$this->x=$this->lmargin;
if($y>=0)
$this->y=$y;
else
$this->y=$this->h+$y;
}
function setxy($x, $y)
{
//set x and y positions
$this->sety($y);
$this->setx($x);
}
function output($name='', $dest='')
{
//output pdf to some destination
if($this->state<3)
$this->close();
$dest=strtoupper($dest);
if($dest=='')
{
if($name=='')
{
$name='doc.pdf';
$dest='i';
}
else
$dest='f';
}
switch($dest)
{
case 'i':
//send to standard output
if(ob_get_length())
$this->error('some data has already been output, can't send pdf file');
if(php_sapi_name()!='cli')
{
//we send to a browser
header('content-type: application/pdf');
if(headers_sent())
$this->error('some data has already been output, can't send pdf file');
header('content-length: '.strlen($this->buffer));
header('content-disposition: inline; filename="'.$name.'"');
header('cache-control: private, max-age=0, must-revalidate');
header('pragma: public');
ini_set('zlib.output_compression','0');
}
echo $this->buffer;
break;
case 'd':
//download file
if(ob_get_length())
$this->error('some data has already been output, can't send pdf file');
header('content-type: application/x-download');
if(headers_sent())
$this->error('some data has already been output, can't send pdf file');
header('content-length: '.strlen($this->buffer));
header('content-disposition: attachment; filename="'.$name.'"');
header('cache-control: private, max-age=0, must-revalidate');
header('pragma: public');
ini_set('zlib.output_compression','0');
echo $this->buffer;
break;
case 'f':
//save to local file
$f=fopen($name,'wb');
if(!$f)
$this->error('unable to create output file: '.$name);
fwrite($f,$this->buffer,strlen($this->buffer));
fclose($f);
break;
case 's':
//return as a string
return $this->buffer;
default:
$this->error('incorrect output destination: '.$dest);
}
return '';
}
/*******************************************************************************
* *
* protected methods *
* *
*******************************************************************************/
function _dochecks()
{
//check availability of %f
if(sprintf('%.1f',1.0)!='1.0')
$this->error('this version of php is not supported');
//check mbstring overloading
if(ini_get('mbstring.func_overload') & 2)
$this->error('mbstring overloading must be disabled');
//disable runtime magic quotes
if(get_magic_quotes_runtime())
@set_magic_quotes_runtime(0);
}
function _getpageformat($format)
{
$format=strtolower($format);
if(!isset($this->pageformats[$format]))
$this->error('unknown page format: '.$format);
$a=$this->pageformats[$format];
return array($a[0]/$this->k, $a[1]/$this->k);
}
function _getfontpath()
{
if(!defined('fpdf_fontpath') && is_dir(dirname(__file__).'/font'))
define('fpdf_fontpath',dirname(__file__).'/font/');
return defined('fpdf_fontpath') ? fpdf_fontpath : '';
}
function _beginpage($orientation, $format)
{
$this->page++;
$this->pages[$this->page]='';
$this->state=2;
$this->x=$this->lmargin;
$this->y=$this->tmargin;
$this->fontfamily='';
//check page size
if($orientation=='')
$orientation=$this->deforientation;
else
$orientation=strtoupper($orientation[0]);
if($format=='')
$format=$this->defpageformat;
else
{
if(is_string($format))
$format=$this->_getpageformat($format);
}
if($orientation!=$this->curorientation || $format[0]!=$this->curpageformat[0] || $format[1]!=$this->curpageformat[1])
{
//new size
if($orientation=='p')
{
$this->w=$format[0];
$this->h=$format[1];
}
else
{
$this->w=$format[1];
$this->h=$format[0];
}
$this->wpt=$this->w*$this->k;
$this->hpt=$this->h*$this->k;
$this->pagebreaktrigger=$this->h-$this->bmargin;
$this->curorientation=$orientation;
$this->curpageformat=$format;
}
if($orientation!=$this->deforientation || $format[0]!=$this->defpageformat[0] || $format[1]!=$this->defpageformat[1])
$this->pagesizes[$this->page]=array($this->wpt, $this->hpt);
}
function _endpage()
{
$this->state=1;
}
function _escape($s)
{
//escape special characters in strings
$s=str_replace('','\',$s);
$s=str_replace('(','(',$s);
$s=str_replace(')',')',$s);
$s=str_replace("r",'r',$s);
return $s;
}
function _textstring($s)
{
//format a text string
return '('.$this->_escape($s).')';
}
function _utf8toutf16($s)
{
//convert utf-8 to utf-16be with bom
$res="xfexff";
$nb=strlen($s);
$i=0;
while($i<$nb)
{
$c1=ord($s[$i++]);
if($c1>=224)
{
//3-byte character
$c2=ord($s[$i++]);
$c3=ord($s[$i++]);
$res.=chr((($c1 & 0x0f)<<4) + (($c2 & 0x3c)>>2));
$res.=chr((($c2 & 0x03)<<6) + ($c3 & 0x3f));
}
elseif($c1>=192)
{
//2-byte character
$c2=ord($s[$i++]);
$res.=chr(($c1 & 0x1c)>>2);
$res.=chr((($c1 & 0x03)<<6) + ($c2 & 0x3f));
}
else
{
//single-byte character
$res.="".chr($c1);
}
}
return $res;
}
function _dounderline($x, $y, $txt)
{
//underline text
$up=$this->currentfont['up'];
$ut=$this->currentfont['ut'];
$w=$this->getstringwidth($txt)+$this->ws*substr_count($txt,' ');
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);
}
function _parsejpg($file)
{
//extract info from a jpeg file
$a=getimagesize($file);
if(!$a)
$this->error('missing or incorrect image file: '.$file);
if($a[2]!=2)
$this->error('not a jpeg file: '.$file);
if(!isset($a['channels']) || $a['channels']==3)
$colspace='devicergb';
elseif($a['channels']==4)
$colspace='devicecmyk';
else
$colspace='devicegray';
$bpc=isset($a['bits']) ? $a['bits'] : 8;
//read whole file
$f=fopen($file,'rb');
$data='';
while(!feof($f))
$data.=fread($f,8192);
fclose($f);
return array('w'=>$a[0], 'h'=>$a[1], 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'dctdecode', 'data'=>$data);
}
function _parsepng($file)
{
//extract info from a png file
$f=fopen($file,'rb');
if(!$f)
$this->error('can't open image file: '.$file);
//check signature
if($this->_readstream($f,8)!=chr(137).'png'.chr(13).chr(10).chr(26).chr(10))
$this->error('not a png file: '.$file);
//read header chunk
$this->_readstream($f,4);
if($this->_readstream($f,4)!='ihdr')
$this->error('incorrect png file: '.$file);
$w=$this->_readint($f);
$h=$this->_readint($f);
$bpc=ord($this->_readstream($f,1));
if($bpc>8)
$this->error('16-bit depth not supported: '.$file);
$ct=ord($this->_readstream($f,1));
if($ct==0)
$colspace='devicegray';
elseif($ct==2)
$colspace='devicergb';
elseif($ct==3)
$colspace='indexed';
else
$this->error('alpha channel not supported: '.$file);
if(ord($this->_readstream($f,1))!=0)
$this->error('unknown compression method: '.$file);
if(ord($this->_readstream($f,1))!=0)
$this->error('unknown filter method: '.$file);
if(ord($this->_readstream($f,1))!=0)
$this->error('interlacing not supported: '.$file);
$this->_readstream($f,4);
$parms='/decodeparms <>';
//scan chunks looking for palette, transparency and image data
$pal='';
$trns='';
$data='';
do
{
$n=$this->_readint($f);
$type=$this->_readstream($f,4);
if($type=='plte')
{
//read palette
$pal=$this->_readstream($f,$n);
$this->_readstream($f,4);
}
elseif($type=='trns')
{
//read transparency info
$t=$this->_readstream($f,$n);
if($ct==0)
$trns=array(ord(substr($t,1,1)));
elseif($ct==2)
$trns=array(ord(substr($t,1,1)), ord(substr($t,3,1)), ord(substr($t,5,1)));
else
{
$pos=strpos($t,chr(0));
if($pos!==false)
$trns=array($pos);
}
$this->_readstream($f,4);
}
elseif($type=='idat')
{
//read image data block
$data.=$this->_readstream($f,$n);
$this->_readstream($f,4);
}
elseif($type=='iend')
break;
else
$this->_readstream($f,$n+4);
}
while($n);
if($colspace=='indexed' && empty($pal))
$this->error('missing palette in '.$file);
fclose($f);
return array('w'=>$w, 'h'=>$h, 'cs'=>$colspace, 'bpc'=>$bpc, 'f'=>'flatedecode', 'parms'=>$parms, 'pal'=>$pal, 'trns'=>$trns, 'data'=>$data);
}
function _readstream($f, $n)
{
//read n bytes from stream
$res='';
while($n>0 && !feof($f))
{
$s=fread($f,$n);
if($s===false)
$this->error('error while reading stream');
$n-=strlen($s);
$res.=$s;
}
if($n>0)
$this->error('unexpected end of stream');
return $res;
}
function _readint($f)
{
//read a 4-byte integer from stream
$a=unpack('ni',$this->_readstream($f,4));
return $a['i'];
}
function _parsegif($file)
{
//extract info from a gif file (via png conversion)
if(!function_exists('imagepng'))
$this->error('gd extension is required for gif support');
if(!function_exists('imagecreatefromgif'))
$this->error('gd has no gif read support');
$im=imagecreatefromgif($file);
if(!$im)
$this->error('missing or incorrect image file: '.$file);
imageinterlace($im,0);
$tmp=tempnam('.','gif');
if(!$tmp)
$this->error('unable to create a temporary file');
if(!imagepng($im,$tmp))
$this->error('error while saving to temporary file');
imagedestroy($im);
$info=$this->_parsepng($tmp);
unlink($tmp);
return $info;
}
function _newobj()
{
//begin a new object
$this->n++;
$this->offsets[$this->n]=strlen($this->buffer);
$this->_out($this->n.' 0 obj');
}
function _putstream($s)
{
$this->_out('stream');
$this->_out($s);
$this->_out('endstream');
}
function _out($s)
{
//add a line to the document
if($this->state==2)
$this->pages[$this->page].=$s."n";
else
$this->buffer.=$s."n";
}
function _putpages()
{
$nb=$this->page;
if(!empty($this->aliasnbpages))
{
//replace number of pages
for($n=1;$n<=$nb;$n++)
$this->pages[$n]=str_replace($this->aliasnbpages,$nb,$this->pages[$n]);
}
if($this->deforientation=='p')
{
$wpt=$this->defpageformat[0]*$this->k;
$hpt=$this->defpageformat[1]*$this->k;
}
else
{
$wpt=$this->defpageformat[1]*$this->k;
$hpt=$this->defpageformat[0]*$this->k;
}
$filter=($this->compress) ? '/filter /flatedecode ' : '';
for($n=1;$n<=$nb;$n++)
{
//page
$this->_newobj();
$this->_out('<
$this->_out('/parent 1 0 r');
if(isset($this->pagesizes[$n]))
$this->_out(sprintf('/mediabox [0 0 %.2f %.2f]',$this->pagesizes[$n][0],$this->pagesizes[$n][1]));
$this->_out('/resources 2 0 r');
if(isset($this->pagelinks[$n]))
{
//links
$annots='/annots [';
foreach($this->pagelinks[$n] as $pl)
{
$rect=sprintf('%.2f %.2f %.2f %.2f',$pl[0],$pl[1],$pl[0]+$pl[2],$pl[1]-$pl[3]);
$annots.='<
if(is_string($pl[4]))
$annots.='/a <_textstring($pl[4]).'>>>>';
else
{
$l=$this->links[$pl[4]];
$h=isset($this->pagesizes[$l[0]]) ? $this->pagesizes[$l[0]][1] : $hpt;
$annots.=sprintf('/dest [%d 0 r /xyz 0 %.2f null]>>',1+2*$l[0],$h-$l[1]*$this->k);
}
}
$this->_out($annots.']');
}
$this->_out('/contents '.($this->n+1).' 0 r>>');
$this->_out('endobj');
//page content
$p=($this->compress) ? gzcompress($this->pages[$n]) : $this->pages[$n];
$this->_newobj();
$this->_out('<<'.$filter.'/length '.strlen($p).'>>');
$this->_putstream($p);
$this->_out('endobj');
}
//pages root
$this->offsets[1]=strlen($this->buffer);
$this->_out('1 0 obj');
$this->_out('<
$kids='/kids [';
for($i=0;$i<$nb;$i++)
$kids.=(3+2*$i).' 0 r ';
$this->_out($kids.']');
$this->_out('/count '.$nb);
$this->_out(sprintf('/mediabox [0 0 %.2f %.2f]',$wpt,$hpt));
$this->_out('>>');
$this->_out('endobj');
}
function _putfonts()
{
$nf=$this->n;
foreach($this->diffs as $diff)
{
//encodings
$this->_newobj();
$this->_out('<>');
$this->_out('endobj');
}
foreach($this->fontfiles as $file=>$info)
{
//font file embedding
$this->_newobj();
$this->fontfiles[$file]['n']=$this->n;
$font='';
$f=fopen($this->_getfontpath().$file,'rb',1);
if(!$f)
$this->error('font file not found');
while(!feof($f))
$font.=fread($f,8192);
fclose($f);
$compressed=(substr($file,-2)=='.z');
if(!$compressed && isset($info['length2']))
{
$header=(ord($font[0])==128);
if($header)
{
//strip first binary header
$font=substr($font,6);
}
if($header && ord($font[$info['length1']])==128)
{
//strip second binary header
$font=substr($font,0,$info['length1']).substr($font,$info['length1']+6);
}
}
$this->_out('< if($compressed)
$this->_
相关文章
- 时空中的绘旅人天宇之间怎么玩 绘旅人天宇之间活动玩法介绍 12-25
- QQ2024年度报告怎么看 2024qq年度报告玩法介绍 12-25
- 归龙潮珠砂什么时候up 归龙潮红缘绮梦卡池介绍 12-25
- 王者荣耀S38赛季有什么更新 12-25
- 王者荣耀S38赛季有什么更新 王者荣耀S38赛季更新内容介绍 12-25
- 世界之外12.25有什么更新 世界之外12月25日更新内容介绍 12-25