Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

pdf - Executing Wkhtmltopdf via php throws error

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

enter image description here

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...