How do I get WKHTMLTOPDF to execute via PHP?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 03:14:59

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

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.

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

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

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

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

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