การใช้ PHPWord แปลง docx เป็น PDF ระบบอ่านแต่ text ไม่แสดงผลการsaveอื่นๆ
ประเด็นอยู่ที่ตอนจะ save pdf ครับ ต้นฉบับ docx มีการจัดหน้า ใส่ header footer ไว้อย่างดี แต่พอ save ผลออกมาแล้ว
ระบบดึงแต่ textกับรูปภาพขาวดำ ออกมาทำเป็น pdf ครับ
require_once __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;
// Load the Word document
$templateProcessor = new TemplateProcessor($pathToOutputFile); // Replace $pathToOutputFile with the path to your Word document
$phpWord = $templateProcessor->getPhpWord();
// Set PDF renderer options
Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF); // Set the PDF renderer to MPDF
Settings::setPdfRendererPath('./vendor/mpdf/mpdf'); // Set the path to MPDF library
// Save to PDF with options
$options = [
'FileName' => 'export-pdf/P_' . $filenameexport . '.pdf', // Set the output file name and path
'format' => 'PDF', // Set the output format to PDF
'preserveFormatting' => true, // Preserve original formatting
'compress' => false, // Disable compression
'tempDir' => './tmp', // Set temporary directory for MPDF
];
IOFactory::createWriter($phpWord, 'PDF')->save($options); // Save the Word document to PDF with specified options
ติดตั้ง MPDF แล้วครับ แต่ลองใช้ดูติด
Call to undefined method PhpOffice\PhpWord\TemplateProcessor::getPhpWord()
ครับ
Code (PHP)
require_once __DIR__ . '/vendor/autoload.php';
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use PhpOffice\PhpWord\IOFactory;
use PhpOffice\PhpWord\Settings;
$pathToOutputFile = "upload-word/template-example.docx";
// Load the Word document
$templateProcessor = new TemplateProcessor($pathToOutputFile); // Replace $pathToOutputFile with the path to your Word document
$phpWord = $templateProcessor->getPhpWord();
// Set PDF renderer options
Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF); // Set the PDF renderer to MPDF
Settings::setPdfRendererPath('./vendor/mpdf/mpdf'); // Set the path to MPDF library
// Save to PDF with options
$options = [
'FileName' => 'export-pdf/P_' . $filenameexport . '.pdf', // Set the output file name and path
'format' => 'PDF', // Set the output format to PDF
'preserveFormatting' => true, // Preserve original formatting
'compress' => false, // Disable compression
'tempDir' => './tmp', // Set temporary directory for MPDF
];
IOFactory::createWriter($phpWord, 'PDF')->save($options); // Save the Word document to PDF with specified options
<?php
require_once __DIR__ . '/vendor/autoload.php';
use \PhpOffice\PhpWord\TemplateProcessor,
\PhpOffice\PhpWord\Shared\Html,
\PhpOffice\PhpWord\PhpWord,
\PhpOffice\PhpWord\IOFactory,
\PhpOffice\PhpWord\Settings;
// set template word
$doc = new TemplateProcessor('ex-template.docx');
// set values
$doc->setValue('NUM', "BKK/DDDDD/001");
$doc->setImageValue('SIG', 'sig.jpg');
// save word
$doc->saveAs('new_wordsetvalues.docx');
// Load the Word document
$templateProcessor = new TemplateProcessor('new_wordsetvalues.docx');
$phpWord = $templateProcessor->getPhpWord();
// Set PDF renderer options
Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF);
Settings::setPdfRendererPath('./vendor/mpdf/mpdf');
// Save to PDF with options
$options = [
'FileName' => 'P_new_pdfexport.pdf',
'format' => 'PDF',
'preserveFormatting' => true,
'compress' => false,
'tempDir' => './tmp',
];
IOFactory::createWriter($phpWord, 'PDF')->save($options);
echo "save complete";
?>