查看完整版本: 用PHP创建PDF中文文档

php 2007-7-28 23:12

用PHP创建PDF中文文档

我使用的是FPDF([url=http://www.fpdf.org]www.fpdf.org[/url]),下载了fpdf类库后,还要使用下面的中文类库才能支持中文,但只能使用一种中文字体(华文仿宋)。为此我烦恼了很长时间,现在终于搞定了,将TrueType字体转化为pt1字体使用:
下面是在FPDF上找的一个中文类库:

[color=#000000][color=#0000cc]<[/color][color=#0000cc]?[/color][color=#0000ff]php[/color]
[color=#0000ff]require[/color][color=#0000cc]([/color][color=#ff00ff]'fpdf.php'[/color][color=#0000cc])[/color][color=#0000cc];[/color]

[color=#0000ff]$[/color][color=#008080]Big5_widths[/color][color=#0000cc]=[/color][color=#ff0000]array[/color][color=#0000cc]([/color][color=#ff00ff]' '[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]250[color=#0000cc],[/color][color=#ff00ff]'!'[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]250[color=#0000cc],[/color][color=#ff00ff]'"'[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]408[color=#0000cc],[/color][color=#ff00ff]'#'[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]668[color=#0000cc],[/color][color=#ff00ff]'$'[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]490[color=#0000cc],[/color][color=#ff00ff]'%'[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]875[color=#0000cc],[/color][color=#ff00ff]'&'[/color][color=#0000cc]=[/color][color=#0000cc]>[/color]698[color=#0000cc],[/color][color=#ff00ff]''[/color][color=#ff00ff]'=>250,
'[/color][color=#0000cc]([/color][color=#ff00ff]'=>240,'[/color][color=#0000cc])[/color][color=#ff00ff]'=>240,'[/color][color=#0000cc]*[/color][color=#ff00ff]'=>417,'[/color][color=#0000cc]+[/color][color=#ff00ff]'=>667,'[/color][color=#0000cc],[/color][color=#ff00ff]'=>250,'[/color][color=#0000cc]-[/color][color=#ff00ff]'=>313,'[/color][color=#0000cc].[/color][color=#ff00ff]'=>250,'[/color][color=#0000cc]/[/color][color=#ff00ff]'=>520,'[/color]0[color=#ff00ff]'=>500,'[/color]1[color=#ff00ff]'=>500,
'[/color]2[color=#ff00ff]'=>500,'[/color]3[color=#ff00ff]'=>500,'[/color]4[color=#ff00ff]'=>500,'[/color]5[color=#ff00ff]'=>500,'[/color]6[color=#ff00ff]'=>500,'[/color]7[color=#ff00ff]'=>500,'[/color]8[color=#ff00ff]'=>500,'[/color]9[color=#ff00ff]'=>500,'[/color][color=#0000cc]:[/color][color=#ff00ff]'=>250,'[/color][color=#0000cc];[/color][color=#ff00ff]'=>250,
'[/color][color=#0000cc]<[/color][color=#ff00ff]'=>667,'[/color][color=#0000cc]=[/color][color=#ff00ff]'=>667,'[/color][color=#0000cc]>[/color][color=#ff00ff]'=>667,'[/color][color=#0000cc]?[/color][color=#ff00ff]'=>396,'[/color][color=#0000cc]@[/color][color=#ff00ff]'=>921,'[/color]A[color=#ff00ff]'=>677,'[/color]B[color=#ff00ff]'=>615,'[/color]C[color=#ff00ff]'=>719,'[/color]D[color=#ff00ff]'=>760,'[/color]E[color=#ff00ff]'=>625,
'[/color]F[color=#ff00ff]'=>552,'[/color]G[color=#ff00ff]'=>771,'[/color]H[color=#ff00ff]'=>802,'[/color]I[color=#ff00ff]'=>354,'[/color]J[color=#ff00ff]'=>354,'[/color]K[color=#ff00ff]'=>781,'[/color]L[color=#ff00ff]'=>604,'[/color]M[color=#ff00ff]'=>927,'[/color]N[color=#ff00ff]'=>750,'[/color]O[color=#ff00ff]'=>823,
'[/color]P[color=#ff00ff]'=>563,'[/color]Q[color=#ff00ff]'=>823,'[/color]R[color=#ff00ff]'=>729,'[/color]S[color=#ff00ff]'=>542,'[/color]T[color=#ff00ff]'=>698,'[/color]U[color=#ff00ff]'=>771,'[/color]V[color=#ff00ff]'=>729,'[/color]W[color=#ff00ff]'=>948,'[/color]X[color=#ff00ff]'=>771,'[/color]Y[color=#ff00ff]'=>677,
'[/color]Z[color=#ff00ff]'=>635,'[/color][color=#0000cc][[/color][color=#ff00ff]'=>344,'[/color][color=#ff00ff]'=>520,'[/color][color=#0000cc]][/color][color=#ff00ff]'=>344,'[/color][color=#ff00ff]^[/color][color=#ff00ff]'=>469,'[/color]_[color=#ff00ff]'=>500,'[/color][color=#ff00ff]`[/color][color=#ff00ff]'=>250,'[/color]a[color=#ff00ff]'=>469,'[/color]b[color=#ff00ff]'=>521,'[/color]c[color=#ff00ff]'=>427,
'[/color]d[color=#ff00ff]'=>521,'[/color]e[color=#ff00ff]'=>438,'[/color]f[color=#ff00ff]'=>271,'[/color]g[color=#ff00ff]'=>469,'[/color]h[color=#ff00ff]'=>531,'[/color]i[color=#ff00ff]'=>250,'[/color]j[color=#ff00ff]'=>250,'[/color]k[color=#ff00ff]'=>458,'[/color]l[color=#ff00ff]'=>240,'[/color]m[color=#ff00ff]'=>802,
'[/color]n[color=#ff00ff]'=>531,'[/color]o[color=#ff00ff]'=>500,'[/color]p[color=#ff00ff]'=>521,'[/color]q[color=#ff00ff]'=>521,'[/color]r[color=#ff00ff]'=>365,'[/color]s[color=#ff00ff]'=>333,'[/color]t[color=#ff00ff]'=>292,'[/color]u[color=#ff00ff]'=>521,'[/color]v[color=#ff00ff]'=>458,'[/color]w[color=#ff00ff]'=>677,
'[/color]x[color=#ff00ff]'=>479,'[/color]y[color=#ff00ff]'=>458,'[/color]z[color=#ff00ff]'=>427,'[/color][color=#0000cc]{[/color][color=#ff00ff]'=>480,'[/color][color=#0000cc]|[/color][color=#ff00ff]'=>496,'[/color][color=#0000cc]}[/color][color=#ff00ff]'=>480,'[/color][color=#0000cc]~[/color][color=#ff00ff]'=>667);

$GB_widths=array('[/color] [color=#ff00ff]'=>207,'[/color][color=#0000cc]![/color][color=#ff00ff]'=>270,'[/color][color=#ff00ff]"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'''=>239,
'('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,
'2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,
'<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,
'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,
'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,
'Z'=>607,'['=>374,''=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,
'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,
'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,
'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605);

class PDF_Chinese extends FPDF
{
function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
{
$i=count($this->fonts)+1;
$fontkey=strtolower($family).strtoupper($style);
$this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);
}

function AddBig5Font($family='Big5')
{
$cw=$GLOBALS['Big5_widths'];
$name='MSungStd-Light-Acro';
$CMap='ETenms-B5-H';
$registry=array('ordering'=>'CNS1','supplement'=>0);
$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
}

function AddGBFont($family='GB')
{
$cw=$GLOBALS['GB_widths'];
$name='STSongStd-Light-Acro';
$CMap='GBKp-EUC-H';
$registry=array('ordering'=>'GB1','supplement'=>2);
$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);
$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);
$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);
$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);
}

function GetStringWidth($s)
{
if($this->CurrentFont['type']=='Type0')
return $this->GetMBStringWidth($s);
else
return parent::GetStringWidth($s);
}

function GetMBStringWidth($s)
{
//Multi-byte version of GetStringWidth()
$l=0;
$cw=&$this->CurrentFont['cw'];
$nb=strlen($s);
$i=0;
while($i<$nb)
{
$c=$s[$i];
if(ord($c)<128)
{
$l+=$cw[$c];
$i++;
}
else
{
$l+=1000;
$i+=2;
}
}
return $l*$this->FontSize/1000;
}

function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
if($this->CurrentFont['type']=='Type0')
$this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
else
parent::MultiCell($w,$h,$txt,$border,$align,$fill);
}

function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
//Multi-byte version of MultiCell()
$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("[/color][color=#ff00ff]\[/color]r[color=#ff00ff]",'',$txt);
$nb=strlen($s);
if($nb>0 and $s[$nb-1]=="[/color][color=#ff00ff]\[/color]n[color=#ff00ff]"
$nb--;
$b=0;
if($border)
{
if($border==1)
{
$border='LTRB';
$b='LRT';
$b2='LR';
}
else
{
$b2='';
if(is_int(strpos($border,'L')))
$b2.='L';
if(is_int(strpos($border,'R')))
$b2.='R';
$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;
}
}
$sep=-1;
$i=0;
$j=0;
$l=0;
$ns=0;
$nl=1;
while($i<$nb)
{
//Get next character
$c=$s[$i];
//Check if ASCII or MB
$ascii=(ord($c)<128);
if($c=="[/color][color=#ff00ff]\[/color]n[color=#ff00ff]"
{
//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 and $nl==2)
$b=$b2;
continue;
}
if(!$ascii)
{
$sep=$i;
$ls=$l;
}
elseif($c==' ')
{
$sep=$i;
$ls=$l;
$ns++;
}
$l+=$ascii ? $cw[$c] : 1000;
if($l>$wmax)
{
//Automatic line break
if($sep==-1 or $i==$j)
{
if($i==$j)
$i+=$ascii ? 1 : 2;
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')
{
if($s[$sep]==' ')
$ns--;
if($s[$i-1]==' ')
{
$ns--;
$ls-=$cw[' '];
}
$this->ws=($ns>0) ? ($wmax-$ls)/1000*$this->FontSize/$ns : 0;
$this->_out(sprintf('%.3f Tw',$this->ws*$this->k));
}
$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);
$i=($s[$sep]==' ') ? $sep+1 : $sep;
}
$sep=-1;
$j=$i;
$l=0;
$ns=0;
$nl++;
if($border and $nl==2)
$b=$b2;
}
else
$i+=$ascii ? 1 : 2;
}
//Last chunk
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
if($border and is_int(strpos($border,'B')))
$b.='B';
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->x=$this->lMargin;
}

function Write($h,$txt,$link='')
{
if($this->CurrentFont['type']=='Type0')
$this->MBWrite($h,$txt,$link);
else
parent::Write($h,$txt,$link);
}

function MBWrite($h,$txt,$link)
{
//Multi-byte version of Write()
$cw=&$this->CurrentFont['cw'];
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$s=str_replace("[/color][color=#ff00ff]\[/color]r[color=#ff00ff]",'',$txt);
$nb=strlen($s);
$sep=-1;
$i=0;
$j=0;
$l=0;
$nl=1;
while($i<$nb)
{
//Get next character
$c=$s[$i];
//Check if ASCII or MB
$ascii=(ord($c)<128);
if($c=="[/color][color=#ff00ff]\[/color]n[/color]


将以上代码存为chinese.php即可引用。但用它只能得到一种字体。为了支持所有中文字体,可用ttf2pt1程序将TrueType字体转化pt1 字体,一个一个地转(具体方法在FPDF的教程中有详细说明)。为了支持其他中文字体,养分要修改上面的chinese.php,如下:

1: Replace the following line in the AddGBFont() method:

function AddGBFont($family='GB',$name='STSongStd-Light-Acro')
{
$cw=$GLOBALS['GB_widths'];
// $name='STSongStd-Light-Acro';
$CMap='GBKp-EUC-H';
........

2: This is a Sample.

[color=#000000][color=#0000cc][color=#000000][color=#0000cc][/color][color=#0000cc]

[color=#000000][color=#0000cc]<[/color][color=#0000cc]?[/color][color=#0000ff]php[/color]
[color=#0000ff]require[/color][color=#0000cc]([/color][color=#ff00ff]'chinese.php'[/color][color=#0000cc])[/color][color=#0000cc];[/color]

[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]=[/color][color=#0000ff]new[/color] PDF_Chinese[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]AddGBFont[color=#0000cc]([/color][color=#ff00ff]'simsun'[/color][color=#0000cc],[/color][color=#ff00ff]'宋体'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]AddGBFont[color=#0000cc]([/color][color=#ff00ff]'simhei'[/color][color=#0000cc],[/color][color=#ff00ff]'黑体'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]AddGBFont[color=#0000cc]([/color][color=#ff00ff]'simkai'[/color][color=#0000cc],[/color][color=#ff00ff]'楷体_GB2312'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]AddGBFont[color=#0000cc]([/color][color=#ff00ff]'sinfang'[/color][color=#0000cc],[/color][color=#ff00ff]'仿宋_GB2312'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Open[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]AddPage[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color][color=#ff00ff]'simsun'[/color][color=#0000cc],[/color][color=#ff00ff]''[/color][color=#0000cc],[/color]20[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Write[color=#0000cc]([/color]10[color=#0000cc],[/color][color=#ff00ff]'简体中文汉字');
$pdf->SetFont('[/color]simhei[color=#ff00ff]','[/color][color=#ff00ff]',20);
$pdf->Write(10,'[/color]简体中文汉字’[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color][color=#ff00ff]'simkai'[/color][color=#0000cc],[/color][color=#ff00ff]''[/color][color=#0000cc],[/color]20[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Write[color=#0000cc]([/color]10[color=#0000cc],[/color][color=#ff00ff]'简体中文汉字'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]SetFont[color=#0000cc]([/color][color=#ff00ff]'sinfang'[/color][color=#0000cc],[/color][color=#ff00ff]''[/color][color=#0000cc],[/color]20[color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Write[color=#0000cc]([/color]10[color=#0000cc],[/color][color=#ff00ff]'简体中文汉字'[/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000ff]$[/color][color=#008080]pdf[/color][color=#0000cc]-[/color][color=#0000cc]>[/color]Output[color=#0000cc]([/color][color=#0000cc])[/color][color=#0000cc];[/color]
[color=#0000cc]?[/color][color=#0000cc]>[/color][/color]
[/color][/color][/color][/color]
页: [1]
查看完整版本: 用PHP创建PDF中文文档