2014-08-29 33 views
5

我用laravel我的應用程序和DOMPDF中發現:製作DOMPDF爲我的PDF作家phpWord

../vendor/dompdf/dompdf

我想實現的是將我.docx文件(Microsoft Word)中爲pdf什麼文件。 docx文件是由phpWord通過加載模板文件並替換值生成的。

這裏的片段:

// Get the absolute path of the template file 
$wordTemplatePath = $this->getDocumentTemplatePath('resignation.docx'); 

// Load the template file 
    $document = $this->phpWord->loadTemplate($wordTemplatePath); 

// This will be filled with data 
$wordData = [] 

.... Process to fill the data ..... 

// Replace value to actual word document 
    $this->setTemplateValues($wordData, $document); 


// Generate its filename 
    $file = $this->generateFileName('Retirement-Certificate.docx', $id); 

     // Fetch the absolute path to save the document 
     $filepath = $this->getSavePath($file); 

     // Save the word document 
     $document->saveAs($filepath); 

之後,將產生一個.docx文件。我也想製作一個PDF版本。所以我搜索,發現這個代碼片斷和在我的代碼添加:

   \PhpOffice\PhpWord\Settings::setPdfRendererPath('../vendor/dompdf/dompdf'); 
\PhpOffice\PhpWord\Settings::setPdfRendererName('DOMPDF'); 

//Load temp file 
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath); 

//Save it 
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF'); 
$xmlWriter->save('result.pdf'); 

但我得到這個錯誤:

{"error": 
{"type":"PhpOffice\\PhpWord\\Exception\\Exception","message":"PDF rendering library or library path has not been defined.", 
"file":"C:\\xampp\\htdocs\\vagrant\\vendor\\phpoffice\\phpword\\src\\PhpWord\\Writer\\PDF.php", 
"line":49}} 

我怎麼會讓DOMPDF爲我的PDF作家PHPWord?我無法找到任何其他選項,所以我在這裏問。我希望你能幫助我。謝謝!

+0

你試過dompdf庫的絕對路徑嗎? – BrianS 2014-09-12 03:04:24

回答

2

您必須將其設置爲絕對路徑。 把它放到你的bootstrap.php文件中。

define('PHPWORD_BASE_DIR', realpath(__DIR__)); 

這是您的來電:

$domPdfPath = realpath(PHPWORD_BASE_DIR . '/../vendor/dompdf/dompdf'); 
\PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath); 
\PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF'); 

//Load temp file 
$phpWord = \PhpOffice\PhpWord\IOFactory::load($filepath); 

//Save it 
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF'); 
$xmlWriter->save('result.pdf'); 
3

我覺得@Franz Holzingers的答案是正確的,只是今天我安裝了PHP字的版本需要渲染的名字是DOMPDF點。如果全部大寫,則該方法失敗,如圖所示。另外,我安裝的日期版本使用配置文件,以便顯式調用能夠正常工作,如果將它們放置在配置文件加載後,它們會在配置加載之前放置它們而被覆蓋。我建議一個更簡單的答案是找到位於phpword安裝目錄中「src」上方目錄中的文件「phpword.ini.dist」;編輯DomPDF目錄的行以顯示您安裝DomPDF的位置,然後將該文件保存在同一目錄下但名稱爲phpword.ini。現在配置加載器將執行Frank爲您記錄的調用。如果您錯誤地輸入了DomPDF的路徑,您將不會收到警告,但您可以使用方法\ PhpOffice \ PhpWord \ Settings :: getPdfRendererPath(domPdfPath)來測試它是否正確安裝。如果該方法的返回值爲空,則意味着您在配置文件中指定的路徑名​​不存在。請注意,加載程序不檢查路徑是否包含DomPDF,只是路徑存在。

值得注意的是,在這個時候,phpword支持三個PDF渲染引擎。通過提供安裝路徑並將PDF渲染器的名稱設置爲下列其中一個值(區分大小寫):DomPDF,TCPDF或MPDF,可以在上面提到的ini文件中配置這三者中的任何一個。建議使用DomPDF和默認設置,但在代碼中出現時,如果您有其他人的投資或偏好,可以將其與phpword一起使用。

+1

非常感謝您提供關於「DomPDF」名稱的注意事項 - 我已經花了好幾個小時來處理這個路徑錯誤,直到閱讀了您的答案並修復了拼寫DOMPDF - > DomPDF,現在它可以工作! – DaneSoul 2015-02-25 15:41:35

+0

\ PhpOffice \ PhpWord \ Settings具有以下常量:PDF_RENDERER_DOMPDF,PDF_RENDERER_TCPDF,PDF_RENDERER_MPDF可與\ PhpOffice \ PhpWord \ Settings :: setPdfRendererName一起使用。如果您使用的是Composer,請在啓動腳本中添加以下行:define('DOMPDF_ENABLE_AUTOLOAD',false); – Marcos 2016-03-10 17:11:43