2015-05-28 110 views
2

試圖創建使用PHP和包裝phpwkhtmltopdfphpwkhtmltopdf - 無法創建PDF - 失敗沒有錯誤消息

require '../vendor/autoload.php'; 
use mikehaertl\wkhtmlto\Pdf; 

// You can pass a filename, a HTML string or an URL to the constructor 
$pdf = new Pdf('http://www.google.co.uk'); 

// On some systems you may have to set the binary path. 
//$pdf->binary = 'C:\pdf'; 

$pdf->send('google.pdf'); 
if (!$pdf->send()) { 
    throw new Exception('Could not create PDF: '.$pdf->getError()); 
} 

但得到的錯誤

Fatal error: Uncaught exception 'Exception' with message 'Could not create PDF: Failed without error message: wkhtmltopdf "http://google.com" "C:\Windows\Temp\tmp4047.tmp.pdf"' in C:\wamp\www\site\ajax\createpdf.php on line 24 

enter image description here

又去到c PDF文件:\ windows \ temp並可以看到文件tmp4047.tmp.pdf - 但已損壞並且不會加載

已經運行在命令行wkhtmltopdf沒有問題 - PDF創建OK

wkhtmltopdf http://google.com google.pdf 

編輯 - 使用snappy代替 - 做工精細,有沒有人得到這個在AWS彈性魔豆的工作?任何教程? TQ

//snappy 
use Knp\Snappy\Pdf; 
$snappy = new Pdf('C://"Program Files"/wkhtmltopdf/bin/wkhtmltopdf.exe'); 
header('Content-Type: application/pdf'); 
header('Content-Disposition: attachment; filename="file.pdf"'); 
echo $snappy->getOutput('http://www.google.co.uk'); 

注 - Windows用戶提出報價一輪「程序文件」

+2

可能是一個權限問題。嘗試捕捉該異常並打印堆棧跟蹤,然後使用跟蹤瀏覽wkhtmltopdf的源代碼,並確切地查看它崩潰的位置。 –

+0

嗨 - 確定你是對的,但嘗試不同的包更快 – zima10101

回答

2

我面臨在Windows和Apache運行具有相同的錯誤信息同樣的問題,我解決了該問題短短的幾分鐘前,由在commandOptions上使用'bypass_shell' => 'true';,並指定指向安裝的wkhtmltopdf文件夾的二進制路徑,通常在Program Files中。

工作代碼:

$pdf = new Pdf([ 
        'commandOptions' => [ 
        'useExec' => false, 
        'escapeArgs' => false, 
        'procOptions' => array(
        // This will bypass the cmd.exe which seems to be recommended on Windows 
        'bypass_shell' => true, 
       // Also worth a try if you get unexplainable errors 
        'suppress_errors' => true, 
       ), 
       ], 
       ]); 
       $globalOptions = array(
       'no-outline', // Make Chrome not complain 
       // Default page options 
       'page-size' => 'Letter' 
       ); 

       $pdf->setOptions($globalOptions); 
       $pdf->addPage('http://www.google.com'); 
       $pdf->binary = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'; 
       if (!$pdf->send()) { 
      throw new Exception('Could not create PDF: '.$pdf->getError()); 
      } 

如果設置useExec命令參數設置爲true,那麼在你的二進制文件的路徑,你應該加上雙引號「程序文件」,否則你會得到錯誤C:\程序是沒有被識別爲內部或外部命令,可操作程序或批處理文件..向Mikehaertl發送他有用的文檔和wkhtmltopdf令人驚歎的工作。乾杯!

相關問題