html-to-pdf-for-php

需求:报表页面点击下载后转成pdf文件;

之前找了很多插件,用下来wkhtmltopdf最好用;

下面说下如何操作

首先下载插件,最好翻墙下载,不然很慢,最好下载tar.gz包,不要到github上clone,文件太大;

http://wkhtmltopdf.org/downloads.html



拷贝文件到命令目录

tar xf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz
cd wkhtmltox/bin
cp wkhtmltopdf /usr/local/bin/

html页面

<button class=”btn-success btn button” style=”margin-left: 85%” onclick=”print_pdf()”>下载PDF</button>
<form action=”<?php echo U(‘Custorm/savePdf’) ?>” method=”post” name=”hld_res” id=”hideform”>
<input type=”hidden” id=”hide_content” name=”content” />
<input type=”hidden” name=”title” value=”<?php echo $data[0][‘title’]?>”/>
</form>

JS方法

function print_pdf(){
//下载范围
bdhtml=window.document.body.innerHTML;
sprnstr=”<!–startprint–>”;
eprnstr=”<!–endprint–>”;
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr)+17);
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));
// 将获取的html代码添加到隐藏域中传给php文件处理
$(“#hide_content”).val(“”+prnhtml+””);
$(“#hideform”).submit();
}

PHP方法

public function savePdf()
{
$html = $_REQUEST[‘content’];
ob_start();
$html=’
<html>
<link href=”http://your_domain_name/Public/css/style.css" rel=”stylesheet”>
<link href=”http://your_domain_name/Public/css/style-responsive.css" rel=”stylesheet”>
<link type=”text/css” rel=”stylesheet” href=”http://your_domain_name/Public/js/bootstrap-table/bootstrap-table.css">
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8” />
<body style=”background: white;”> ‘.$html;
$html .= ‘ </body></html>’;
$filename = $_REQUEST[‘title’] ? $_REQUEST[‘title’] : ‘报表-‘.date(‘Y-m-d’);
file_put_contents(“{$filename}.html”, $html);
ob_end_clean();
//转换HTML TO PDF
sh_exec(“/usr/local/bin/wkhtmltopdf -q -s A2 -O Landscape {$filename}.html {$filename}.pdf”);
if(file_exists(“{$filename}.pdf”)){
header(‘Content-type:application/octet-stream’);
header(“Content-Disposition:attachment;filename={$filename}.pdf”);
header(‘Content-Length:’.filesize(“{$filename}.pdf”));
readfile(“{$filename}.pdf”);
//删除本地的文件
unlink(“{$filename}.pdf”);
unlink(“{$filename}.html”);
}else{
exit;
}
}

pdf文档效果

效果还是不错的;

这里遇到一个问题,就是线上的sh_exec()函数不执行。改目录权限,改sudo配置等等,都没效果,然后去配置文件里查看php.ini, 里面添加了disable_functions,去掉你要用的函数就可以了。

参考:http://blog.csdn.net/qq_14873105/article/details/51394026

坚持原创技术分享,您的支持将鼓励我继续创作!