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

Member

by aubrey , in category: PHP General , 10 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 10 months ago

@aubrey 

To convert a DOC file to DOCX format correctly in PHP, you can use the PHPWord library. Here is a step-by-step guide on how to do that:


Step 1: Install the PHPWord library via Composer by running the following command:

1
composer require phpoffice/phpword


Step 2: Create a PHP script with the following code to convert the DOC file to DOCX format:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php

require 'vendor/autoload.php';

use PhpOfficePhpWordIOFactory;

// Load the DOC file
$docPath = 'path/to/input.doc';
$phpWord = IOFactory::load($docPath);

// Save the PHPWord object as a DOCX file
$docxPath = 'path/to/output.docx';
$phpWord->save($docxPath, 'Word2007');

echo 'DOC file converted to DOCX successfully!';
?>


Replace path/to/input.doc and path/to/output.docx with the actual file paths for the input DOC file and output DOCX file.


Step 3: Run the PHP script to convert the DOC file to DOCX format.


After running the script, the DOC file should be successfully converted to the DOCX format using the PHPWord library in PHP.