在PHP中将html转换为pdf?

开发技术 作者: 2024-06-30 12:25:01
我知道有很多关于这个问题的帖子.但是有人可以帮我设置这个 http://www.rustyparts.com/pdf.php脚本来处理我的本地主机.我只花了整整一周就这个问题.我有下载imagemagic,ghostscript,activeperl,… ,一切,但仍然不能使简单的例子工作. 通过系统调用使用 wkhtmltopdf.有关安装帮助,请参见 How to install wkhtml
我知道有很多关于这个问题的帖子.但是有人可以帮我设置这个 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();
}
原创声明
本站部分文章基于互联网的整理,我们会把真正“有用/优质”的文章整理提供给各位开发者。本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
本文链接:http://www.jiecseo.com/news/show_36134.html
php 中将 html 转换 pdf