在PHP中将html转换为pdf? [英] Converting html to pdf in php?

查看:139
本文介绍了在PHP中将html转换为pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多关于这个问题的帖子,但有人可以帮我设置这个 http ://www.rustyparts.com/pdf.php 脚本在我的本地主机上工作。我只是花了整整一周的时间解决这个问题。我已经下载imagemagic,ghostscript,activeperl,...,一切,但仍然可以没有简单的例子来工作。 使用解析方案 noreferrer> wkhtmltopdf 通过系统调用。请参阅如何在基于Linux的(共享主机)上安装wkhtmltopdf )web服务器以获得安装帮助。


wkhtmltopdf是一个命令行程序
允许创建PDF
网址,本地html文件或stdin。它的
使用
WebKit引擎生成一个像rendred的pdf。

查看本页中的示例这里



php代码测试在Ubuntu上(您需要将 / 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绑定,它不需要自己使用系统调用,这是一个更简单(更安全!)的选项。

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


I know that there is a lot of posts about this problem.But can someone help me to setup this http://www.rustyparts.com/pdf.php script to work on my localhost.I just spent all week on this problem.I have download imagemagic,ghostscript,activeperl,...,everything,but still can't make simple example to work.

解决方案

Use wkhtmltopdf via a system call. See How to install wkhtmltopdf on a linux based (shared hosting) web server for installation help.

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.

See the sample from this page here.

php code tested on Ubuntu (you'll need to change the /tmp/ to a temporary directory on 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);

There are also php bindings which removes the need to use a system call yourself, which is a simpler (and safer!) option.

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();
}

这篇关于在PHP中将html转换为pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆