PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.23k stars 2.69k forks source link

Word Docx. converting to pdf losing data, images, and structure layout. #2069

Open DulanNB opened 3 years ago

DulanNB commented 3 years ago

public function convertWordToPDF() { $domPdfPath = base_path('vendor/dompdf/dompdf'); \PhpOffice\PhpWord\Settings::setPdfRendererPath($domPdfPath); \PhpOffice\PhpWord\Settings::setPdfRendererName('DomPDF');

    $Content = \PhpOffice\PhpWord\IOFactory::load(public_path('test.docx'));
    dd($Content);
    $PDFWriter = \PhpOffice\PhpWord\IOFactory::createWriter($Content,'PDF');
    $PDFWriter->save(public_path('new-result.pdf'));
    echo 'File has been successfully converted';

}

Description

This function is converting to pdf losing data, images, and structure layout.

4unkur commented 3 years ago

@DulanNB I did some research on this topic last year and found out that it is almost impossible to convert DOCX to PDF without corrupting the layout.
There is a possibility to convert a DOCX file to PDF using LibreOffice CLI - but the result will be like if you did it in the LibreOffice (which might be suitable for you, 90% layout will be exactly the same)
But in my case, I needed PDF that would look 100% exactly the same as DOCX file, so I ended up using third party service CloudConvert (I am sure there are other variants too) Hope this helps

mocharasa-dev commented 3 years ago

@DulanNB I did some research on this topic last year and found out that it is almost impossible to convert DOCX to PDF without corrupting the layout. There is a possibility to convert a DOCX file to PDF using LibreOffice CLI - but the result will be like if you did it in the LibreOffice (which might be suitable for you, 90% layout will be exactly the same) But in my case, I needed PDF that would look 100% exactly the same as DOCX file, so I ended up using third party service CloudConvert (I am sure there are other variants too) Hope this helps

thank's this help me a lot. i developing app that must to convert document such as word to pdf, try many ways and always fail. desperate can't find the answers finally theres an option to make it trough.

your answer really helpfull

technilogics commented 1 year ago

@4unkur Your Research saved me, i spent several hours in trying different PDF Libraries, and no one work for DOCX to PDF conversion, Your suggestion for CloudCovert help me a lot, They have very good API. Thanks for Sharing.