使用 phpword 将 docx 转换为 pdf
Converting docx to pdf with phpword
我想使用 phpword
将 docx 文件转换为 pdf
我的代码如下所示:
$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
最后我找到了安装 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";
我想使用 phpword
将 docx 文件转换为 pdf我的代码如下所示:
$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
最后我找到了安装 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";