How do I get WKHTMLTOPDF to execute via PHP?

拜拜、爱过 提交于 2019-11-26 10:27:51

问题


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.


回答1:


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



回答2:


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.




回答3:


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;
?>



回答4:


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



回答5:


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



回答6:


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).




回答7:


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


来源:https://stackoverflow.com/questions/5663814/how-do-i-get-wkhtmltopdf-to-execute-via-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!