Convert a word(DOC,DOCX) document to PFD using PHP or Javascript

Asked

Viewed 1,554 times

1

I wonder if there is the possibility of converting a Word file (.doc,.docx) to a . pdf file using Php or Javascript (or something similar). For example: the user uploads a. docx file that should be saved only in . pdf format --for this to do the conversion.

1 answer

1

phpgearbox/pdf

Requires: http://phantomjs.org

You can use the https://github.com/phpgearbox/pdf, shall install via Poser:

composer require gears/pdf:*

Then in his composer.json add:

"scripts":
{
    "post-install-cmd": ["PhantomInstaller\\Installer::installPhantomJS"],
    "post-update-cmd": ["PhantomInstaller\\Installer::installPhantomJS"]
}

Then after upload select so:

<?php
require_once 'vendor/autoload.php';

//Upload vem aqui

\Gears\Pdf::convert('<caminho do documento>/documento.docx', '<caminho aonde será salvo o pdf>/documento.pdf');

Phpoffice/Phpword

You can use the https://github.com/PHPOffice/PHPWord which supports various formats:

  • odt
  • RTF
  • Word2007
  • HTML
  • PDF

You must install via Poser:

composer require phpoffice/phpword

Example:

<?php
require_once 'vendor/autoload.php';

//Upload vem aqui

$source = '<caminho do documento>/documento.docx';

$phpWord = \PhpOffice\PhpWord\IOFactory::load($source);

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'PDF');
$objWriter->save('<caminho aonde será salvo o pdf>/documento.pdf');
  • According to the documentation, it is necessary to install libreoffice in the case of phpgearbox. That’s msm?

  • @Renanlopes libre-office-headless or unocon

Browser other questions tagged

You are not signed in. Login or sign up in order to post.