Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > PHP > PHP Forum > การใช้ PHPWord แปลง docx เป็น PDF ระบบอ่านแต่ text ไม่แสดงผลการsaveอื่นๆ



 

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

 



Topic : 136992



โพสกระทู้ ( 288 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Hi5 Blogger



Code (PHP)

การใช้ PHPWord แปลง docx เป็น PDF ระบบอ่านแต่ text ไม่แสดงผลการsaveอื่นๆ
ประเด็นอยู่ที่ตอนจะ save pdf ครับ ต้นฉบับ docx มีการจัดหน้า ใส่ header footer ไว้อย่างดี แต่พอ save ผลออกมาแล้ว
ระบบดึงแต่ textกับรูปภาพขาวดำ ออกมาทำเป็น pdf ครับ

ลองใช้ทั้ง PDF_RENDERER_MPDF และ PDF_RENDERER_DOMPDF แล้วครับ

require_once __DIR__ . '/vendor/autoload.php';

use \PhpOffice\PhpWord\TemplateProcessor,
    \PhpOffice\PhpWord\Shared\Html,
    \PhpOffice\PhpWord\PhpWord,
    \PhpOffice\PhpWord\IOFactory,
    \PhpOffice\PhpWord\Settings;


// ขั้นตอน save pdf
   Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF); //PDF_RENDERER_DOMPDF PDF_RENDERER_TCPDF PDF_RENDERER_MPDF
    Settings::setPdfRendererPath('.');
    $phpWord = IOFactory::load($pathToOutputFile, 'Word2007');
    $phpWord->save('export-pdf\P_'.$filenameexport.'.pdf', 'PDF');






Tag : PHP







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2023-04-07 23:29:16 By : tonmasterbp View : 834 Reply : 6
 

 

No. 1



โพสกระทู้ ( 288 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Hi5 Blogger

ต้นฉบับ MSword docx
word-t


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






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-07 23:39:08 By : tonmasterbp
 


 
ถ้าใช้ mpdf renderer เช็คว่าได้ติดตั้ง mpdf ใน project แล้วหรือยัง...ถ้ายังก้ทำให้เรียบร้อย

Code
composer require mpdf/mpdf



ลองใช้ TemplateProcessor และกำหนด options ใหม่

Code (PHP)
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

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-08 10:42:09 By : 009
 

 

No. 3



โพสกระทู้ ( 288 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Hi5 Blogger

ตอบความคิดเห็นที่ : 2 เขียนโดย : 009 เมื่อวันที่ 2023-04-08 10:42:09
รายละเอียดของการตอบ ::

ติดตั้ง 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




แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-08 12:19:31 By : tonmasterbp
 


 

No. 4



โพสกระทู้ ( 288 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Hi5 Blogger

ตอบความคิดเห็นที่ : 3 เขียนโดย : tonmasterbp เมื่อวันที่ 2023-04-08 12:19:31
รายละเอียดของการตอบ ::

จาก comment2 ผมใส่ตัวแปรผิดไปตัวนึง

ตอนนี้ได้แล้วนะครับไม่ error แต่ style ไม่มาเหมือนเดิมครับ
จาก ISSUE นี้น่าจะเป็นปัญหาเดียวกันครับ
https://github.com/PHPOffice/PHPWord/issues/1139



แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-08 12:26:01 By : tonmasterbp
 


 

No. 5



โพสกระทู้ ( 288 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Hi5 Blogger

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
 


 

No. 6



โพสกระทู้ ( 288 )
บทความ ( 0 )



สถานะออฟไลน์
Twitter Facebook Hi5 Blogger

ISSEUE นี้เหมือนกันเลยครับ มีรูปประกอบด้วยครับ
https://github.com/PHPOffice/PHPWord/issues/2139
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2023-04-08 14:07:43 By : tonmasterbp
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : การใช้ PHPWord แปลง docx เป็น PDF ระบบอ่านแต่ text ไม่แสดงผลการsaveอื่นๆ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่