怎麼用PHP建立PDF中文文檔
怎麼用PHP建立PDF中文文檔
我使用的是FPDF(www.fpdf.org),下載了fpdf類別庫後,還要使用下面的中文類庫來支援中文,但只能使用一種中文字體(華文仿宋)。為此我煩惱了很長時間,現在終於搞定了,將TrueType字體轉換為pt1字體使用:
下面是在FPDF上找的一個中文類別庫:
require('fpdf.php');
$Big5_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,';'=>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);
}
函數AddGBFont($family='GB')
{
$cw=$GLOBALS['GB_widths'];
$name='STSongStd-Light-Acro';
$CMap='GBKp-EUC-H';
$registry=array('排序'=>'GB1','補充'=>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);
}
函數 GetStringWidth($s)
{
if($this->CurrentFont['type']=='Type0')
返回 $this->GetMBStringWidth($s);
其他
返回父級::GetStringWidth($s);
}
函數 GetMBStringWidth($s)
{
//GetStringWidth()的多位元組版本
$l=0;
$cw=&$this->CurrentFont['cw'];
$nb=strlen($s);
$i=0;
而($i
{
$c=$s[$i];
if(ord($c)
{
$l+=$cw[$c];
$i++;
}
其他
{
$l+=1000;
$i+=2;
}
}
返回 $l*$this->FontSize/1000;
}
函數 MultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
if($this->CurrentFont['type']=='Type0')
$this->MBMultiCell($w,$h,$txt,$border,$align,$fill);
其他
父::MultiCell($w,$h,$txt,$border,$align,$fill);
}
函數 MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
//MultiCell()的多位元組版本
$cw=&$this->CurrentFont['cw'];
如果($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==1)
{
$border='LTRB';
$b='輕軌';
$b2='LR';
}
其他
{
$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;
而($i
{
//取得下一個字元
$c=$s[$i];
//檢查是ASCII還是MB
$ascii=(ord($c)
if($c=="n"
{
//明確換行
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;
繼續;
}
if(!$ascii)
{
$sep=$i;
$ls=$l;
}
elseif($c==' ')
{
$sep=$i;
$ls=$l;
$ns++;
}
$l+=$ascii ? $cw[$c] : 1000;
如果($l>$wmax)
{
//自動換行
if($sep==-1 或 $i==$j)
{
如果($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);
}
其他
{
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 和 $nl==2)
$b=$b2;
}
其他
$i+=$ascii ? 1 : 2;
}
//最後一塊
if($this->ws>0)
{
$this->ws=0;
$this->_out('0 Tw');
}
if($border 和 is_int(strpos($border,'B')))
$b.='B';
$this->Cell($w,$h,substr($s,$j,$i-$j),$b,2,$align,$fill);
$this->x=$this->lMargin;
}
函數 Write($h,$txt,$link='')
{
if($this->CurrentFont['type']=='Type0')
$this->MBWrite($h,$txt,$link);
其他
父::寫($h,$txt,$link);
}
函數 MBWrite($h,$txt,$link)
{
//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;
而($i
{
//取得下一個字元
$c=$s[$i];
//檢查是ASCII還是MB
$ascii=(ord($c)
if($c=="n"
{
//明確換行
$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++;
繼續;
}
if(!$ascii 或 $c==' ')
$sep=$i;
$l+=$ascii ? $cw[$c] : 1000;
如果($l>$wmax)
{
//自動換行
if($sep==-1 或 $i==$j)
{
if($this->x>$this->lMargin)
{
//移至下一行
$this->x=$this->lMargin;
$this->y+=$h;
$w=$this->w-$this->rMargin-$this->x;
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
$i++;
$nl++;
繼續;
}
如果($i==$j)
$i+=$ascii ? 1 : 2;
$this->Cell($w,$h,substr($s,$j,$i-$j),0,2,'',0,$link);
}
其他
{
$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++;
}
其他
$i+=$ascii ? 1 : 2;
}
//最後一塊
如果($i!=$j)
$this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
}
函數_putfonts()
{
$nf=$this->n;
foreach($this->diffs as $diff)
{
// 編碼
$this->_newobj();
$this->_out('>');
$this->_out('endobj');
}
$mqr=get_magic_quotes_runtime();
set_magic_quotes_runtime(0);
foreach($this->FontFiles as $file=>$info)
{
// 字型檔嵌入
$this->_newobj();
$this->FontFiles[$file]['n']=$this->n;
if(已定義('FPDF_FONTPATH'))
$file=FPDF_FONTPATH.$file;
$大小=檔案大小($檔案);
if(!$size)
$this->Error('找不到字體檔案');
$this->_out('
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)
{
// 字體物件
$this->_newobj();
$this->字體[$k]['n']=$this->n;
$this->_out('
if($font['type']=='Type0')
$this->_putType0($font);
其他
{
$name=$font['name'];
$this->_out('/BaseFont /'.$name);
if($font['type']=='core')
{
//標準字體
$this->_out('/Subtype /Type1');
if($name!='Symbol' and $name!='ZapfDingbats')
$this->_out('/Encoding /WinAnsiEncoding');
}
其他
{
// 附加字體
$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');
其他
$this->_out('/Encoding /WinAnsiEncoding');
}
}
$this->_out('>>');
$this->_out('endobj');
if($font['type']!='core')
{
//寬度
$this->_newobj();
$cw=&$font['cw'];
$s='[';
for($i=32;$i
$s.=$cw[chr($i)].' ';
$this->_out($s.']');
$this->_out('endobj');
// 回覆
$this->_newobj();
$s='
foreach($font['desc'] as $k=>$v)
$s.='/'.$k.' '.$v;
$file=$font['file'];
如果($文件)
$s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->FontFiles[$file]['n'].' 0 R';
$this->_out($s.'>>');
$this->_out('endobj');
}
}
}
}
函數_putType0($font)
{
//型0
$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('
$this->_out('/Subtype /CIDFontType0');
$this->_out('/BaseFont /'.$font['name']);
$this->_out('/CIDSystemInfo >');
$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');
// 字型申請
$this->_newobj();
$this->_out('
$this->_out('/FontName /'.$font['name']);
$this->_out('/Flags 6');
$this->_out('/FontBBox [0 0 1000 1000]');
$this->_out('/ItalicAngle 0');
$this->_out('/上升 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.
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();
?>
以上就介紹了怎樣用PHP創建PDF中文文檔,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

熱門話題

許多用戶在選擇智慧型手錶的時候都會選擇的華為的品牌,其中華為GT3pro和GT4都是非常熱門的選擇,不少用戶都很好奇華為GT3pro和GT4有什麼區別,下面就給大家介紹一下二者。華為GT3pro和GT4有什麼差別一、外觀GT4:46mm和41mm,材質是玻璃鏡板+不鏽鋼機身+高分纖維後殼。 GT3pro:46.6mm和42.9mm,材質是藍寶石玻璃鏡+鈦金屬機身/陶瓷機身+陶瓷後殼二、健康GT4:採用最新的華為Truseen5.5+演算法,結果會更加的精準。 GT3pro:多了ECG心電圖和血管及安

主機板上SPDIFOUT連接線序最近我遇到了一個問題,就是關於電線的接線順序。我上網查了一下,有些資料說1、2、4對應的是out、+5V、接地;而有些資料則說1、2、4對應的是out、接地、+5V。最好的方法是查看你的主機板說明書,如果找不到說明書,你可以使用萬用電表進行測量。首先找到接地,然後就可以確定其他的接線順序了。主機板vdg怎麼接線連接主機板的VDG接線時,您需要將VGA連接線的一端插入顯示器的VGA接口,另一端插入電腦的顯示卡VGA接口。請注意,不要將其插入主機板的VGA介面。完成連接後,您可以

為什麼截圖工具在Windows11上不起作用了解問題的根本原因有助於找到正確的解決方案。以下是截圖工具可能無法正常工作的主要原因:對焦助手已開啟:這可以防止截圖工具開啟。應用程式損壞:如果截圖工具在啟動時崩潰,則可能已損壞。過時的圖形驅動程式:不相容的驅動程式可能會幹擾截圖工具。來自其他應用程式的干擾:其他正在運行的應用程式可能與截圖工具衝突。憑證已過期:升級過程中的錯誤可能會導致此issu簡單的解決方案這些適合大多數用戶,不需要任何特殊的技術知識。 1.更新視窗與Microsoft應用程式商店應用程

1.按住鍵盤的【Win+R】快捷組合鍵,開啟【執行】對話指令窗口,輸入【services.msc】指令,點選【確定】;。 2.開啟服務介面後找到【RemoteRegistry】選項,並左鍵雙擊開啟其屬性對話方塊。 3.在開啟的【RemoteRegistry的屬性】對話方塊中,在啟動類型選項中選擇【停用】選項,再點選【套用】--【停止】--【確定】按鈕儲存設定即可。

out介面指的是輸出接口,in接口指的是輸入接口。 out介面一般代表著音源線路輸出接口,用來接負載,例音箱、耳機等;而in接口一般代表著音源線路輸入接口,用來接CD機、手機、MP3、電腦等。

Pygame的Font文字和字體Pygame透過pygame.font模組來建立一個字體對象,從而實現繪製文字的目的。此模組的常用方法如下所示:名稱說明pygame.font.init()初始化字體模組pygame.font.quit()取消初始化字體模組pygame.font.get_init()檢查字體模組是否被初始化,並傳回一個布林值。 pygame.font.get_default_font()取得預設字體的檔案名稱。傳回系統中字體的檔案名稱pygame.font.get_fonts()取得所有

第1部分:初始故障排除步驟檢查蘋果的系統狀態:在深入研究複雜的解決方案之前,讓我們先從基礎知識開始。問題可能不在於您的設備;蘋果的伺服器可能會關閉。造訪Apple的系統狀態頁面,查看AppStore是否正常運作。如果有問題,您所能做的就是等待Apple修復它。檢查您的網路連接:確保您擁有穩定的網路連接,因為「無法連接到AppStore」問題有時可歸因於連接不良。嘗試在Wi-Fi和行動數據之間切換或重置網路設定(「常規」>「重置」>「重置網路設定」>設定)。更新您的iOS版本:

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code
