用PHP创建PDF中文文档

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

下面是在FPDF上找的一个中文类库:

  1   
  2require('fpdf.php'); 
  3
  4$Big5_widths=array(' '=>250,'!'=>250,'"'=>408,'#'=>668,'$'=>490,'%'=>875,'&'=>698,'''=>250,   
  5'('=>240,')'=>240,'*'=>417,'+'=>667,','=>250,'-'=>313,'.'=>250,'/'=>520,'0'=>500,'1'=>500,   
  6'2'=>500,'3'=>500,'4'=>500,'5'=>500,'6'=>500,'7'=>500,'8'=>500,'9'=>500,':'=>250,';'=>250,   
  7'<'=>667,'='=>667,'>'=>667,'?'=>396,'@'=>921,'A'=>677,'B'=>615,'C'=>719,'D'=>760,'E'=>625,   
  8'F'=>552,'G'=>771,'H'=>802,'I'=>354,'J'=>354,'K'=>781,'L'=>604,'M'=>927,'N'=>750,'O'=>823,   
  9'P'=>563,'Q'=>823,'R'=>729,'S'=>542,'T'=>698,'U'=>771,'V'=>729,'W'=>948,'X'=>771,'Y'=>677,   
 10'Z'=>635,'['=>344,''=>520,']'=>344,'^'=>469,'_'=>500,'`'=>250,'a'=>469,'b'=>521,'c'=>427,   
 11'd'=>521,'e'=>438,'f'=>271,'g'=>469,'h'=>531,'i'=>250,'j'=>250,'k'=>458,'l'=>240,'m'=>802,   
 12'n'=>531,'o'=>500,'p'=>521,'q'=>521,'r'=>365,'s'=>333,'t'=>292,'u'=>521,'v'=>458,'w'=>677,   
 13'x'=>479,'y'=>458,'z'=>427,'{'=>480,'|'=>496,'}'=>480,'~'=>667); 
 14
 15$GB_widths=array(' '=>207,'!'=>270,'"'=>342,'#'=>467,'$'=>462,'%'=>797,'&'=>710,'''=>239,   
 16'('=>374,')'=>374,'*'=>423,'+'=>605,','=>238,'-'=>375,'.'=>238,'/'=>334,'0'=>462,'1'=>462,   
 17'2'=>462,'3'=>462,'4'=>462,'5'=>462,'6'=>462,'7'=>462,'8'=>462,'9'=>462,':'=>238,';'=>238,   
 18'<'=>605,'='=>605,'>'=>605,'?'=>344,'@'=>748,'A'=>684,'B'=>560,'C'=>695,'D'=>739,'E'=>563,   
 19'F'=>511,'G'=>729,'H'=>793,'I'=>318,'J'=>312,'K'=>666,'L'=>526,'M'=>896,'N'=>758,'O'=>772,   
 20'P'=>544,'Q'=>772,'R'=>628,'S'=>465,'T'=>607,'U'=>753,'V'=>711,'W'=>972,'X'=>647,'Y'=>620,   
 21'Z'=>607,'['=>374,''=>333,']'=>374,'^'=>606,'_'=>500,'`'=>239,'a'=>417,'b'=>503,'c'=>427,   
 22'd'=>529,'e'=>415,'f'=>264,'g'=>444,'h'=>518,'i'=>241,'j'=>230,'k'=>495,'l'=>228,'m'=>793,   
 23'n'=>527,'o'=>524,'p'=>524,'q'=>504,'r'=>338,'s'=>336,'t'=>277,'u'=>517,'v'=>450,'w'=>652,   
 24'x'=>466,'y'=>452,'z'=>407,'{'=>370,'|'=>258,'}'=>370,'~'=>605); 
 25
 26class PDF_Chinese extends FPDF   
 27{   
 28function AddCIDFont($family,$style,$name,$cw,$CMap,$registry)   
 29{   
 30$i=count($this->fonts)+1;   
 31$fontkey=strtolower($family).strtoupper($style);   
 32$this->fonts[$fontkey]=array('i'=>$i,'type'=>'Type0','name'=>$name,'up'=>-120,'ut'=>40,'cw'=>$cw,'CMap'=>$CMap,'registry'=>$registry);   
 33} 
 34
 35function AddBig5Font($family='Big5')   
 36{   
 37$cw=$GLOBALS['Big5_widths'];   
 38$name='MSungStd-Light-Acro';   
 39$CMap='ETenms-B5-H';   
 40$registry=array('ordering'=>'CNS1','supplement'=>0);   
 41$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);   
 42$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);   
 43$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);   
 44$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);   
 45} 
 46
 47function AddGBFont($family='GB')   
 48{   
 49$cw=$GLOBALS['GB_widths'];   
 50$name='STSongStd-Light-Acro';   
 51$CMap='GBKp-EUC-H';   
 52$registry=array('ordering'=>'GB1','supplement'=>2);   
 53$this->AddCIDFont($family,'',$name,$cw,$CMap,$registry);   
 54$this->AddCIDFont($family,'B',$name.',Bold',$cw,$CMap,$registry);   
 55$this->AddCIDFont($family,'I',$name.',Italic',$cw,$CMap,$registry);   
 56$this->AddCIDFont($family,'BI',$name.',BoldItalic',$cw,$CMap,$registry);   
 57} 
 58
 59function GetStringWidth($s)   
 60{   
 61if($this->CurrentFont['type']=='Type0')   
 62return $this->GetMBStringWidth($s);   
 63else   
 64return parent::GetStringWidth($s);   
 65} 
 66
 67function GetMBStringWidth($s)   
 68{   
 69//Multi-byte version of GetStringWidth()   
 70$l=0;   
 71$cw=&$this->CurrentFont['cw'];   
 72$nb=strlen($s);   
 73$i=0;   
 74while($i<$nb)   
 75{   
 76$c=$s[$i];   
 77if(ord($c)<128)   
 78{   
 79$l+=$cw[$c];   
 80$i++;   
 81}   
 82else   
 83{   
 84$l+=1000;   
 85$i+=2;   
 86}   
 87}   
 88return $l*$this->FontSize/1000;   
 89} 
 90
 91function MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)   
 92{   
 93if($this->CurrentFont['type']=='Type0')   
 94$this->MBMultiCell($w,$h,$txt,$border,$align,$fill);   
 95else   
 96parent::MultiCell($w,$h,$txt,$border,$align,$fill);   
 97} 
 98
 99function MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)   
100{   
101//Multi-byte version of MultiCell()   
102$cw=&$this->CurrentFont['cw'];   
103if($w==0)   
104$w=$this->w-$this->rMargin-$this->x;   
105$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;   
106$s=str_replace("\r",'',$txt);   
107$nb=strlen($s);   
108if($nb>0 and $s[$nb-1]=="\n"   
109$nb--;   
110$b=0;   
111if($border)   
112{   
113if($border==1)   
114{   
115$border='LTRB';   
116$b='LRT';   
117$b2='LR';   
118}   
119else   
120{   
121$b2='';   
122if(is_int(strpos($border,'L')))   
123$b2.='L';   
124if(is_int(strpos($border,'R')))   
125$b2.='R';   
126$b=is_int(strpos($border,'T')) ? $b2.'T' : $b2;   
127}   
128}   
129$sep=-1;   
130$i=0;   
131$j=0;   
132$l=0;   
133$ns=0;   
134$nl=1;   
135while($i<$nb)   
136{   
137//Get next character   
138$c=$s[$i];   
139//Check if ASCII or MB   
140$ascii=(ord($c)<128);   
141if($c=="\n"   
142{   
143//Explicit line break   
144if($this->ws>0)   
145{   
146$this->ws=0;   
147$this->_out('0 Tw');   
148}   
149$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);   
150$i++;   
151$sep=-1;   
152$j=$i;   
153$l=0;   
154$ns=0;   
155$nl++;   
156if($border and $nl==2)   
157$b=$b2;   
158continue;   
159}   
160if(!$ascii)   
161{   
162$sep=$i;   
163$ls=$l;   
164}   
165elseif($c==' ')   
166{   
167$sep=$i;   
168$ls=$l;   
169$ns++;   
170}   
171$l+=$ascii ? $cw[$c] : 1000;   
172if($l>$wmax)   
173{   
174//Automatic line break   
175if($sep==-1 or $i==$j)   
176{   
177if($i==$j)   
178$i+=$ascii ? 1 : 2;   
179if($this->ws>0)   
180{   
181$this->ws=0;   
182$this->_out('0 Tw');   
183}   
184$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);   
185}   
186else   
187{   
188if($align=='J')   
189{   
190if($s[$sep]==' ')   
191$ns--;   
192if($s[$i-1]==' ')   
193{   
194$ns--;   
195$ls-=$cw[' '];   
196}   
197$this->ws=($ns>0) ? ($wmax-$ls)/1000*$this->FontSize/$ns : 0;   
198$this->_out(sprintf('%.3f Tw',$this->ws*$this->k));   
199}   
200$this->Cell($w,$h,substr($s,$j,$sep-$j),$b,2,$align,$fill);   
201$i=($s[$sep]==' ') ? $sep+1 : $sep;   
202}   
203$sep=-1;   
204$j=$i;   
205$l=0;   
206$ns=0;   
207$nl++;   
208if($border and $nl==2)   
209$b=$b2;   
210}   
211else   
212$i+=$ascii ? 1 : 2;   
213}   
214//Last chunk   
215if($this->ws>0)   
216{   
217$this->ws=0;   
218$this->_out('0 Tw');   
219}   
220if($border and is_int(strpos($border,'B')))   
221$b.='B';   
222$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);   
223$this->x=$this->lMargin;   
224} 
225
226function Write($h,$txt,$link='')   
227{   
228if($this->CurrentFont['type']=='Type0')   
229$this->MBWrite($h,$txt,$link);   
230else   
231parent::Write($h,$txt,$link);   
232} 
233
234function MBWrite($h,$txt,$link)   
235{   
236//Multi-byte version of Write()   
237$cw=&$this->CurrentFont['cw'];   
238$w=$this->w-$this->rMargin-$this->x;   
239$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;   
240$s=str_replace("\r",'',$txt);   
241$nb=strlen($s);   
242$sep=-1;   
243$i=0;   
244$j=0;   
245$l=0;   
246$nl=1;   
247while($i<$nb)   
248{   
249//Get next character   
250$c=$s[$i];   
251//Check if ASCII or MB   
252$ascii=(ord($c)<128);   
253if($c=="\n"   
254{   
255//Explicit line break   
256$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);   
257$i++;   
258$sep=-1;   
259$j=$i;   
260$l=0;   
261if($nl==1)   
262{   
263$this->x=$this->lMargin;   
264$w=$this->w-$this->rMargin-$this->x;   
265$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;   
266}   
267$nl++;   
268continue;   
269}   
270if(!$ascii or $c==' ')   
271$sep=$i;   
272$l+=$ascii ? $cw[$c] : 1000;   
273if($l>$wmax)   
274{   
275//Automatic line break   
276if($sep==-1 or $i==$j)   
277{   
278if($this->x>$this->lMargin)   
279{   
280//Move to next line   
281$this->x=$this->lMargin;   
282$this->y+=$h;   
283$w=$this->w-$this->rMargin-$this->x;   
284$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;   
285$i++;   
286$nl++;   
287continue;   
288}   
289if($i==$j)   
290$i+=$ascii ? 1 : 2;   
291$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);   
292}   
293else   
294{   
295$this->Cell($w,$h,substr($s,$j,$sep-$j),0,2,'',0,$link);   
296$i=($s[$sep]==' ') ? $sep+1 : $sep;   
297}   
298$sep=-1;   
299$j=$i;   
300$l=0;   
301if($nl==1)   
302{   
303$this->x=$this->lMargin;   
304$w=$this->w-$this->rMargin-$this->x;   
305$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;   
306}   
307$nl++;   
308}   
309else   
310$i+=$ascii ? 1 : 2;   
311}   
312//Last chunk   
313if($i!=$j)   
314$this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);   
315} 
316
317function _putfonts()   
318{   
319$nf=$this->n;   
320foreach($this->diffs as $diff)   
321{   
322//Encodings   
323$this->_newobj();   
324$this->_out('<
325>');   
326$this->_out('endobj');   
327}   
328$mqr=get_magic_quotes_runtime();   
329set_magic_quotes_runtime(0);   
330foreach($this->FontFiles as $file=>$info)   
331{   
332//Font file embedding   
333$this->_newobj();   
334$this->FontFiles[$file]['n']=$this->n;   
335if(defined('FPDF_FONTPATH'))   
336$file=FPDF_FONTPATH.$file;   
337$size=filesize($file);   
338if(!$size)   
339$this->Error('Font file not found');   
340$this->_out('<
341_out('/Filter /FlateDecode');   
342$this->_out('/Length1 '.$info['length1']);   
343if(isset($info['length2']))   
344$this->_out('/Length2 '.$info['length2'].' /Length3 0');   
345$this->_out('>>');   
346$f=fopen($file,'rb');   
347$this->_putstream(fread($f,$size));   
348fclose($f);   
349$this->_out('endobj');   
350}   
351set_magic_quotes_runtime($mqr);   
352foreach($this->fonts as $k=>$font)   
353{   
354//Font objects   
355$this->_newobj();   
356$this->fonts[$k]['n']=$this->n;   
357$this->_out('<
358_putType0($font);   
359else   
360{   
361$name=$font['name'];   
362$this->_out('/BaseFont /'.$name);   
363if($font['type']=='core')   
364{   
365//Standard font   
366$this->_out('/Subtype /Type1');   
367if($name!='Symbol' and $name!='ZapfDingbats')   
368$this->_out('/Encoding /WinAnsiEncoding');   
369}   
370else   
371{   
372//Additional font   
373$this->_out('/Subtype /'.$font['type']);   
374$this->_out('/FirstChar 32');   
375$this->_out('/LastChar 255');   
376$this->_out('/Widths '.($this->n+1).' 0 R');   
377$this->_out('/FontDescriptor '.($this->n+2).' 0 R');   
378if($font['enc'])   
379{   
380if(isset($font['diff']))   
381$this->_out('/Encoding '.($nf+$font['diff']).' 0 R');   
382else   
383$this->_out('/Encoding /WinAnsiEncoding');   
384}   
385}   
386$this->_out('>>');   
387$this->_out('endobj');   
388if($font['type']!='core')   
389{   
390//Widths   
391$this->_newobj();   
392$cw=&$font['cw'];   
393$s='[';   
394for($i=32;$i<=255;$i++)   
395$s.=$cw[chr($i)].' ';   
396$this->_out($s.']');   
397$this->_out('endobj');   
398//Descriptor   
399$this->_newobj();   
400$s='<
401$v)   
402$s.=' /'.$k.' '.$v;   
403$file=$font['file'];   
404if($file)   
405$s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';   
406$this->_out($s.'>>');   
407$this->_out('endobj');   
408}   
409}   
410}   
411} 
412
413function _putType0($font)   
414{   
415//Type0   
416$this->_out('/Subtype /Type0');   
417$this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);   
418$this->_out('/Encoding /'.$font['CMap']);   
419$this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');   
420$this->_out('>>');   
421$this->_out('endobj');   
422//CIDFont   
423$this->_newobj();   
424$this->_out('<
425_out('/Subtype /CIDFontType0');   
426$this->_out('/BaseFont /'.$font['name']);   
427$this->_out('/CIDSystemInfo <
428>');   
429$this->_out('/FontDescriptor '.($this->n+1).' 0 R');   
430$W='/W [1 [';   
431foreach($font['cw'] as $w)   
432$W.=$w.' ';   
433$this->_out($W.']]');   
434$this->_out('>>');   
435$this->_out('endobj');   
436//Font descriptor   
437$this->_newobj();   
438$this->_out('<
439_out('/FontName /'.$font['name']);   
440$this->_out('/Flags 6');   
441$this->_out('/FontBBox [0 0 1000 1000]');   
442$this->_out('/ItalicAngle 0');   
443$this->_out('/Ascent 1000');   
444$this->_out('/Descent 0');   
445$this->_out('/CapHeight 1000');   
446$this->_out('/StemV 10');   
447$this->_out('>>');   
448$this->_out('endobj');   
449}   
450}   

将以上代码存为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.

 1   
 2require('chinese.php'); 
 3
 4$pdf=new PDF_Chinese();   
 5$pdf->AddGBFont('simsun','宋体');   
 6$pdf->AddGBFont('simhei','黑体');   
 7$pdf->AddGBFont('simkai','楷体_GB2312');   
 8$pdf->AddGBFont('sinfang','仿宋_GB2312'');   
 9$pdf->Open();   
10$pdf->AddPage();   
11$pdf->SetFont('simsun','',20);   
12$pdf->Write(10,'简体中文汉字‘);   
13$pdf->SetFont('simhei','',20);   
14$pdf->Write(10,'简体中文汉字’);   
15$pdf->SetFont('simkai','',20);   
16$pdf->Write(10,'简体中文汉字‘);   
17$pdf->SetFont('sinfang','',20);   
18$pdf->Write(10,'简体中文汉字’);   
19$pdf->Output();   
Published At
Categories with Web编程
Tagged with
comments powered by Disqus