生成PDF

1、Tcpdf官网文档地址:https://tcpdf.org/
2、Github地址:https://github.com/tecnickcom/tcpdf

必读说明

  • TCPDF类库存在Bug:重新定义TCPDF中的K_PATH_IMAGES常量(解决:设置头部logo不显示的问题,如果不定义常量,可以将logo图片移动到vendor/tecnickcom/examples/images下)
  • 解决中文乱码问题
    1.下载Droid Sans字体:本次项目中已包含,未包含网盘下载地址:http://pan.baidu.com/s/1bnq21Ld
    2.将解压后的三个文件:droidsansfallback.php、droidsansfallback.z和droidsansfallback.ctg.z 放入 vendor/tecnickcom/tcpdf/fonts/目录下
    3.修改类库常量(vendor/tecnickcom/tcpdf/config/tcpdf_config.php):define (‘PDF_FONT_NAME_DATA’, ‘droidsansfallback’); define (‘PDF_FONT_NAME_MAIN’, ‘droidsansfallback’);
    4.不修改第三步的前提下,在调用setHeaderFont方法时可自行定义

实例

index.php
<?php
/*** Created by PhpStorm.* User: Jason* Date: 2019/1/23* Time: 13:23*/// 导入类库
require_once './vendor/autoload.php';// 重新定义K_PATH_IMAGES常量
define('K_PATH_IMAGES', 'D:\newPhpStudy\WWW\TCPDF\images\\');// 创建新的PDF文档
// 说明:
// P1:页面方向[P,L]
// P2:文档计量单位[pt = point, mm = millimeter, cm = centimeter, in = inch]
// P3:页面格式[A4]
// P4:输入文本是否为Unicode[true, false]
// P5:字符集编码[UTF-8....],仅在转换回HTML实体时使用
// P6:不推荐使用的功能[false]
// P7:是否PDF/A模式[true, false]
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// 设置文档信息[定义文档的创建者,这通常是生成PDF的应用程序的名称]
$pdf->SetCreator('ZhouCreator_' . date('Ymd'));
// 定义文档的作者
$pdf->SetAuthor('Jason');
// 定义文档的标题
$pdf->SetTitle('PDF测试标题');
// 定义文档的主题
$pdf->SetSubject('PDF测试主题');
// 设置文档的关键字,格式:A1 A2 A3
$pdf->SetKeywords('关键字1,关键字2,关键字3');
// 设置默认头信息
// 说明:
// P1:图标Logo地址,必须是常量K_PATH_IMAGES下的文件
// P2:图标Logo宽度
// P3:作为标题打印在文档标题上的字符串
// P4:要在文档头上打印的字符串
// P5:文本的RGB数组颜色[0,0,0]
// P6:线条的RGB数组颜色[0,0,0]
$pdf->SetHeaderData('logo.jpg', 15, '周小店', 'www.smallzhou.cn', [50, 35, 125], [200, 80, 128]);
// 设置页眉和页脚字体(描述基本字体参数,数组格式[字体,样式,大小])
$pdf->setHeaderFont(['droidsansfallback', '', PDF_FONT_SIZE_MAIN]);
$pdf->setFooterFont(['droidsansfallback', '', PDF_FONT_SIZE_DATA]);
// 设置默认单空格字体
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// 设定页边空白(下边的三个统称)
// 定义左、上、右、边距
// 说明:
// P1:左边距 P2:上边距 P3:右边距(设置为-1,以左边距为准) P4:True/False,true的情况下覆盖默认页边框
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
// 设置页眉边距(以用户单位表示的距离):页眉和上页边距之间的最小距离
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
// 设置页脚边距(以用户单位表示的距离):页脚和底页边距之间的最小距离
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// 启用或禁用自动分页符模式(启用时,第二个参数是与定义触发限制的页面底部的距离。默认情况下,模式为“开”,边距为2 cm。)
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// 设置调整因子以将像素转换为用户单位
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// 设置用于打印字符串的字体
// 说明:
// P1:字体 P2:样式 P3:大小
$pdf->SetFont('droidsansfallback', '', 10);// 向文档添加新页
// 说明:
// P1:页面方向 P2:页面的格式 P3:如果为真,则用当前页边距覆盖默认页边距 P4:如果未真,添加的页面将用于显示目录
$pdf->AddPage();
// 定义html代码
$html = '<h1>HTML 示例</h1>
特殊字符: &lt; € &euro; € &amp; è &egrave; &copy; &gt; \\slash \\\\double-slash \\\\\\triple-slash
<h2>List</h2>
列表示例:
<ol><li><img src="data:images/2.jpg" alt="test alt attribute" width="30" height="30" border="0" /> test image</li><li><b>bold text</b></li><li><i>italic text</i></li><li><u>underlined text</u></li><li><b>b<i>bi<u>biu</u>bi</i>b</b></li><li><a href="http://www.tecnick.com" dir="ltr">link to http://www.tecnick.com</a></li><li>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo.<br />Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</li><li>SUBLIST<ol><li>row one<ul><li>sublist</li></ul></li><li>row two</li></ol></li><li><b>T</b>E<i>S</i><u>T</u> <del>line through</del></li><li><font size="+3">font + 3</font></li><li><small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal</li>
</ol>
<dl><dt>Coffee</dt><dd>Black hot drink</dd><dt>Milk</dt><dd>White cold drink</dd>
</dl>
<div style="text-align:center">IMAGES<br />
<img src="data:images/2.jpg" alt="test alt attribute" width="100" height="100" border="0" />
<img src="data:images/2.jpg" alt="test alt attribute" width="100" height="100" border="0" />
<img src="data:images/2.jpg" alt="test alt attribute" width="100" height="100" border="0" />
</div>';
// Html输出
// 说明:
// P1:显示文本
// P2:如果为真,则在文本后添加新行
// P3:设置背景必须绘制还是透明
// P4:如果为真,重置最后一个单元格高度
// P5:如果为true,则在每次写入中添加当前的左(或右)填充(默认为false)
$pdf->writeHTML($html, true, false, true, false, '');// 测试一些内联CSS
$html = '<p>This is just an example of html code to demonstrate some supported CSS inline styles.
<span style="font-weight: bold;">bold text</span>
<span style="text-decoration: line-through;">line-trough</span>
<span style="text-decoration: underline line-through;">underline and line-trough</span>
<span style="color: rgb(0, 128, 64);">color</span>
<span style="background-color: rgb(255, 0, 0); color: rgb(255, 255, 255);">background color</span>
<span style="font-weight: bold;">bold</span>
<span style="font-size: xx-small;">xx-small</span>
<span style="font-size: x-small;">x-small</span>
<span style="font-size: small;">small</span>
<span style="font-size: medium;">medium</span>
<span style="font-size: large;">large</span>
<span style="font-size: x-large;">x-large</span>
<span style="font-size: xx-large;">xx-large</span>
</p>';
$pdf->writeHTML($html, true, false, true, false, '');
// 重置指向最后一个文档页的指针
$pdf->lastPage();// 打印Table表格
$pdf->AddPage();
$html = '<h1>Image alignments on HTML table</h1>
<table cellpadding="1" cellspacing="1" border="1" style="text-align:center;">
<tr><td><img src="data:images/logo_example.png" border="0" height="41" width="41" /></td></tr>
<tr style="text-align:left;"><td><img src="data:images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr>
<tr style="text-align:center;"><td><img src="data:images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr>
<tr style="text-align:right;"><td><img src="data:images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr>
<tr><td style="text-align:left;"><img src="data:images/logo_example.png" border="0" height="41" width="41" align="top" /></td></tr>
<tr><td style="text-align:center;"><img src="data:images/logo_example.png" border="0" height="41" width="41" align="middle" /></td></tr>
<tr><td style="text-align:right;"><img src="data:images/logo_example.png" border="0" height="41" width="41" align="bottom" /></td></tr>
</table>';
$pdf->writeHTML($html, true, false, true, false, '');// 打印所有HTML颜色
$pdf->AddPage();
$textcolors = '<h1>HTML Text Colors</h1>';
$bgcolors = '<hr /><h1>HTML Background Colors</h1>';
foreach (TCPDF_COLORS::$webcolor as $k => $v) {$textcolors .= '<span color="#' . $v . '">' . $v . '</span> ';$bgcolors .= '<span bgcolor="#' . $v . '" color="#333333">' . $v . '</span> ';
}
$pdf->writeHTML($textcolors, true, false, true, false, '');
$pdf->writeHTML($bgcolors, true, false, true, false, '');$pdf->AddPage();
// 测试字包
$html = '<h1>测试字包</h1>
<a href="#2">锚点:连接到第二页</a><br />
<font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font> <font face="courier"><b>thisisaverylongword</b></font> <font face="helvetica"><i>thisisanotherverylongword</i></font> <font face="times"><b>thisisaverylongword</b></font> thisisanotherverylongword <font face="times">thisisaverylongword</font>';
$pdf->writeHTML($html, true, false, true, false, '');// 测试字体嵌套
$pdf->AddPage();
$html1 = '测试字体嵌套 <font face="courier">Courier <font face="helvetica">Helvetica <font face="times">Times <font face="dejavusans">dejavusans </font>Times </font>Helvetica </font>Courier </font>Default';
$html2 = '<small>small text</small> normal <small>small text</small> normal <sub>subscript</sub> normal <sup>superscript</sup> normal';
$html3 = '<font size="10" color="#ff7f50">The</font> <font size="10" color="#6495ed">quick</font> <font size="14" color="#dc143c">brown</font> <font size="18" color="#008000">fox</font> <font size="22"><a href="http://www.tcpdf.org">jumps</a></font> <font size="22" color="#a0522d">over</font> <font size="18" color="#da70d6">the</font> <font size="14" color="#9400d3">lazy</font> <font size="10" color="#4169el">dog</font>.';
$html = $html1 . '<br />' . $html2 . '<br />' . $html3 . '<br />' . $html3 . '<br />' . $html2;
$pdf->writeHTML($html, true, false, true, false, '');// 测试预标记
$pdf->AddPage();
$html = <<<EOF
<div style="background-color:#880000;color:white;">
Hello World!<br />
Hello
</div>
<pre style="background-color:#336699;color:white;">
int main() {printf("HelloWorld");return 0;
}
</pre>
<tt>Monospace font</tt>, normal font, <tt>monospace font</tt>, normal font.
<br />
<div style="background-color:#880000;color:white;">DIV LEVEL 1<div style="background-color:#008800;color:white;">DIV LEVEL 2</div>DIV LEVEL 1</div>
<br />
<span style="background-color:#880000;color:white;">SPAN LEVEL 1 <span style="background-color:#008800;color:white;">SPAN LEVEL 2</span> SPAN LEVEL 1</span>
EOF;
$pdf->writeHTML($html, true, false, true, false, '');// 测试列表的自定义项目符号点
$pdf->AddPage();
$html = <<<EOF
<h1>测试列表的自定义项目符号点</h1>
<ul style="font-size:14pt;list-style-type:img|png|4|4|images/logo_example.png"><li>test custom bullet image</li><li>test custom bullet image</li><li>test custom bullet image</li><li>test custom bullet image</li>
<ul>
EOF;
$pdf->writeHTML($html, true, false, true, false, '');
$pdf->lastPage();// 输出
// 说明:
// P1:pdf名称 P2:输出方式['I':直接输出到浏览器 'D':浏览器下载,文件名默认为定义的文件名 'F':指定名称保存到服务器  'S':以字符串形式返回文档(忽略名称)'FI':相反与F和I的组合]
$pdf->Output('20190123.pdf', 'I');
// $pdf->Output(K_PATH_IMAGES . '20190123.pdf', 'FI');

源码百度云盘下载地址:

链接:https://pan.baidu.com/s/1aJ8XAKp0q11ZUCBHmheTZA
提取码:ib06

TCPDF:html文件转pdf格式相关推荐

  1. 文件 打包 及 生成Excel文件转PDF格式和Excsl文件格式一体数据

    文件 打包 及 生成Excel文件转PDF格式和Excsl文件格式一体数据 前言 一.转PDF 方法 二.生成Excsl文件 三.打包 总结 前言 个人理解,希望对你有所帮助. 一.转PDF 方法 代 ...

  2. msg文件转成html文件,如何将MSG格式的文件转换为PDF格式文件?

    三.如何将MSG格式的文件转换为PDF格式的文件? 想要随时查看MSG格式文件的话,格式转换就成为了理想的途径,下面将教大家将MSG格式的文件转换为PDF格式文件三种方式,一起去学习一下吧. 1.借助 ...

  3. 轻松转换CAJ文件为PDF格式:免费工具和技巧

    在处理中国知网(CNKI)数据库中的CAJ文件时,将其转换为更常用的PDF格式可以提供更广泛的共享和便捷的阅读体验.本文将介绍一种免费的工具和一些技巧,帮助您轻松地将CAJ文件转换为PDF格式.我们将 ...

  4. xps 转 pdf android版,OakDoc XPS to PDF Converter(XPS文件转PDF格式工具)V2.2 正式版

    OakDoc XPS to PDF Converter(XPS文件转PDF格式工具)是一款很优秀好用的XPS转PDF的辅助工具.如果你需要一款好用的文件转换工具,小编带来的这款OakDoc XPS t ...

  5. 如何将CAD文件转换为PDF格式,免费试用

    刚刚接触CAD文件,对于文件的格式还不是很会操作,PDF格式我们平时用的多,使用起来更加方便,随时随地都能打开查看,那么我们如何将CAD文件转换为PDF格式?今天和大家分享一种简单的操作方法,并且是免 ...

  6. 把从中国知网上下载的CAJ文件转换为PDF格式

    许多在中国知网上下载下来的文章是CAJ格式的,这些CAJ格式的文章只能使用知网的阅读工具阅读,在打印.复制等方面都很麻烦.下面笔者就教大家如何把CAJ格式文件转换为PDF格式文件. 工具/原料 CAJ ...

  7. 使用Java代码将word、execl、ppt文件转换为pdf格式

    office文件转换为pdf格式 使用OpenOffice转换 前言:通过第三方工具openoffice,将word.excel.ppt等文件转换为pdf文件支持在线 预 览:官网地址:http:// ...

  8. CAD文件转PDF格式后有白底怎么办?

    我们在进行图纸设计过程中经常会遇到很多的问题.我们完成图纸设计后都会转换成PDF的格式.你有遇到转换格式后出现白底的情况吗? 1.面对少量的图纸我们一般直接另存为PDF的格式.可是面对大量的文件就需要 ...

  9. 使用java对html源代码(拼装、可获取页面源代码)转换为(html页面,doc文档文件,pdf格式),doc文件转换成为pdf,文件的删除、压缩功能

    目录: 1.转换成为html页面 2.html源代码转换成为doc文件 3.html源代码转换成为pdf文件 4.压缩多个文件成为一个zip文件 5.对文件进行删除 实施过程: A.html页面的实现 ...

最新文章

  1. 上拉电阻和下拉电阻_硬件基础:下拉电阻和上拉电阻如何工作
  2. 一篇文章教你搞定传说中的“随机森林”
  3. Python函数——.strip()
  4. springboot13 发布和监听事件
  5. 配置httpd虚拟主机
  6. leetcode 45. 跳跃游戏 II 思考分析
  7. Linux 系统故障排查思路简析
  8. Team Leader 究竟要不要写代码?
  9. 安全合规/等级保护--10--等级保护介绍及流程
  10. BUUCTF_Crytpo_robomunication
  11. 基于arduino单片机的室内温度控制
  12. 小葵花妈妈课堂开课了:《Runnable、Callable、Future、RunnableFuture、FutureTask 源码分析》...
  13. 小程序修改weiui-uploadimg的+号图标为自定义图片
  14. 中蜂几月份自然分蜂_蜜蜂一年自然分蜂几次?
  15. 抖音胡闹天宫直播项目,可虚拟人直播 抖音报白 实时互动直播软
  16. win10 可以复制但无法粘贴的问题
  17. MFC界面开发中的一些问题
  18. 中英文排版字符间距不一致,英文自动断字
  19. ubuntu18.04上使用7z分卷压缩和解压
  20. 矩阵的秩和向量组的秩

热门文章

  1. vi8超级版Android 6,vi8超级版怎么切换到安卓
  2. 《Pygame游戏编程入门》学习——第4章 用户输入:Bomb Catcher游戏
  3. ls -l 列表信息详解
  4. 基于51单片机的数字电子钟万年历闰年判断闹钟设计
  5. 定义产品的一些考虑因素
  6. iOS体会篇 大学编程到公司的过程
  7. C++编写Windows全局键盘钩子记录键盘输入
  8. 无root手机版抓android包工具,超好用
  9. STL技术——STL概述和入门
  10. 棘轮源码_通过棘轮轻松实现原型移动应用程序