返回列表 回復 發帖

[轉貼] PDF报表中文都是乱码

文章来自 http://leokan.blogchina.com/blog/6021016.html

這段時間在研究weberp,開源系統,php+mysql+apache,支持簡體,繁體,日文,韓文,界面很友好,只是在打印的時候會亂碼,我搞了好久終於找到原因可能是老外寫的吧,把簡體和繁體混為一談了,在這個系統中,打印是由fpdf1.52完成,造成亂碼的文件是class.pdf.php
節遷代碼如下:
define('FPDF_FONTPATH','./fonts/');
include ('fpdf.php');

if ($_SESSION['Language']=='zh_CN'){
include('FPDF_Chinese.php');
} elseif ($_SESSION['Language']=='ja_JP'){
include('FPDF_Japanese.php');
}elseif ($_SESSION['Language']=='ko_KR'){
include('FPDF_Korean.php');
} else {
class PDF_Language extends FPDF {
}
}

class Cpdf extends PDF_Language {

function Cpdf($pageSize=array(0,0,612,792)) {

$this->PDF_Language( 'P', 'pt',array($pageSize[2]-$pageSize[0],$pageSize[3]-$pageSize[1]));
$this->setAutoPageBreak(0);
$this->AddPage();
$this->SetLineWidth(1);
$this->cMargin = 0;

// Next three lines should be here for any fonts genarted with 'makefont' utility
if ($_SESSION['Language']=='zh_CN'){
$this->AddBig5Font();
}elseif ($_SESSION['Language']=='ja_JP'){
$this->AddSJISFont();
}elseif ($_SESSION['Language']=='ko_KR'){
$this->AddUHCFont();
} else {
$this->AddFont('helvetica');
$this->AddFont('helvetica','I');
$this->AddFont('helvetica','B');
}
}

function selectFont($FontName) {

$type = '';
if(strpos($FontName, 'Oblique')) {
$type = 'I';
}
if(strpos($FontName, 'Bold')) {
$type = 'B';
}
if ($_SESSION['Language']=='zh_CN'){
$FontName = 'Big5';
} elseif ($_SESSION['Language']=='ja_JP'){
$FontName = 'SJIS';
} elseif ($_SESSION['Language']=='ko_KR'){
$FontName = 'UHC';
} else {
$FontName ='helvetica';
}
$this->SetFont($FontName, $type);
}
很顯然,當$_SESSION['Language']=='zh_CN'時,應為GB,而不是BIG5,只有當$_SESSION['Language']=='zh_HK'才應該是addbig5font以及$FontName='Big5',更改的代碼如下
define('FPDF_FONTPATH','./fonts/');
include ('fpdf.php');

if (($_SESSION['Language']=='zh_HK') or ($_SESSION['Language']=='zh_CN')){
include('FPDF_Chinese.php');
} elseif ($_SESSION['Language']=='ja_JP'){
include('FPDF_Japanese.php');
}elseif ($_SESSION['Language']=='ko_KR'){
include('FPDF_Korean.php');
} else {
class PDF_Language extends FPDF {
}
}

class Cpdf extends PDF_Language {

function Cpdf($pageSize=array(0,0,612,792)) {

$this->PDF_Language( 'P', 'pt',array($pageSize[2]-$pageSize[0],$pageSize[3]-$pageSize[1]));
$this->setAutoPageBreak(0);
$this->AddPage();
$this->SetLineWidth(1);
$this->cMargin = 0;

// Next three lines should be here for any fonts genarted with 'makefont' utility
if ($_SESSION['Language']=='zh_HK'){
$this->AddBig5Font();
}elseif ($_SESSION['Language']=='zh_CN'){
$this->AddGBFont();
}elseif ($_SESSION['Language']=='ja_JP'){
$this->AddSJISFont();
}elseif ($_SESSION['Language']=='ko_KR'){
$this->AddUHCFont();
} else {
$this->AddFont('helvetica');
$this->AddFont('helvetica','I');
$this->AddFont('helvetica','B');
}
}

function selectFont($FontName) {

$type = '';
if(strpos($FontName, 'Oblique')) {
$type = 'I';
}
if(strpos($FontName, 'Bold')) {
$type = 'B';
}
if ($_SESSION['Language']=='zh_HK'){
$FontName = 'Big5';
}elseif ($_SESSION['Language']=='zh_CN'){
$FontName = 'GB';
} elseif ($_SESSION['Language']=='ja_JP'){
$FontName = 'SJIS';
} elseif ($_SESSION['Language']=='ko_KR'){
$FontName = 'UHC';
} else {
$FontName ='helvetica';
}
$this->SetFont($FontName, $type);
}
這樣就不會有亂碼了
返回列表 回復 發帖