PHP to create PDF Chinese document program code

Source: Internet
Author: User
Tags foreach continue diff ord strlen

I am using Fpdf (www.fpdf.org), after downloading the Fpdf class library, but also use the following Chinese class library to support Chinese, but only one Chinese font (Chinese imitation). I've been bothering for a long time, and now I'm done. Convert TrueType fonts to pt1 font usage:

The following is a library of Chinese classes found on the fpdf:

<?php
Require (' fpdf.php ');
$Big 5_widths=array (' =>250, '! ') =>250, ' =>408 ', ' # ' =>668, ' $ ' =>490, '% ' =>875, ' & ' =>698, ' =>250,
' (' =>240, ') ' =>240, ' * ' =>417, ' + ' =>667, ', ' =>250, '-' =>313, '. ' =>250, '/' =>520, ' 0 ' =>500, ' 1 ' =>500,
' 2 ' =>500, ' 3 ' =>500, ' 4 ' =>500, ' 5 ' =>500, ' 6 ' =>500, ' 7 ' =>500, ' 8 ' =>500, ' 9 ' =>500, ': ' =>250, '; =&GT;250,
' < ' =>667, ' = ' =>667 ', ' > ' =>667, '? ' =>396, ' @ ' =>921, ' A ' =>677, ' B ' =>615, ' C ' =>719, ' D ' =>760, ' E ' =>625,
' F ' =>552, ' G ' =>771, ' H ' =>802, ' I ' =>354, ' J ' =>354, ' K ' =>781, ' L ' =>604, ' M ' =>927, ' N ' =>750, ' O ' =>823,
' P ' =>563, ' Q ' =>823, ' R ' =>729, ' S ' =>542, ' T ' =>698, ' U ' =>771, ' V ' =>729, ' W ' =>948, ' X ' =>771, ' Y ' =>677,
' Z ' =>635, ' [' =>344, ' =>520, '] ' =>344, ' ^ ' =>469, ' _ ' =>500, ' ' =>250, ' a ' =>469, ' B ' =>521, ' C ' =>427,
' d ' =>521, ' e ' =>438, ' f ' =>271, ' G ' =>469, ' h ' =>531, ' I ' =>250, ' j ' =>250, ' K ' =>458, ' l ' =>240, ' M ' =>802,
' N ' =>531, ' o ' =>500, ' P ' =>521, ' Q ' =>521, ' R ' =>365, ' s ' =>333, ' t ' =>292, ' u ' =>521, ' V ' =>458, ' W ' =>677,
' x ' =>479, ' y ' =>458, ' z ' =>427, ' {' =>480, ' | ' =>496, '} ' =>480, ' ~ ' =>667);
$GB _widths=array (' =>207, '! ') =>270, ' =>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 ("R", ", $txt");
$NB =strlen ($s);
if ($nb >0 and $s [$nb -1]== \ n]
$NB--;
$b = 0;
if ($border)
{
if ($border ==1)
{
$border = ' LTRB ';
$b = ' LRT ';
$b 2= ' LR ';
}
Else
{
$b 2 = ';
if (Is_int (Strpos ($border, ' L '))
$b 2.= ' L ';
if (Is_int (Strpos ($border, ' R '))
$b 2.= ' R ';
$b =is_int (Strpos ($border, ' T '))? $b 2 ' T ': $b 2;
}
}
$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 = = "\ 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 and $NL ==2)
$b = $b 2;
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 = $b 2;
}
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 ("R", ", $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 = = "\ 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 (! $ascii or $c = = ")
$sep = $i;
$l = $ascii? $CW [$c]: 1000;
if ($l > $wmax)
{
Automatic line break
if ($sep ==-1 or $i = = $j)
{
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 = $ascii? 1:2;
$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 = ($s [$sep]== ')? $SEP + 1: $sep;
}
$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 = $ascii? 1:2;
}
Last Chunk
if ($i!= $j)
$this->cell ($l/1000* $this->fontsize, $h, substr ($s, $j, $i-$j), 0,0, ', 0, $link);
}
function _putfonts ()
{
$NF = $this->n;
foreach ($this->diffs as $diff)
{
Encodings
$this->_newobj ();
$this->_out (' <</type/encoding/baseencoding/winansiencoding/differences ['. $diff. ']>> ');
$this->_out (' endobj ');
}
$MQR =get_magic_quotes_runtime ();
Set_magic_quotes_runtime (0);
foreach ($this->fontfiles as $file => $info)
{
Font file Embedding
$this->_newobj ();
$this->fontfiles[$file] [' n ']= $this->n;
if (defined (' Fpdf_fontpath '))
$file =fpdf_fontpath. $file;
$size =filesize ($file);
if (! $size)
$this->error (' Font file not found ');
$this->_out (' <</length '. $size);
if (substr ($file, -2) = = '. Z ')
$this->_out ('/filter/flatedecode ');
$this->_out ('/length1 '. $info [' length1 ']);
if (Isset ($info [' length2 '])
$this->_out ('/length2 '. $info [' length2 ']. '/length3 0 ');
$this->_out (' >> ');
$f =fopen ($file, ' RB ');
$this->_putstream (Fread ($f, $size));
Fclose ($f);
$this->_out (' endobj ');
}
Set_magic_quotes_runtime ($MQR);
foreach ($this->fonts as $k => $font)
{
Font objects
$this->_newobj ();
$this->fonts[$k] [' n ']= $this->n;
$this->_out (' <</type/font ');
if ($font [' type ']== ' TYPE0 ')
$this-&GT;_PUTTYPE0 ($font);
Else
{
$name = $font [' name '];
$this->_out ('/basefont/'. $name);
if ($font [' type ']== ' core ')
{
Standard Font
$this->_out ('/subtype/type1 ');
if ($name!= ' Symbol ' and $name!= ' ZapfDingbats ')
$this->_out ('/encoding/winansiencoding ');
}
Else
{
Additional Font
$this->_out ('/subtype/'. $font [' type ']);
$this->_out ('/firstchar 32 ');
$this->_out ('/lastchar 255 ');
$this->_out ('/widths '). ( $this->n+1). ' 0 R ');
$this->_out ('/fontdescriptor '). ( $this->n+2). ' 0 R ');
if ($font [' enc '])
{
if (Isset ($font [' diff '])
$this->_out ('/encoding '). ( $NF + $font [' diff ']). ' 0 R ');
Else
$this->_out ('/encoding/winansiencoding ');
}
}
$this->_out (' >> ');
$this->_out (' endobj ');
if ($font [' type ']!= ' core ')
{
Widths
$this->_newobj ();
$CW =& $font [' CW '];
$s = ' [';
For ($i =32 $i <=255; $i + +)
$s. = $CW [Chr ($i)]. ' ';
$this->_out ($s. ']);
$this->_out (' endobj ');
Descriptor
$this->_newobj ();
$s = ' <</type/fontdescriptor/fontname/'. $name;
foreach ($font [' desc '] as $k => $v)
$s. = '/'. $k. $v;
$file = $font [' file '];
if ($file)
$s. = '/fontfile '. ($font [' type ']== ' Type1 '?) ': ' 2 '). ' '. $this->fontfiles[$file] [' n ']. ' 0 R ';
$this->_out ($s. ' >> ');
$this->_out (' endobj ');
}
}
}
}
function _puttype0 ($font)
{
Type0
$this->_out ('/subtype/type0 ');
$this->_out ('/basefont/'. $font [' name ']. -'. $font [' CMap ']);
$this->_out ('/encoding/'. $font [' CMap ']);
$this->_out ('/descendantfonts [ $this->n+1). ' 0 R] ');
$this->_out (' >> ');
$this->_out (' endobj ');
Cidfont
$this->_newobj ();
$this->_out (' <</type/font ');
$this->_out ('/subtype/cidfonttype0 ');
$this->_out ('/basefont/'. $font [' name ']);
$this->_out ('/cidsysteminfo <</registry (Adobe)/ordering ('. $font [' Registry '] [' ordering ']. ')/supplement ' . $font [' Registry '] [' Supplement ']. ' >> ');
$this->_out ('/fontdescriptor '). ( $this->n+1). ' 0 R ');
$W = '/w [1];
foreach ($font [' CW '] as $W)
$W. = $w. ';
$this->_out ($W. ']];
$this->_out (' >> ');
$this->_out (' endobj ');
Font Descriptor
$this->_newobj ();
$this->_out (' <</type/fontdescriptor ');
$this->_out ('/fontname/'. $font [' name ']);
$this->_out ('/flags 6 ');
$this->_out ('/fontbbox [0 0 1000 1000] ');
$this->_out ('/italicangle 0 ');
$this->_out ('/ascent 1000 ');
$this->_out ('/descent 0 ');
$this->_out ('/capheight 1000 ');
$this->_out ('/stemv 10 ');
$this->_out (' >> ');
$this->_out (' endobj ');
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.