edit docx file and save to PDF in php

3

I have a docx file, my goal is to be able to edit the docx, edit a specific string (do something like str_replace), and finally save the file to PDF. The docx as is a zipped xml file, I'm thinking of editing from there. Or would you advise another, simpler solution?

Now to save docx to PDF is harder, I'm not finding any free library that will make it fit for me.

Is there an easier way to do this process? Is there any library that does this?

    
asked by anonymous 10.09.2015 / 17:02

2 answers

1

Use the PHPDOCX library = link and link , which has the free and paid version as well.

Open the docx file:

require_once 'phpdocx_pro/classes/TransformDoc.inc';
    $docx = new TransformDoc();
    $docx->setStrFile('document.docx');
    $docx->generateXHTML();
    $html = $docx->getStrXHTML();

And to export to pdf uses:

$docx->generatePDF();
    
10.09.2015 / 17:53
1

Regarding the answer from @henriquedpereira , PHPdocX has two versions a licensed LGPL and Pro, the The first one is free (LGPL license attention) and the last one is paid. PHPdocX free allows you to dynamically generate docx files with simple formatting options such as lists, page and table numbering, watermarks are not inserted in the test period or limit on the amount of documents you can generate. If the watermark is not an inconvenience it already meets your need.

However, there is a great library for accessing Office files, the PHPOffice / PHPWord

To install add to require: of your composer.json ( dompdf is also required to write the PDF ):

{
    "require": {
        "dompdf/dompdf": "0.6.*",
        "phpoffice/phpword": "v0.13.*"
    }
}

And then run in terminal or cmd:

cd c:\wamp\www\projeto
composer update

To convert a Word to PDF you only need to import to libraries (you will need to use composer ), follow an example (source: link ):

<?php

require_once 'bootstrap.php';

use PhpOffice\PhpWord\Settings;
use PhpOffice\PhpWord\IOFactory;

Settings::setPdfRendererPath('vendor/dompdf/dompdf');
Settings::setPdfRendererName('DomPDF');

$temp = IOFactory::load('pasta/doc.docx');

$xmlWriter = IOFactory::createWriter($temp , 'PDF');
$xmlWriter->save('pasta/doc.pdf', true);

Unfortunately in 1.3.0 I have finally removed the custom autoloader, which allowed me to install without composer the library, I know it seems a difficult situation, I understand that composer seems complicated, but in fact it is easier to structure a project than manually, outside if you need to update something, add or remove composer do it for you.

27.11.2015 / 21:21