天天看點

用PHP建立PDF中文文檔

<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> <script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

$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');

}

}

?>

将以上代碼存為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.

<?PHP

require('chinese.PHP');

$PDF=new PDF_Chinese();

$PDF->AddGBFont('simsun','宋體');

$PDF->AddGBFont('simhei','黑體');

$PDF->AddGBFont('simkai','楷體_GB2312');

$PDF->AddGBFont('sinfang','仿宋_GB2312'');

$PDF->Open();

$PDF->AddPage();

$PDF->SetFont('simsun','',20);

$PDF->Write(10,'簡體中文漢字‘);

$PDF->SetFont('simhei','',20);

$PDF->Write(10,'簡體中文漢字’);

$PDF->SetFont('simkai','',20);

$PDF->Write(10,'簡體中文漢字‘);

$PDF->SetFont('sinfang','',20);

$PDF->Write(10,'簡體中文漢字’);

$PDF->Output();

?>

  <script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>

<script type="text/javascript"> </script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>