PHP7.27: pdf
程序员文章站
2022-07-05 14:41:53
http://www.fpdf.org/ https://github.com/Setasign/FPDF https://www.ntaso.com/fpdf-and-chinese-characters/ https://www.phpbook.jp/fpdf/japan/index2.html ......
http://www.fpdf.org/
https://github.com/setasign/fpdf
https://www.ntaso.com/fpdf-and-chinese-characters/
<?php
// 1.8.1
//define("fpdf_fontpath","font/");
try
{
require("../fpdf.php");
$pdf = new fpdf();
$pdf->addpage();
$pdf->setfont("arial","b",16);
$pdf->cell(40,10,"hello world! 涂聚文"); //中文乱码
$pdf->output();
}
catch(exception $ex)
{
echo($ex->getmessage());
}
?>
https://www.phpbook.jp/fpdf/japan/index2.html
解决中文问题:
http://www.fpdf.org/en/script/script92.php
把中文后缀为.tff字体文件放入至:tfpdf\font\unifont 的文夹下即可。参考 http://www.fpdf.org/en/script/script92.php
下载:
https://dejavu-fonts.github.io/download.html
<?php
// optionally define the filesystem path to your system fonts
// otherwise tfpdf will use [path to tfpdf]/font/unifont/ directory
// define("_system_ttfonts", "c:/windows/fonts/"); tfpdf
//http://www.fpdf.org/en/script/script92.php
require('tfpdf/tfpdf.php');
$pdf = new tfpdf();
$pdf->addpage();
// add a unicode font (uses utf-8)
//tfpdf\font\unifont 把中文的字体文件放入其内则可。
$pdf->addfont('dejavu','','3pdwg6wqe1jpcgh.ttf',true); //msyh.ttf 设置字体即可,字体名称不可以定义为中文 简体篆体 :3pdwg6wqe1jpcgh.ttf
$pdf->setfont('dejavu','',14);
// load a utf-8 string from a file and print it
// 在操作系统下可以选择的字体可以显示,则也可以生成
$txt ="english: hello world
greek: γειά σου κόσμος
polish: witaj świecie
portuguese: olá mundo
russian: здравствулте мир
vietnamese: xin chào thế giới,中国智造0123456789 涂聚文geovindu 生命的意义,涂聚文,*";// file_get_contents('helloworld.txt');
$pdf->write(8,$txt);
// select a standard font (uses windows-1252)
$pdf->setfont('arial','',14);
$pdf->ln(10);
$pdf->write(5,'the file size of this pdf is only 13 kb.');
$pdf->output();
?>
<?php
// optionally define the filesystem path to your system fonts
// otherwise tfpdf will use [path to tfpdf]/font/unifont/ directory
// define("_system_ttfonts", "c:/windows/fonts/"); tfpdf
//http://www.fpdf.org/en/script/script92.php
require('tfpdf/tfpdf.php');
$pdf = new tfpdf();
$pdf->addpage();
// add a unicode font (uses utf-8)
//tfpdf\font\unifont 把中文的字体文件放入其内则可。
$pdf->addfont('dejavu','','3pdwg6wqe1jpcgh.ttf',true); //msyh.ttf 设置字体即可,字体名称不可以定义为中文 简体篆体 :3pdwg6wqe1jpcgh.ttf
$pdf->setfont('dejavu','',14);
// load a utf-8 string from a file and print it
// 在操作系统下可以选择的字体可以显示,则也可以生成
$txt ="english: hello world
greek: γειά σου κόσμος
polish: witaj świecie
portuguese: olá mundo
russian: здравствулте мир
vietnamese: xin chào thế giới,中国智造0123456789 涂聚文geovindu 生命的意义,涂聚文,*";// file_get_contents('helloworld.txt');
$pdf->write(8,$txt);
$pdf->ln(16);
$sheader=array("姓名","年龄","性别","工资","学历");
$data=array();
$data[0]=array("geovindu","1","男","100","");
$data[1]=array("sibodu","2","男","200","");
$data[2]=array("涂聚文","3","女","300","");
$data[3]=array("小涂","4","女","400","");
$data[4]=array("阿文","5","男","500","");
$data[5]=array("小文","6","女","600","");
$width=array(40,40,40,40); //设置每列宽度
for($i=0;$i<count($sheader);$i++)
{
$pdf->cell($width[$i],6,$sheader[$i],1);
}
$pdf->ln(6);
foreach($data as $row)
{
$pdf->cell($width[0],6,$row[0],1);
$pdf->cell($width[1],6,$row[1],1);
$pdf->cell($width[2],6,$row[2],1);
$pdf->cell($width[3],6,$row[3],1);
$pdf->cell($width[4],6,$row[4],1);
$pdf->cell($width[5],6,$row[5],1);
$pdf->ln(6);
}
// select a standard font (uses windows-1252)
$pdf->setfont('arial','',14);
$pdf->ln(10);
$pdf->write(5,'the file size of this pdf is only 13 kb.');
$pdf->output();
?>
<?php
// optionally define the filesystem path to your system fonts
// otherwise tfpdf will use [path to tfpdf]/font/unifont/ directory
// define("_system_ttfonts", "c:/windows/fonts/");
require('tfpdf\tfpdf.php');
$pdf = new tfpdf();
$pdf->addpage();
// add a unicode font (uses utf-8)
$pdf->addfont('dejavu','','dejavusanscondensed.ttf',true);
$pdf->setfont('dejavu','',14);
// load a utf-8 string from a file and print it
$txt = file_get_contents('./tfpdf/helloworld.txt', false, null, 20, 1400); //
$pdf->write(8,$txt);
// select a standard font (uses windows-1252)
$pdf->setfont('arial','',14);
$pdf->ln(10);
$pdf->write(5,'the file size of this pdf is only 13 kb.');
$pdf->output();
?>