PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

Convert pdf to Word Document #2560

Open richardsonoge opened 5 months ago

richardsonoge commented 5 months ago

I need to convert from PDF to Word(docx, doc and other word files too,,,), I proceeded with this code but it gave me a big error. I'll give you below my code and the errors I received in this Library. I would like to say is there any way you know of that I could use to convert the PDF to Word file efficiently? Any way you think I could use without going through APIs please tell me!

<?php
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\IOFactory as WordIOFactory;

/*********************** Convert a DOCM file to PDF ************************/

    // To enable pdf exporting with phpword
    class_alias( \Dompdf\Dompdf::class, 'DOMPDF');

    // Assuming dompdf_config.inc.php is in the same directory
    use \PhpOffice\PhpWord\Settings;
    Settings::setPdfRenderer( Settings::PDF_RENDERER_DOMPDF, __DIR__ );

    $phpWord = WordIOFactory::load('documents/ar-11_unlocked.pdf', 'PDF');

    $objWriter = WordIOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('convert/test.pdf');

/*********************** END Convert a DOCM file to PDF ************************/

Here the errors that I get:

[30-Jan-2024 19:05:48 America/New_York] PHP Fatal error:  Uncaught PhpOffice\PhpWord\Exception\Exception: "PDF" is not a valid Reader. in /opt/lampp/htdocs/filenigma/vendor/phpoffice/phpword/src/PhpWord/IOFactory.php:73
Stack trace:
#0 /opt/lampp/htdocs/filenigma/vendor/phpoffice/phpword/src/PhpWord/IOFactory.php(54): PhpOffice\PhpWord\IOFactory::createObject()
#1 /opt/lampp/htdocs/filenigma/vendor/phpoffice/phpword/src/PhpWord/IOFactory.php(87): PhpOffice\PhpWord\IOFactory::createReader()
#2 /opt/lampp/htdocs/filenigma/index.php(59): PhpOffice\PhpWord\IOFactory::load()
#3 {main}
  thrown in /opt/lampp/htdocs/filenigma/vendor/phpoffice/phpword/src/PhpWord/IOFactory.php on line 73

PLEASE! Any help will be appreciated and don't hesitate to comment.