skip to Main Content

Here is the code I used to convert my docx files to PDF:

<?php
require_once 'vendor/autoload.php';
use PhpOfficePhpWordIOFactory as WordIOFactory;
use PhpOfficePhpWordSettings;

// 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 = WordIOFactory::load('test/graph.docx', 'Word2007');
$phpWord->save('graph.pdf', 'PDF');

This code worked, but the problem was when it converted the file to PDF. The colors of the graphics were all lost and I feel like even the format was lost.

Here is the PDF file I had:

This picture shows the PDF file I had.

Here is the result after converting the DOCX file to a PDF file:

This picture shows the result after converting the DOCX file to a PDF file.

As you can see. The PDF file has lost the colors of the graphics. Who then, I would like to know from you. Do you have an idea to at least take into account the colors of the graphics? I have done a lot of research but have not found anything concrete. Can you help me please?

Well, I am doing my best to explain my problem. I hope I will get some nice answers. 🙂

Thank you for helping me. 🙂

2

Answers


  1. Microsoft Word has many proprietary features that cannot readily be carried over from conversion from DocX to simpler Rich Formats. This can be seen in Microsoft’s own applications too. Very simply open a DocX in the native Write.exe here we see complex styles are unsupported.

    enter image description here

    It is thus not surprising that the only App that can faithfully convert Word Documents to PDF is MS Word export to PDF (Similar when converting PDF "To" DocX), and many converter programs/scripts can invoke an installed MS Office by calling Microsoft "Interop" DDLs.

    Adobe Online Converter uses copies of MS Word as can be seen when using their conversion apps:-
    enter image description here

    Similar with other brand leading converters here Aspose clearly use MS Word

    File: graph (1).pdf
    ...
    Application: Microsoft Office Word
    PDF Producer: Aspose.Words for .NET 22.11.0
    PDF Version: 1.7
    File Size: 7.07 KB (7,237 Bytes)
    Number of Pages: 1
    Page Size: 21.0 x 29.7 cm (A4)
    

    Bottom line for docX fidelity you need a running copy of MS word

    Next best

    You can sometimes get close (but not always) using Libre Office or a variant so that chart works well using Server conversion. However, note the significant increase in size (over 10x bigger) to embed different MS aspects.

    File: graph (2).pdf
    ...
    Application: Writer
    PDF Producer: ConvertAPI
    PDF Version: 1.7
    File Size: 93.37 KB (95,607 Bytes)
    Number of Pages: 1
    Page Size: 21.0 x 29.7 cm (A4)
    

    You can better that (only 5 times bigger) by using a batch with local current 7.4

    libreofficeprogram>soffice --convert-to pdf *.docx

    "The program script" as shown above, will start soffiice in the libraryprogram folder, to convert every local docx into a local pdf.
    If you need to customise, i.e. where you start it as a CWD, or run one by one files elsewhere, then that will be specific to your operating system and workflow, mine is just run one single line (or a few) as a full blown suite of commands, = power via one line simplicity.
    There are 100’s of variants of php aids and many to run and install libre office so try searching say github.com/search?q=php+libreoffice, a good example is the one shown by @zeikman

    $converter->convertTo('output-file.pdf'); //generates pdf file in same directory as test-file.docx

    enter image description here

    Login or Signup to reply.
  2. I believe the OfficeConverter library will definitely helps you in what you’re going to achieve, which it is actually utilising the libreoffice way mentioned by KJ.

    I downloaded your graph.docx file and tested, the result should be what you’re looking for. I have a demo site for you to test out, feel free to try it out, it is my open source project.

    Here the link to the site PhpOfficeTemplate.

    1. Do remember to Enable the Office Converter options.
    2. Then select your docx file in the Template.

    https://i.stack.imgur.com/fTHik.png

    Hope it helps !

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search