如何通过PHP执行WKHTMLTOPDF?

Jam*_*mie 48 php wkhtmltopdf

这在某种程度上已被要求,但没有解决方案或接受的答案,我想尝试在我的问题中更全面,所以:

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

我尝试过各种各样的东西,最基本的东西什么也没做,只是默默地失败了:

exec("/path/to/binary/wkhtmltopdf http://www.google.com pdf1.pdf");
Run Code Online (Sandbox Code Playgroud)

- 来源: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');
Run Code Online (Sandbox Code Playgroud)

- 来源:Google Code上的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
Run Code Online (Sandbox Code Playgroud)

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

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.
Run Code Online (Sandbox Code Playgroud)

我也试过了一些其他的选择,但结果相同; 没有PDF.那我现在该怎么办,怎么弄清楚出了什么问题呢?我的PHP级别是基本的,但我会尽我所能.

Mic*_*rtl 36

你也可以在这里试试我的项目.它为命令行实用程序提供了一个干净的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();
Run Code Online (Sandbox Code Playgroud)

  • 一个"真正的"教程可能非常好像在Windows系统上安装,没有作曲家安装(有些人不使用它),列出所有可用的选项... (8认同)
  • 如何安装WITHOUT composer?我无法访问服务器!我尝试包含所有三个文件并'使用mikehaertl\wkhtmlto\Pdf;',但它返回一个错误:"类'mikehaertl\shellcommand\Command'未找到".你能帮助我吗? (4认同)
  • @RafaelMoni你可以从安装了`composer`的盒子中复制它:在一些临时位置安装包,然后将`vendor /`目录复制到你的项目中. (3认同)

小智 15

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


Bri*_*n F 10

您可能需要设置正确的标题并将其设置为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;
?>
Run Code Online (Sandbox Code Playgroud)


sof*_*nic 8

对于Linux:

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

对于Windows:

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

echo "PDF Created Successfully";
?>
Run Code Online (Sandbox Code Playgroud)


小智 5

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

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

exec($cmd);
Run Code Online (Sandbox Code Playgroud)


wim*_*vds 2

当你运行时会发生什么:

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

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

IE。

exec("/path/to/binary/wkhtmltopdf http://www.google.com /path/to/pdf1.pdf");
Run Code Online (Sandbox Code Playgroud)

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