php 在php中将html转换为pdf?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4542230/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting html to pdf in php?
提问by user147
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.phpscript 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.
我知道有很多关于这个问题的帖子。但是有人可以帮我设置这个http://www.rustyparts.com/pdf.php脚本来在我的本地主机上工作。我花了整整一周的时间来解决这个问题。我已经下载了 imagemagic、ghostscript、activeperl、...,所有东西,但仍然无法制作简单的示例来工作。
回答by moinudin
Use wkhtmltopdfvia a system call. See How to install wkhtmltopdf on a linux based (shared hosting) web serverfor installation help.
通过系统调用使用wkhtmltopdf。有关安装帮助,请参阅如何在基于 linux(共享主机)的 Web 服务器上安装 wkhtmltopdf。
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.
wkhtmltopdf 是一个命令行程序,它允许从 url、本地 html 文件或 stdin 创建 pdf。它使用 WebKit 引擎生成类似于渲染的 pdf。
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):
在 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);
There are also php bindingswhich removes the need to use a system call yourself, which is a simpler (and safer!) option.
还有一些php 绑定,它消除了自己使用系统调用的需要,这是一个更简单(更安全!)的选项。
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();
}
回答by Dejan Marjanovic
Simple but powerful: http://html2pdf.fr/en/default
简单但功能强大:http: //html2pdf.fr/en/default
$html = file_get_contents("valid.html");
require_once("html2pdf.class.php");
$html2pdf = new HTML2PDF("P", "A4", "en", array(10, 10, 10, 10));
$html2pdf->setEncoding("ISO-8859-1");
$html2pdf->WriteHTML($html);
$html2pdf->Output("pdf/PDF.pdf", "F"); //output to file
$html2pdf->Output(); //output to browser
回答by Julie
DocRaptor.comis a good option - it uses Prince XML, so the quality is better than other tools, and it's a web app, so nothing to download. It also works in any language.
DocRaptor.com是一个不错的选择——它使用 Prince XML,所以质量比其他工具要好,而且它是一个网络应用程序,所以没有什么可以下载的。它也适用于任何语言。