Here is the code I used to convert my docx files to PDF:
<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory as WordIOFactory;
use PhpOffice\PhpWord\Settings;
// 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:
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:-
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
libreoffice\program>soffice --convert-to pdf *.docx
CodePudding user response:
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.
- Do remember to Enable the Office Converter options.
- Then select your docx file in the Template.
Here the quick glance of the result
Hope it helps !