如何通过 PHP 执行 WKHTMLTOPDF?

发布于 2024-11-01 03:14:41 字数 1639 浏览 4 评论 0原文

之前已经在某种程度上被问过,但没有解决方案或可接受的答案,我想尝试在我的问题中更全面,因此:

我正在尝试通过 PHP 在共享服务器上运行 WKHTMLTOPDF 并运行(在此如果是 MediaTemple (gs))。根据主机的说法,这没有理由不起作用,事实上它是通过 SSH 工作的。所以......

我尝试了各种各样的事情,最基本的没有任何作用,只是默默地失败:

exec("/path/to/binary/wkhtmltopdf http://www.google.com pdf1.pdf");

- 来源:Stack Overflow 上的问题

完整的 PHP 绑定以及以下内容给了我错误,尽管我最好的谷歌搜索我无法弄清楚:

呼叫:

$html = file_get_contents("http://www.google.com");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');

- 来源:Google 代码上的 WKHTMLTOPDF

错误:

Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return
any data. <pre>Loading pages (1/6) [> ] 0% [======> ] 10% terminate called
after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc </pre>'
in /path/to/wkhtmltopdf.php:206 Stack trace: #0 /path/to/index.php(8):
WKPDF->render() #1 {main} thrown in /path/to/wkhtmltopdf.php on line 206

一旦我得到了这个(下面是摘录,因为我现在无法重现它):

Qt Concurrent has caught an exception thrown from a worker thread. This is not
supported, exceptions thrown in worker threads must be caught before
control returns to Qt Concurrent.

我还尝试了其他一些选项但结果相同;没有 PDF。那么我现在该怎么办,如何找出问题所在?我的PHP水平还处于初级阶段,但我会尽力而为。

This has been asked to some degree before but there are no solutions or accepted answers and I'd like to try and be more comprehensive in my question so:

I'm trying to WKHTMLTOPDF up and running via PHP on a shared server (in this case it's MediaTemple (gs)). According to the host there is no reason this won't work and in fact it is working via SSH. So...

I've tried a variety of things, the most basic does nothing, just silently fails:

exec("/path/to/binary/wkhtmltopdf http://www.google.com pdf1.pdf");

- Source: Question on Stack Overflow

The full PHP bindings along with the following give me errors, which despite my best Googling I can't figure out:

Call:

$html = file_get_contents("http://www.google.com");
$pdf = new WKPDF();
$pdf->set_html($html);
$pdf->render();
$pdf->output(WKPDF::$PDF_EMBEDDED,'sample.pdf');

- Source: WKHTMLTOPDF on Google Code

Error:

Fatal error: Uncaught exception 'Exception' with message 'WKPDF didn't return
any data. <pre>Loading pages (1/6) [> ] 0% [======> ] 10% terminate called
after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc </pre>'
in /path/to/wkhtmltopdf.php:206 Stack trace: #0 /path/to/index.php(8):
WKPDF->render() #1 {main} thrown in /path/to/wkhtmltopdf.php on line 206

And once I got this (below is an extract as I can't reproduce it now):

Qt Concurrent has caught an exception thrown from a worker thread. This is not
supported, exceptions thrown in worker threads must be caught before
control returns to Qt Concurrent.

I've also tried a few other options but with the same results; no PDF. So what do I do now, how do I figure out what's wrong? My PHP level is on the basic side but I'll do my best.

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

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

发布评论

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

评论(7

白云不回头 2024-11-08 03:14:41

您也可以在这里尝试我的项目。它为命令行实用程序提供了一个干净的 OO 界面:

https://github.com/mikehaertl/phpwkhtmltopdf

用法非常简单:

<?php
use mikehaertl\wkhtmlto\Pdf; 

$pdf = new Pdf;

// Add a HTML file, a HTML string or a page from a URL
$pdf->addPage('/home/joe/page.html');
$pdf->addPage('<html>....</html>');
$pdf->addPage('http://google.com');

// Add a cover (same sources as above are possible)
$pdf->addCover('mycover.html');

// Add a Table of contents
$pdf->addToc();

// Save the PDF
$pdf->saveAs('/tmp/new.pdf');

// ... or send to client for inline display
$pdf->send();

You can also try my project here. It provides a clean OO interface to the command line utility:

https://github.com/mikehaertl/phpwkhtmltopdf

Usage is very simple:

<?php
use mikehaertl\wkhtmlto\Pdf; 

$pdf = new Pdf;

// Add a HTML file, a HTML string or a page from a URL
$pdf->addPage('/home/joe/page.html');
$pdf->addPage('<html>....</html>');
$pdf->addPage('http://google.com');

// Add a cover (same sources as above are possible)
$pdf->addCover('mycover.html');

// Add a Table of contents
$pdf->addToc();

// Save the PDF
$pdf->saveAs('/tmp/new.pdf');

// ... or send to client for inline display
$pdf->send();
鸠魁 2024-11-08 03:14:41

看一下 snappy,一个 PHP5 库,允许从 url 或 html 页面生成缩略图、快照或 PDF 。它是 wkhtmltopdf/wkhtmltoimage 的包装。

Have a look at snappy, a PHP5 library allowing thumbnail, snapshot or PDF generation from a url or a html page. It's a wrapper for wkhtmltopdf/wkhtmltoimage.

花开半夏魅人心 2024-11-08 03:14:41

对于 Linux:

exec('wkhtmltopdf http://somesite.com /home/user/file.pdf 2>&1');

对于 Windows:

<?php
exec('C://abc//wkhtmltopdf http://google.html form1.pdf');

echo "PDF Created Successfully";
?>

For Linux:

exec('wkhtmltopdf http://somesite.com /home/user/file.pdf 2>&1');

For Windows:

<?php
exec('C://abc//wkhtmltopdf http://google.html form1.pdf');

echo "PDF Created Successfully";
?>
黯然#的苍凉 2024-11-08 03:14:41

您可能需要设置适当的标题并将其设置为 application/pdf 以及将其设置为内联,以便人们可以在浏览器中看到它(几乎每个最新的浏览器都支持此功能)并根据需要保存它。这是我正在使用的代码:

exec("/usr/local/bin/wkhtmltopdf ../uploads/test.pdf");
$file = "../uploads/test.pdf";
$pdf = file_get_contents("../uploads/test.pdf");

header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($file).'";');
ob_clean(); 
flush(); 
echo $pdf;
?>

You may want to set up proper headers and set it to application/pdf as well as setting it to inline so that people can see it in their browsers (almost every recent browser supports this) and save it if they want. This is the code that I'm using:

exec("/usr/local/bin/wkhtmltopdf ../uploads/test.pdf");
$file = "../uploads/test.pdf";
$pdf = file_get_contents("../uploads/test.pdf");

header('Content-Type: application/pdf');
header('Cache-Control: public, must-revalidate, max-age=0'); // HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));
header('Content-Disposition: inline; filename="'.basename($file).'";');
ob_clean(); 
flush(); 
echo $pdf;
?>
王权女流氓 2024-11-08 03:14:41

要从 php(在 Linux 上)创建 pdf,您必须使用包装器。

$cmd = '/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/bin/wkhtmltopdf http://google.com /tmp/google.pdf';

exec($cmd);

To create a pdf from php (on linux) you must use a wrapper.

$cmd = '/usr/bin/xvfb-run --server-args="-screen 0, 1920x1080x24" /usr/bin/wkhtmltopdf http://google.com /tmp/google.pdf';

exec($cmd);
稳稳的幸福 2024-11-08 03:14:41

当你运行时会发生什么:

$out = array();
exec("/path/to/binary/wkhtmltopdf http://www.google.com pdf1.pdf", $out);
print_r($out);

我的猜测是,由于你没有为该文件指定任何文件夹,它会尝试将其写入当前工作目录。网络用户可能没有该文件夹的写入权限(很可能就是这种情况)。因此,您应该能够通过提供网络用户可写入的文件夹的完整路径来解决此问题。

IE。

exec("/path/to/binary/wkhtmltopdf http://www.google.com /path/to/pdf1.pdf");

除此之外,我不太确定 wkhtmltopdf 是否可以无头工作,它可能需要一个正在运行的 X 服务器(在不运行 X 的服务器上,您可以通过安装 Xvfb 并使用 xvfb-run 运行 wkhtmltopdf)。

And what happens when you run :

$out = array();
exec("/path/to/binary/wkhtmltopdf http://www.google.com pdf1.pdf", $out);
print_r($out);

My guess is that since you are not specifying any folder for the file, it will try to write it to the current working directory. It could be (well most probably this is the case) that the webuser doesn't have write permission on that folder. So you should by able to solve this by providing the full path to a folder that is writeable by the webuser.

ie.

exec("/path/to/binary/wkhtmltopdf http://www.google.com /path/to/pdf1.pdf");

Apart from that I'm not quite sure that wkhtmltopdf can work headless, it could be that it requires a running X server (on a server not running X you could solve this by installing Xvfb and using xvfb-run to run wkhtmltopdf).

深白境迁sunset 2024-11-08 03:14:41

这对我来说是一种预生成 PDF 报告的方法,然后使用唯一的会话 ID 从请求页面(同一会话的)获取它。

$cmd = "/usr/local/bin/wkhtmltopdf 'http://127.0.0.1/myApp/pdfReport.php?key=something' tmp_reports/report_".$_POST['sessionid'].".pdf";
exec($cmd);

This worked for me as a way to pre-generate a PDF report then use a unique session ID to fetch it from the requesting page (of the same session).

$cmd = "/usr/local/bin/wkhtmltopdf 'http://127.0.0.1/myApp/pdfReport.php?key=something' tmp_reports/report_".$_POST['sessionid'].".pdf";
exec($cmd);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文