>  기사  >  백엔드 개발  >  PHP를 사용하여 PDF 중국어 문서를 만드는 방법

PHP를 사용하여 PDF 중국어 문서를 만드는 방법

WBOY
WBOY원래의
2016-08-08 09:33:311028검색

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

클래스 PDF_중국어는 FPDF를 확장합니다
{
함수 AddCIDFont($family,$style,$name,$cw,$CMap,$registry)
{
$i=count($this->글꼴)+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);
}

함수 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('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);
}

함수 GetStringWidth($s)
{
if($this->CurrentFont['type']=='Type0')
return $this->GetMBStringWidth($s);
그 외
부모 반환::GetStringWidth($s);
}

함수 GetMBStringWidth($s)
{
//GetStringWidth()의 멀티바이트 버전
$l=0;
$cw=&$this->현재 글꼴['cw'];
$nb=strlen($s);
$i=0;
동안($i<$nb)
{
$c=$s[$i];
if(ord($c)<128)
{
$l+=$cw[$c];
$i++;
}
그 외
{
$l+=1000;
$i+=2;
}
}
return $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);
그 외
parent::MultiCell($w,$h,$txt,$border,$align,$fill);
}

함수 MBMultiCell($w,$h,$txt,$border=0,$align='L',$fill=0)
{
//MultiCell()의 멀티바이트 버전
$cw=&$this->현재 글꼴['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 및 $s[$nb-1]=="n"
$nb--;
$b=0;
if($border)
{
if($border==1)
{
$border='LTRB';
$b='LRT';
$b2='LR';
}
그 외
{
$b2='';
if(is_int(strpos($border,'L')))
$b2.='엘';
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<$nb)
{
//다음 문자 가져오기
$c=$s[$i];
//ASCII인지 MB인지 확인
$ascii=(ord($c)<128);
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)
{
$9=$i;
$ls=$l;
}
elseif($c==' ')
{
$9=$i;
$ls=$l;
$ns++;
}
$l+=$ascii ? $cw[$c] : 1000;
if($l>$wmax)
{
//자동 줄 바꿈
if($sep==-1 또는 $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);
}
그 외
{
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]==' ') ? $9월+1 : $9월;
}
$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;
}

함수 쓰기($h,$txt,$link='')
{
if($this->CurrentFont['type']=='Type0')
$this->MBWrite($h,$txt,$link);
그 외
상위::쓰기($h,$txt,$link);
}

함수 MBWrite($h,$txt,$link)
{
//Write()의 멀티바이트 버전
$cw=&$this->현재 글꼴['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<$nb)
{
//다음 문자 가져오기
$c=$s[$i];
//ASCII인지 MB인지 확인
$ascii=(ord($c)<128);
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==' ')
$9=$i;
$l+=$ascii ? $cw[$c] : 1000;
if($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++;
계속;
}
if($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]==' ') ? $9월+1 : $9월;
}
$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;
}
//마지막 청크
if($i!=$j)
$this->Cell($l/1000*$this->FontSize,$h,substr($s,$j,$i-$j),0,0,'',0,$link);
}

함수 _putfonts()
{
$nf=$this->n;
foreach($this->$diff로 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;
$size=파일 크기($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->글꼴은 $k=>$font)
{
//글꼴 객체
$this->_newobj();
$this->글꼴[$k]['n']=$this->n;
$this->_out('< if($font['type']=='Type0')
$this->_putType0($font);
그 외
{
$name=$font['이름'];
$this->_out('/BaseFont /'.$name);
if($font['type']=='코어')
{
//표준 글꼴
$this->_out('/하위 유형 /Type1');
if($name!='Symbol' 및 $name!='ZapfDingbats')
$this->_out('/인코딩 /WinAnsiEncoding');
}
그 외
{
//추가 글꼴
$this->_out('/하위 유형 /'.$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('/인코딩 '.($nf+$font['diff']).' 0 R');
그 외
$this->_out('/인코딩 /WinAnsiEncoding');
}
}
$this->_out('>>');
$this->_out('endobj');
if($font['type']!='core')
{
//너비
$this->_newobj();
$cw=&$font['cw'];
$s='[';
for($i=32;$i<=255;$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['파일'];
if($파일)
$s.=' /FontFile'.($font['type']=='Type1' ? '' : '2').' '.$this->글꼴파일[$file]['n'].' 0R';
$this->_out($s.'>>');
$this->_out('endobj');
}
}
}
}

함수 _putType0($font)
{
//유형0
$this->_out('/하위 유형 /Type0');
$this->_out('/BaseFont /'.$font['name'].'-'.$font['CMap']);
$this->_out('/인코딩 /'.$font['CMap']);
$this->_out('/DescendantFonts ['.($this->n+1).' 0 R]');
$this->_out('>>');
$this->_out('endobj');
//CID폰트
$this->_newobj();
$this->_out('< $this->_out('/하위 유형 /CIDFontType0');
$this->_out('/BaseFont /'.$font['name']);
$this->_out('/CIDSystemInfo <>');
$this->_out('/FontDescriptor '.($this->n+1).' 0 R');
$W='/W [1 [';
foreach($font['cw']를 $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('/하강 0');
$this->_out('/CapHeight 1000');
$this->_out('/StemV 10');
$this->_out('>>');
$this->_out('endobj');
}
}
?>

참고를 위해 위의 코드를 Chinese.php로 저장하세요. 하지만 하나의 글꼴만 얻을 수 있습니다. 모든 중국어 글꼴을 지원하기 위해 ttf2pt1 프로그램을 사용하여 트루타입 글꼴을 pt1 글꼴로 하나씩 변환할 수 있습니다(구체적인 방법은 FPDF 튜토리얼에 자세히 설명되어 있습니다). 다른 중국어 글꼴을 지원하려면 위의 Chinese.php를 다음과 같이 수정해야 합니다.

1: AddGBFont() 메서드에서 다음 줄을 바꿉니다.

함수 AddGBFont($family='GB',$name='STSongStd-Light-Acro')
{
$cw=$GLOBALS['GB_widths']
// $name='STSongStd-라이트-아크로'
$CMap='GBKp-EUC-H'
........

2: 이것은 샘플입니다.

require('중국어.php');

$pdf=새 PDF_중국어()
$pdf->AddGBFont('simsun','宋体')
$pdf->AddGBFont('simhei','黑体')
$pdf->AddGBFont('simkai','楷体_GB2312')
$pdf->AddGBFont('sinfang','모조송나라_GB2312'')
$pdf->열기()
$pdf->페이지 추가()
$pdf->SetFont('simsun','',20)
$pdf->Write(10,'간체자')
$pdf->SetFont('심헤이','',20)
$pdf->Write(10,'간체자')
$pdf->SetFont('simkai','',20)
$pdf->Write(10,'간체자')
$pdf->SetFont('sinfang','',20)
$pdf->Write(10,'간체자')
$pdf->출력()
?>

위 내용은 PHP를 사용하여 PDF 중국어 문서를 작성하는 방법과 관련 내용을 소개하고 있으며, PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.