加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在PHP中将html转换为pdf?

发布时间:2020-12-13 16:27:51 所属栏目:PHP教程 来源:网络整理
导读:我知道有很多关于这个问题的帖子.但是有人可以帮我设置这个 http://www.rustyparts.com/pdf.php脚本来处理我的本地主机.我只花了整整一周就这个问题.我有下载imagemagic,ghostscript,activeperl,…,一切,但仍然不能使简单的例子工作. 通过系统调用使用 wkhtm
我知道有很多关于这个问题的帖子.但是有人可以帮我设置这个 http://www.rustyparts.com/pdf.php脚本来处理我的本地主机.我只花了整整一周就这个问题.我有下载imagemagic,ghostscript,activeperl,…,一切,但仍然不能使简单的例子工作.
通过系统调用使用 wkhtmltopdf.有关安装帮助,请参见 How to install wkhtmltopdf on a linux based (shared hosting) web server.

wkhtmltopdf is a command line program
which permits to create a pdf from an
url,a local html file or stdin. It
produces a pdf like rendred with the
WebKit engine.

请参阅本页here中的示例.

在Ubuntu上测试的php代码(你需要将/ tmp /更改为Windows上的临时目录):

$url = 'http://www.google.com';
$pdffile = tempnam('/tmp/','wkhtmltopdf_');
$handle = popen("wkhtmltopdf $url $pdffile 2>&1","r");
while (!feof($handle)) 
  fread($handle,4096);
pclose($handle);
header('Content-type: application/pdf');
$file = fopen($pdffile,"r");
while(!feof($file))
  print fread($file,4096);
unlink($pdffile);

还有php bindings,它不需要自己使用系统调用,这是一个更简单(和更安全!)的选项.

try {
    $wkhtmltopdf = new Core_Wkhtmltopdf(array('path' => APPLICATION_PATH . '/../public/uploads/'));
    $wkhtmltopdf->setTitle("Title");
    $wkhtmltopdf->setHtml("Content");
    $wkhtmltopdf->output(Wkhtmltopdf::MODE_DOWNLOAD,"file.pdf");
} catch (Exception $e) {
    echo $e->getMessage();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读