php 基于tcpdf插件生成pdf
之前在公司做了个项目,,需要导出pdf合同,,在网上找了很久,选择用了tcpdf插件,,具体的插件网上可以搜到,中间遇到了很多的坑,慢慢的填。
先下好插件放到指定文件夹下

然后使用tcpdf插件里html生成pdf,,,不过有好多复杂html的样式tcpdf识别不了,,,简单点的可以。
require_once(FW_PATH.'/plugins/tcpdf/tcpdf.php');
class Controller_Pdf_Gps extends Controller_Base {
/**
*
* @param string $html
* @param string $title
* @author wy
*/
public function actionWriteToPdf(){
$contractCarrentId = isset($_REQUEST['contract_carrent_id']) ? $_REQUEST['contract_carrent_id'] : 0;
$productTypeId = isset($_REQUEST['product_type_id']) ? $_REQUEST['product_type_id'] : 0;
$didiType = isset($_REQUEST['didi_type']) ? $_REQUEST['didi_type'] : 0;//滴滴合同没有附件1、2标识, 用didi_type区分
//I,默认值,在浏览器中打开;D,PDF文件会被下载下来;F,文件会被保存在服务器中;S,PDF会以字符串形式输出;E:PDF以邮件的附件输出。
$title = isset($_REQUEST['title']) ? $_REQUEST['title'] : 'GPS安装确认及使用规则告知书';
$headerLogo = isset($_REQUEST['header_logo']) ? $_REQUEST['header_logo'] : 'caixin1.png';
$headerLogoWidth = isset($_REQUEST['header_logo_width']) ? $_REQUEST['header_logo_width'] : 30;
$headerTitle = isset($_REQUEST['header_title']) ? $_REQUEST['header_title'] : '';
$headerString = isset($_REQUEST['header_string']) ? $_REQUEST['header_string'] : ' 发过火发过火发过火有限公司';
if($contractCarrentId <= 0) {
return $this->error('合同(租车)ID错误');
}
if($productTypeId <= 0) {
return $this->error('产品类型ID错误');
}
//contract_carrent
$contractCarrent = Remote::instance()->get(CAIXIN_BASE_DOMIAN, 'contract/carrent/detail', array('contract_carrent_id' => $contractCarrentId));
if($contractCarrent['code'] != 200 || !isset($contractCarrent['data']['contract_carrent'])) {
return $this->error('获取合同(租车)信息错误');
}
$contractCarrent = $contractCarrent['data']['contract_carrent'];
//application_control
$appControl = Remote::instance()->get(CAIXIN_BASE_DOMIAN, 'application/control/detail', array('application_control_id' => $contractCarrent['application_control_id']));
if($appControl['code'] != 200 || !isset($appControl['data']['application_control'])) {
return $this->error('获取申请流程控制信息错误');
}
$appControl = $appControl['data']['application_control'];
$html = '';
$html = $this->personInfo($appControl, $title, $didiType);
if($html == '') {
return $this->error('获取不到转换页面');
}
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetTitle($title); //设置文件的title
$pdf->SetHeaderData($headerLogo, $headerLogoWidth, $headerTitle, $headerString);//设置头部,比如header_logo,header_title,header_string及其属性
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); //设置页头字体
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); //设置页尾字体
$pdf->setPrintHeader(true); //页面头部横线 false取消
$pdf->setPrintFooter(true); //页面底部更显 false取消
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
$pdf->SetMargins(PDF_MARGIN_LEFT, 13, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);//自动分页
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->AddPage();//增加一个页面
$pdf->setPageMark();
$pdf->SetFont('stsongstdlight', '', 13);
$pdf->writeHTML($html, true, true, true, true, '');
// $pdf->writeHTML($html, true, 0, true, true);
// $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, 'C', true);
$pdf->lastPage();
ob_end_clean();
//写入文件
$time = time();
$content = $pdf->Output($time.'.pdf', 'S');
if(in_array($productTypeId, array(4,5))) {
$category_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夹/文件名';
$file_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夹/文件名/'.$title.date('Ymd',time()).'.pdf';
}else if(in_array($productTypeId, array(6,7))) {
$category_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夹/文件名';
$file_path = WWW_ROOT.'/tmp_zip/合同'.$contractCarrentId.'/文件夹/文件名/'.$title.date('Ymd',time()).'.pdf';
}
if (!file_exists($category_path)){
mkdir($category_path,0777,true);
}
$fp = fopen($file_path,'w') or die("Unable to open file!");
fwrite($fp,$content);
fclose($fp);
return $this->output('ok');
}
以上有些业务。
/**
* 个人
* @author yxf
*/
private function personInfo($appControl, $title, $didiType) {
//门店
$store = Remote::instance()->get(CAIXIN_BASE_DOMIAN, 'store/detail', array('store_id' => $appControl['store_id']));
if($store['code'] != 200 || !isset($store['data']['store'])) {
return $this->error('获取门店信息错误');
}
$store = $store['data']['store'];
if($appControl['customer_type'] != 1) {
return $this->error('客户性质错误');
}
//申请单(自然人)
$appPerson = Remote::instance()->get(CAIXIN_BASE_Y_DOMIAN, 'applicationperson/detail', array('application_person_id' => $appControl['application_person_id']));
if($appPerson['code'] != 200 || !isset($appPerson['data']['application_person'])) {
return $this->error($appPerson['msg']);
}
$appPerson = $appPerson['data']['application_person'];
$appPerson['name'] = !empty($appPerson) ? $appPerson['name'] : '';
/* 附件二:GPS安装确认及使用规则告知书 */
$html = <<<EOD
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{$title}</title>
<style>
body {
padding: 10px;
}
.color-red {
color: red;
text-decoration: underline;
}
h1 {
text-align: center;
font-size: 22px;
position: relative;
}
h3 span {
position: absolute;
left: 0;
top: 0;
}
h2 {
font-size: 16px;
margin: 5px 0;
position: relative;
}
h3 {
font-size: 16px;
text-align: center;
position: relative;
margin-top: 50px;
margin-bottom: 50px;
}
p {
font-size: 14px;
}
table {
border-collapse: collapse;
width: 100%;
}
th {
border: 1px solid #000;
}
td {
border: 1px solid #000;
padding: 5px 10px;
font-size: 13px;
line-height: 30px;
height: 30px;
}
td img {
max-width: 100%;
}
.title-xl {
font-size: 16px;
}
.title {
background: #d4d4d4;
width: 110px;
text-align: center;
}
.underline_xl {
width: 200px;
display: inline-block;
border-bottom: 1px solid #000;
vertical-align: bottom;
}
.underline_l {
width: 150px;
display: inline-block;
border-bottom: 1px solid #000;
vertical-align: bottom;
}
.underline_s {
width: 50px;
display: inline-block;
border-bottom: 1px solid #000;
vertical-align: bottom;
}
.item {
display: table;
}
.text-no {
display: table-cell;
width: 50px;
padding-left: 20px;
}
.text-no + span {
display: table-cell;
}
.check {
border: 1px solid #000;
display: inline-block;
width: 14px;
height: 14px;
position: relative;
top: 3px;
}
.uncheck {
border: 1px solid #000;
display: inline-block;
width: 14px;
height: 14px;
position: relative;
top: 3px;
overflow: hidden;
}
.uncheck:after {
content: 'X';
position: absolute;
top: 0;
left: 0px;
font-size: 24px;
font-style: normal;
display: block;
font-weight: 100;
width: 14px;
height: 14px;
line-height: 16px;
text-align: center;
}
.unline {
border-bottom: 1px solid #000;
}
.unline .underline_s {
border: 0;
vertical-align: bottom;
}
.no_border {
border: 0;
}
.width_s {
width: 110px;
text-align: center;
}
</style>
</head>
<body>
<p style="font-weight: bold">
EOD;
$html2 = '';
if($didiType != 1) {
$html2 .= <<<EOD
附件二:使用规则告知书
EOD;
}
$html2 .= <<<EOD
</p>
<h1 style="text-align:center;">使用规则告知书</h1>
<br/><br/>
<p>尊敬的客户:{$appPerson['name']}<br/>
</p>
<div style="text-align:right;">
<p>公司:{$store['invoice']}
</p>
</div>
<br/>
<div style="text-align:right;">
<p>
年
月
日</p>
</div>
<p><b>本人已阅读、理解并同意上述规则条款,自愿执行上述规则,如因违反上述规则导致的任何后果由本人自行承担。</b>
</p>
<div style="text-align: right;">
<p>客户(签字): </p>
<p>
年
月
日</p>
</div>
</body>
</html>
EOD;
return $html1.$html2;
}
以上具体的html,自行设计。

php 基于tcpdf插件生成pdf的更多相关文章
- 使用TCPDF插件生成pdf以及pdf的中文处理
目录(?)[+] 多种多样的pdf开发库 WKHTMLTOPDF 2FPDF 3TCPDF 中文问题 做了这么多年项目,以前只是在别人的项目中了解过PHP生成pdf文件,知道并不难,但是涉及到了p ...
- vue通过插件实现PDF生成以及下载
第一步: 安装插件 npm install --save html2canvas //将html转换成图片 npm install jspdf --save //将图片生成pdf 第二步: 在需要的地 ...
- 前端生成PDF,让后端刮目相看
PDF 简介 PDF 全称Portable Document Format (PDF)(便携文档格式),该格式的显示与操作系统.分辨率.设备等因素没有关系,不论是在Windows,Unix还是在苹果公 ...
- php生成pdf,php+tcpdf生成pdf, php pdf插件
插件例子:https://tcpdf.org/examples/ 下载tcpdf插件: demo // Include the main TCPDF library (search for insta ...
- 赞!jsPDF – 基于 HTML5 的强大 PDF 生成工具
jsPDF 是一个基于 HTML5 的客户端解决方案,用于生成各种用途的 PDF 文档.使用方法很简单,只要引入 jsPDF 库,然后调用内置的方法就可以了.浏览器兼容性: IE 10, Firefo ...
- jsPDF – 基于 HTML5 的强大 PDF 生成工具
jsPDF 是一个基于 HTML5 的客户端解决方案,用于生成各种用途的 PDF 文档. 使用方法很简单,只要引入 jsPDF 库,然后调用内置的方法就可以了. 米扑科技项目用到了HHTML5生成PD ...
- PHP生成PDF完美支持中文,解决TCPDF乱码
PHP生成PDF完美支持中文,解决TCPDF乱码 2011-09-26 09:04 418人阅读 评论(0) 收藏 举报 phpfontsheaderttfxhtml文档 PHP生成PDF完美支持中文 ...
- tcpdf 将网页生成pdf
需求:需要将HTML页面生成PDF文档 开发语言:PHP 使用TCPDF第三方类库进行生成,下载地址:http://sourceforge.net/projects/tcpdf/ 核心代码: publ ...
- php+tcpdf生成pdf: 中文乱码
TCPDF是一个生成PDF的不错的库,可惜,官方对包括中文在内的东亚字体支持不怎么样的.场景:某项目需要根据数据库信息生成pdf格式的发票,考虑采用稳定的tcpdf,虽然还有许多其它选择,但是这个应该 ...
随机推荐
- 【转】基于Jenkins实现持续集成【持续更新中】
知识预览 持续集成 Jenkins安装 Jenkins插件 Jenkins配置 Jenkins备份与恢复 发布PHP项目 SVN 发布Maven项目 按版本发布 远程管理 War文件部署设置 任务 J ...
- Simple Factory Pattern
Question: How do you achieve the functions of calculator by Object-Oriented ? Analysis: 1,The functi ...
- 20175208 实验一 Java开发环境的熟悉
20175208 实验一 Java开发环境的熟悉 一.实验报告封面 课程:Java程序设计 班级:1752班 姓名:张家华 学号:20175208 指导教师:娄嘉鹏 实验日期:2019年4月2日 实 ...
- Node.js基础学习三之登录功能
本篇介绍Node.js访问数据库并返回数据给客户端 需求基于Node.js学习(二) 数据库请下载:user.sql 1.创建user 实体类(model-user.js) function User ...
- 用keytool制作证书并在tomcat配置https服务(一)
https分为单项认证和双向认证. 一般https页面上的访问都是单项认证,服务端发送数字证书给客户端,客户单方面验证.而服务端不做验证. 而双向认证,需要双方都有证书,然后发送给对方进行验证.一般用 ...
- log4j日志实现重复警告slf4j-jdk14和log4j-over-slf4j
因为各种三方库依赖的log4j实现不同,所以可能会出现找到多个log4j实现的警告,但是不影响程序(logback是会影响的),如下: SLF4J: Class path contains multi ...
- 【题解】Luogu CF817F MEX Queries
原题传送门 817,我突然想到了某8位质数 这题珂以说是珂朵莉树的模板 三个操作都肥肠简单,前两个区间赋值,第三个区间0变1,1变0 每次输出从头开始扫描就行(我忘了珂朵莉树的性质,竟然还动态维护最左 ...
- 软件测试4gkd
一.性能测试有几种类型,它们之间什么关系? (1)性能测试包括:负载测试.压力测试.配置测试.并发测试.容量测试.可靠性测试.失败测试. 负载测试:通过逐渐增加系统的负载,测试系统性能的变化,并最终确 ...
- 持久层Mybatis3底层源码分析,原理解析
Mybatis-持久层的框架,功能是非常强大的,对于移动互联网的高并发 和 高性能是非常有利的,相对于Hibernate全自动的ORM框架,Mybatis简单,易于学习,sql编写在xml文件中,和代 ...
- jQuery Gantt Edit:(一)参数以及方法说明
jQuery Gantt editor jQuery Gantt Edit(以下简称GE)是一款开源的基于jQuery的甘特图插件,作者:robicch. GitHub地址:https://githu ...