How to convert Word document into PDF using PHPWord

How to convert Word document into PDF using PHPWord

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to convert Word document into PDF using PHPWord

When working with dynamic reports in PHP, it is common to generate a .docx file by populating a template and then attempt to render that same object as a PDF. The standard approach involves loading the Word document via IOFactory, setting a PDF renderer path (often TCPDF), and saving the result. However, if you encounter formatting loss—where fonts disappear, tables collapse, or images resize incorrectly—the issue usually stems from how PHPWord handles legacy data versus native creation.

The core problem in your specific scenario is that PHPWord's built-in PDF conversion logic is not a true "rendering" engine in the way users expect. When you load an existing .docx file and ask PHPWord to convert it, the library attempts to reconstruct the document structure from its internal XML representation. During this reconstruction, complex styling information, embedded fonts, or specific layout constraints defined in the original Word template often get stripped or approximated using default fallbacks. This is why your generated PDF looks different from the source Word file; PHPWord is not preserving the exact visual fidelity of the input document when using its internal converter.

To achieve high-fidelity conversion where the PDF matches the Word layout exactly, you should avoid relying solely on PHPWord's internal PDF writer for existing files. Instead, consider exporting the populated Word document as a native HTML file first. PHPWord supports saving documents to HTML format while preserving CSS styles and inline formatting much better than its direct-to-PDF converter does. Once you have the HTML output, you can pipe that into a dedicated PDF generation library like DOMPDF or TCPDF. These libraries are designed to interpret standard HTML/CSS and render them to a vector-based PDF without losing the structural details that PHPWord might discard during its internal reconstruction phase.

If you must stick with TCPDF as your renderer, ensure that the IOFactory::createWriter method is explicitly told to use the TCPDF writer class rather than relying on the generic 'PDF' alias which might default to an internal converter depending on your version. You can do this by constructing the writer instance directly:

$writer = new \PhpOffice\PhpWord\Writer\PDF_TCPDF($phpWord, $path_to_tcpdf);
$writer->save($report_file_pdf);

Alternatively, using the HTML intermediate step is often more robust for complex reports. Here is how you might adjust your workflow:

  1. Save the populated Word object as an HTML file using \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML').
  2. Use a separate script to read that HTML and convert it to PDF using TCPDF's Html2Ps class or similar utilities.

This separation of concerns ensures that PHPWord handles the document manipulation while specialized libraries handle the visual rendering. This approach minimizes the risk of data loss during the conversion process. If you are integrating this into a Laravel application, remember that file paths like $DOC_ROOT need to be resolved relative to your public directory or storage link depending on your deployment configuration. Always ensure the TCPDF library is loaded from an accessible path and that permissions allow the script to write the output PDF file without issues.

Our network