如何获得WKHTMLTOPDF通过PHP执行? [英] How do I get WKHTMLTOPDF to execute via PHP?

查看:539
本文介绍了如何获得WKHTMLTOPDF通过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:

我正在尝试WKHTMLTOPDF并通过PHP在共享服务器上运行(在本例中为MediaTemple(gs)).据主持人说,没有理由不起作用,实际上它是通过SSH起作用的.所以...

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");

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

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

致电:

$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

一旦我明白了(下面是摘录,因为我现在无法重现):

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.

我也尝试了其他一些选择,但结果相同.没有PDF.那么,我现在该怎么办,我该如何找出问题所在?我的PHP级别是基本方面,但我会尽力而为.

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.

推荐答案

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

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

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

这篇关于如何获得WKHTMLTOPDF通过PHP执行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆