我正在使用PHPWord
创建Microsoft Word报告。我基本上是从模板开始,然后填充字段并将其另存为单词报告。
我想将此报告转换为pdf文件。我尝试通过PHPWord加载生成的doc文件。但是,当我保存pdf文件时,格式全部丢失。
这是我正在使用的代码:
require_once DOC_ROOT . '/vendor/phpoffice/phpword/bootstrap.php';
$path_to_tcpdf = DOC_ROOT . '/includes/plugins/TCPDF/'; // C:\xampp\htdocs\clients\corporate\includes\plugins\TCPDF
\PhpOffice\PhpWord\Settings::setPdfRendererPath($path_to_tcpdf);
\PhpOffice\PhpWord\Settings::setPdfRendererName('TCPDF');
$report_file_doc = DOC_ROOT . '/reports/business_report_U72900GJ2002PTC040573_68628.docx';
$report_file_pdf = DOC_ROOT . '/reports/business_report_U72900GJ2002PTC040573_68628.pdf';
$phpWord = \PhpOffice\PhpWord\IOFactory::load($report_file_doc);
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord , 'PDF');
$xmlWriter->save($report_file_pdf);
知道会丢失什么吗?
谢谢
最佳答案
对于PHPWord v0.14
这是TCPDF
渲染器的示例(v0.13 中已弃用的):// Require composer autoloder.
require __DIR__.'/vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;
// Set PDF renderer.
// Make sure you have `tecnickcom/tcpdf` in your composer dependencies.
Settings::setPdfRendererName(Settings::PDF_RENDERER_TCPDF);
// Path to directory with tcpdf.php file.
// Rigth now `TCPDF` writer is depreacted. Consider to use `DomPDF` or `MPDF` instead.
Settings::setPdfRendererPath('vendor/tecnickcom/tcpdf');
$phpWord = IOFactory::load('document.docx', 'Word2007');
$phpWord->save('document.pdf', 'PDF');
这是DomPDF
渲染器的示例:// Require composer autoloder.
require __DIR__.'/vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;
// Make sure you have `dompdf/dompdf` in your composer dependencies.
Settings::setPdfRendererName(Settings::PDF_RENDERER_DOMPDF);
// Any writable directory here. It will be ignored.
Settings::setPdfRendererPath('.');
$phpWord = IOFactory::load('document.docx', 'Word2007');
$phpWord->save('document.pdf', 'PDF');