2014-09-11 46 views
0

我使用鏈接中提到的類 How do I get WKHTMLTOPDF to execute via PHP? 通過PHP使用wkhtmltopdf將html轉換爲pdf。通過php執行Wkhtmltopdf引發錯誤

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安裝在我的網絡服務器,我可以能夠通過命令行中的任何HTML頁面轉換爲PDF沒有錯誤日誌

enter image description here

當我試圖通過PHP將HTML轉換爲PDF類,我得到下面的異常

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

我也得到在錯誤日誌以下

[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 

但pdo & Pdo_mysql與我的PHP安裝一起安裝。

這是否是服務器上安裝PHP的問題?或者我正在使用的PHP類有問題?或與上面的代碼?

回答

0

這個工作對我來說:

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

?> 

您不需要$ ID,我可以用www.google.com取代了測試。

+0

您的代碼容易受殼層注入影響。你需要引用'「http://webpage.php?id =」。$ id。 「pdf.pdf」'與[escapeshellarg()](http://php.net/manual/en/function.escapeshellarg.php)。 – jimp 2015-09-18 15:08:18