How to convert the doc file to docx correctly in php?

Member

by dana , in category: PHP General , a year ago

How to convert the doc file to docx correctly in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by lizzie , a year ago

@dana 

To convert a .doc file to .docx format in PHP, you can use the PHPWord library. Here is a step-by-step guide to achieving this:

  1. Install PHPWord library using Composer. Run the following command in your project directory:
1
composer require phpoffice/phpword


  1. Create a PHP script and include the necessary PHPWord classes:
1
2
3
4
require_once 'vendor/autoload.php';

use PhpOfficePhpWordPhpWord;
use PhpOfficePhpWordIOFactory;


  1. Load the .doc file using the IOFactory class:
1
2
$docFilePath = 'path/to/input.doc';
$phpWord = IOFactory::load($docFilePath);


  1. Save the loaded .doc file as .docx:
1
2
$docxFilePath = 'path/to/output.docx';
$phpWord->save($docxFilePath, 'Word2007');


Note: Make sure that the PHP process has read and write permissions for the input and output files' directories.


That's it! The .doc file will now be converted and saved as .docx format.