如何PHP使用mpdf实现导出pdf文件功能讲解
这篇文章主要为大家详细介绍了PHP如何使用mpdf实现导出pdf文件功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
mpdf的开发文档地址:
Supported CSS – CSS & Stylesheets – mPDF Manual
1.加载依赖库
1
|
composer require mpdf /mpdf |
2.页面
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
$html = <<< EOD <body style = "background:url($img1);" lang = "zh-CN" > < div style = "background:rgba(255,255,255,0.3);" > < div style = "width: 12rem;height: 15rem;float: right;" > $avatar </ div > < div style = "padding-top: 5rem;padding-left: 2rem;" > < span style = "font-size: 20pt;font-weight: bold;" >$strTitle</ span > < table style = "color: #666666;padding-top: 1rem;font-size: 14pt;line-height: 16pt;" > < tr > < td style = "" > 姓名:$studentName </ td > < td style = "padding-left: 2rem;" > 性别:$sex </ td > < td style = "" > 出生年月:$birthday </ td > </ tr > < tr > < td > 民族:$nationality </ td > < td style = "" > 政治面貌:$political </ td > < td style = "padding-left: 2rem;" > 电话:$phone </ td > </ tr > </ table > </ div > </ div > < div style = "background-color: white;padding-bottom: initial;" > < table style = "text-align:center;font-size:20pt;" > </ table > < h2 ></ h2 >< h2 ></ h2 > < h2 style = "padding-left: 2rem" >技能特长</ h2 > < table style = "text-align:left;line-height:200%;font-size:14pt;color: #666666;" > $strSkills </ table > < h2 ></ h2 > < h2 style = "padding-left: 2rem" >成绩单</ h2 > < table style = "text-align:left;line-height:200%;font-size:12pt;border-collapse: collapse;margin-left: 2rem;width: 95%;" > < tr > < th style = "border: 1px solid black;border-right: 0px;" >总分:$scoreTotal</ th > < th style = "border: 1px solid black;border-right: 0px;border-left: 0px;" ></ th > < th style = "border: 1px solid black;border-right: 0px;border-left: 0px;" ></ th > < th style = "border: 1px solid black;border-right: 0px;border-left: 0px;" ></ th > < th style = "border: 1px solid black;border-left: 0px;" ></ th > </ tr > $strActivity </ table > < h2 ></ h2 > < h2 style = "padding-left: 2rem;" >获奖证书</ h2 > < table style = "text-align:left;line-height:200%;font-size:12pt;border-collapse: collapse;margin-left: 2rem;width: 95%;" > < tr > < th style = "border: 1px solid black;color: #333333;" >项目名称</ th > < th style = "border: 1px solid black;color: #333333;" >等级</ th > < th style = "border: 1px solid black;color: #333333;" >获得时间</ th > < th style = "border: 1px solid black;color: #333333;" >经历描述</ th > </ tr > $strCertificate </ table > < h2 ></ h2 > < h2 style = "padding-left: 2rem;" >实践经历</ h2 > $strTraining < h2 ></ h2 > < h2 style = "padding-left: 2rem;" >个人风采</ h2 > $strPersona </ div > </ body > EOD; |
3.导出
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
|
require_once 'vendor/autoload.php' ; $mpdf = new Mpdf([ 'mode' => '' , 'format' => 'A4' , 'default_font_size' => 0, 'default_font' => 'sans' , 'margin_left' => 10, 'margin_right' => 10, 'margin_top' => 10, 'margin_bottom' => 10, 'margin_header' => 9, 'margin_footer' => 9, 'orientation' => 'P' , ]); $mpdf ->useAdobeCJK = true; $mpdf ->autoScriptToLang = true; //支持中文设置 $mpdf ->autoLangToFont = true; //支持中文设置 $mpdf ->setAutoTopMargin = 'stretch' ; //设置自动顶部边距 $mpdf ->setAutoBottomMargin = 'stretch' ; //设置自动低部边距 $mpdf ->AddPage(); //获取配置文字或者logo $conf =M( "confScoreList" )->find(); //设置页眉 $SetHeader = '<table class = "header" style= "text-align: right;width: 100%;" > <tr> <td width= "33%" style= "text-align: left;font-size: 10pt;color: #999999;" > <img src= "'.K_PATH_IMAGES.$conf['logo'].'" alt= "" > </td> <td width= "33%" style= "text-align: right;color: #999999;" > '.$conf[' headerRight '].' </td> </tr> </table>'; $mpdf ->SetHeader( $SetHeader ); //这是一个页脚的范例{PAGENO}是当前的页数,{nb}是总共的页数 //<td width="33%" style="text-align: center;font-size: 10pt;">第 {PAGENO} 页 共 {nb} 页</td> $setFooter = '<table class = "footer" style= "text-align: right;width: 100%;" > <tr> <td width= "33%" style= "text-align: left;font-size: 10pt;color: #999999;" > '.$conf[' footerLeft '].' </td> <td width= "33%" style= "text-align: right;color: #999999;" > '.$conf[' footerRight '].' </td> </tr> </table>'; $mpdf ->setFooter( $setFooter ); //设置中文编码 $mpdf ->WriteHTML( $html ); //导出pdf文件重命名 // $mpdf->Output($dataResume['resumeName'] . '.pdf', true); $mpdf ->Output(); exit ; |
到此这篇关于PHP使用mpdf实现导出pdf文件功能的文章就介绍到这了。