在php中将html转换为pdf?

发布于 2024-10-09 12:36:26 字数 229 浏览 0 评论 0原文

我知道有很多关于这个问题的帖子。但是有人可以帮我设置这个 http:// www.rustyparts.com/pdf.php 脚本在我的本地主机上工作。我刚刚花了一周的时间来解决这个问题。我已经下载了 imagemagic,ghostscript,activeperl,...,一切,但仍然无法制作简单的例子来工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心头的小情儿 2024-10-16 12:36:26

通过系统调用使用 wkhtmltopdf。请参阅 如何在基于 Linux 的(共享主机)上安装 wkhtmltopdf ) Web 服务器 获取安装帮助。

wkhtmltopdf是一个命令行程序
它允许从
url、本地 html 文件或 stdin。它
生成类似于 rendred 的 pdf
WebKit 引擎。

请在此处查看本页的示例。

在 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 绑定 无需自己使用系统调用,这是一个更简单(也更安全!)的选项。

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

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();
}
如此安好 2024-10-16 12:36:26

简单但功能强大: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

Simple but powerful: 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
泼猴你往哪里跑 2024-10-16 12:36:26

另外,还有另一个生成 PDF 文件的库:TCPDF。很好而且很简单。你可以在周围找到很多例子。

Also, there is another library that generates PDF files: TCPDF. Nice and quite simple. You can find many examples around.

江南月 2024-10-16 12:36:26

DocRaptor.com 是一个不错的选择 - 它使用 Prince XML,因此质量比其他工具更好,而且它是一个网络应用程序,所以没有什么可下载的。它也适用于任何语言。

DocRaptor.com is 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文