 |
การใช้ PHPWord แปลง docx เป็น PDF ระบบอ่านแต่ text ไม่แสดงผลการsaveอื่นๆ |
|
 |
|
|
 |
 |
|
ต้นฉบับ MSword docx

ผลที่ได้การจัดวางยังไม่เหมือนครับ

|
 |
 |
 |
 |
Date :
2023-04-07 23:39:08 |
By :
tonmasterbp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Code (PHP)
ขออธิบาย concept ใหม่ครับผม comment 3-4 มองผ่านไปก่อนผมอธิบายไม่ละเอียด
1. ผมต้องการ set docx template (ex-template.docx) ใส่ $doc->setValue ให้กับ docx และ save เป็น new_wordsetvalues.docx // ขั้นตอนนี้ทำงานปกติครับ
2. แต่ผมต้องการทำ new_wordsetvalues.docx ที่ได้จาก 1 มาเป็น PDF ซึ่งปัญหาเมื่อตอนเริ่มกระทู้ คือ style บน word มาไม่สมบูรณ์ สั่ง แต่ตัวหนังสือและรูปมาครบหมด จากต้นเรื่อง docx สั่งจัดขวา พอทำเป็น pdf มาอยู่ชิดซ้ายหมด และอื่นๆที่เป็นพวก header foot ตาม docx ก็ไม่มาด้วยครับ
ผมลองค้นๆในเนตดูน่าจะเป็นปัญหาเดียวกับ ISSUE ตามลิ้งครับ
https://stackoverflow.com/questions/54616086/no-styling-when-converting-docx-into-pdf-with-phpword/54660038#54660038
https://github.com/PHPOffice/PHPWord/issues/1139
3. จากที่มีพี่ comment2 เข้ามาช่วย ลองดูติดตั้ง mpdf ผมจัดการก่อนหน้านี้เรียบร้อยครับ ใช้ได้ทั้ง dopdf และ mpdf เลย
และผมลองใส่ $templateProcessor->getPhpWord() และ $options ตาม
ERROR ตามนี้ครับผม
PHP Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpWord\TemplateProcessor::getPhpWord()
จากด้านล่างเป็น code เต็มที่ลองแก้ไขตาม comment ที่ 2 ครับ
<?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";
?>
|
 |
 |
 |
 |
Date :
2023-04-08 13:16:55 |
By :
tonmasterbp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ISSEUE นี้เหมือนกันเลยครับ มีรูปประกอบด้วยครับ
https://github.com/PHPOffice/PHPWord/issues/2139
|
 |
 |
 |
 |
Date :
2023-04-08 14:07:43 |
By :
tonmasterbp |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|