2017-03-24 155 views
0

我想用轉換一個的docx文件爲PDF phpword轉換的docx爲PDF phpword

我的代碼看起來是這樣的:

$FilePath = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".docx"; 
$FilePathPdf = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".pdf"; 

//DOCX TO PDF 
require_once APPPATH.'third_party/phpword/bootstrap.php'; 

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

$phpWord = new \PhpOffice\PhpWord\PhpWord(); 

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

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

但我產生一個空的PDF文件:LINK TO FILE

路徑是正確的.docx有內容

我試着用tcpdf渲染:

$rendererLibraryPath = PHPWORD_BASE_DIR . '/vendor/tecnickcom/tcpdf'; 
\PhpOffice\PhpWord\Settings::setPdfRendererPath($rendererLibraryPath); 
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF'); 

和我的pdf文件有2頁,而內容:LINK TO 2ND FILE

回答

1

終於讓我找到一個免費的替代解決方案安裝LibreOffice的:

$FilePath = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".docx"; 
$FilePathPdf = APPPATH."media/Documentos/Facturas/Factura ".$FacturaId.".pdf"; 

require_once APPPATH.'third_party/phpword/bootstrap.php'; 
$template = new \PhpOffice\PhpWord\TemplateProcessor(APPPATH.'media/Plantillas/Factura/Factura.docx'); 

foreach($data as $key => $value){ 
    $template->setValue($key, $value); 
} 

$template->saveAs($FilePath); 

shell_exec($this->CI->config->item('libreoffice_exec')." --headless --convert-to pdf --outdir \"".$Path."\" \"$FilePath\""); 

調用soffice.exe CLI與此參數:

soffice.exe --headless --convert-to pdf --outdir "C:/media/Documentos/Facturas/pdf" "C:/media/Documentos/Facturas/Factura1.docx";