PHP7.27: pdf

http://www.fpdf.org/

https://github.com/Setasign/FPDF

https://www.ntaso.com/fpdf-and-chinese-characters/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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 

下载: http://www.fpdf.org/en/script/dl.php?id=92&f=zip

https://dejavu-fonts.github.io/Download.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?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();
?>

  

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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();
?>

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?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();
?>

  

posted @   ®Geovin Du Dream Park™  阅读(583)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2012-08-27 Create Ms Word doc using Javascript And vbscript
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示