问题
I use the class mentioned in the link How do I get WKHTMLTOPDF to execute via PHP? to convert html to pdf using wkhtmltopdf via PHP.
require_once('WkHtmlToPdf.php');
$link = "https://www.google.co.in";
$pdf = new WkHtmlToPdf;
$options = array(
'no-outline', // option without argument
'encoding' => 'UTF-8', // option with argument
'binPath' => '/usr/bin/wkhtmltopdf', // absolute actual file path
);
$pdf->setOptions($options);
$pdf->addPage($link);
$pdf->send();
if (!$pdf->send('test.pdf')) {
throw new Exception('Could not create PDF: '.$pdf->getError());
}
Wkhtmltopdf is installed on my webserver and i could able to convert any html page to pdf from command line with no error logs
When i try to convert HTML to PDF via PHP class, i get the following exception
Fatal error: Uncaught exception 'Exception' with message 'Could not create PDF: Could not run command '/usr/bin/wkhtmltopdf' --no-outline --encoding 'UTF-8' https://www.google.co.in /tmp/tmp_WkHtmlToPdf_CdfhSw: Loading pages (1/6) [> ] 0% [======> ] 10% ' in test.php:52 Stack trace: #0 {main} thrown on test.php:52
I also get following in the error log
[11-Sep-2014 10:32:47 Asia/Kolkata] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20100525/pdo.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20100525/pdo.so: cannot open shared object file: No such file or directory in Unknown on line 0
[11-Sep-2014 10:32:47 Asia/Kolkata] PHP Warning: PHP Startup: Unable to load dynamic library '/usr/local/lib/php/extensions/no-debug-non-zts-20100525/pdo_mysql.so' - /usr/local/lib/php/extensions/no-debug-non-zts-20100525/pdo_mysql.so: cannot open shared object file: No such file or directory in Unknown on line 0
[11-Sep-2014 10:32:53 Asia/Kolkata] PHP Fatal error: Uncaught exception 'Exception' with message 'Could not create PDF: Could not run command '/usr/bin/wkhtmltopdf' https://google.co.in /tmp/tmp_WkHtmlToPdf_P5ykhK:
Loading pages (1/6)
[> ] 0%
[======> ] 10%
' in test.php:51
Stack trace:
#0 {main}
thrown in test.php on line 51
But the pdo & Pdo_mysql are installed with my php installation.
Whether this is the problem with the PHP installation on the server? Or there is a problem with PHP class i am using? or with the code above?
回答1:
This works for me:
<?php
if (isset( $_GET['id'])) {$id=$_GET["id"];}
exec("wkhtmltopdf http://webpage.php?id=" .$id . " pdf.pdf");
header('Content-type: application/pdf');
$file = readfile('pdf.pdf');
fclose($file);
?>
You don't need the $id, I could have replaced with www.google.com for testing.
来源:https://stackoverflow.com/questions/25780131/executing-wkhtmltopdf-via-php-throws-error