edit docx file and save to PDF in php

Asked

Viewed 1,665 times

3

I have a docx file, my goal is to edit docx, edit a specific string (do something like str_replace), and finally save the file in PDF. docx as an xml zipped file, I’m thinking of editing from there. Or advise another solution but simple?

Now to save from docx to PDF is more difficult, I’m not finding any free library that will do it to me in conditions.

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

2 answers

1

On the response of @henriquedpereira, Phpdocx has two versions a with LGPL license and Pro, the first is free (attention LGPL license) and the last yes is paid. Phpdocx free allows you to dynamically generate docx files with simple formatting options such as lists, page numbering and tables, watermarks are not inserted in the test period or limit the amount of documents you can generate. If the watermark is not a nuisance already meets your need.

However there is a great library to access Office files, is the Phpoffice/Phpword

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

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

And then run on the terminal or cmd:

cd c:\wamp\www\projeto
composer update

To convert a Word to PDF you only need to import the libraries (you will need to use composer), follows an example (source: https://github.com/PHPOffice/PHPWord):

<?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 the version 1.3.0 was definitely removed the custom autoloader, which allowed installing without composer the library, I know it seems a difficult situation, I understand that the composer seems complicated, but in fact it is easier to structure a project than manually, besides that if you need to update something, add or remove the composer does it for you.

1

  • but the PHPDOCX library is paid for. I was interested in a free

  • @pc_oc is not paid, has two versions a with LGPL and Pro license, this last yes is paid, as Henrique said (that has the paid and free version).

  • @Guillhermenascimento but the free version must be limited

  • @pc_oc then only the phpoffice/phpword :)

  • @Guilhermenascimento I at the time ended up doing everything in html and converting to PDF since it was the only solution I found at the time. It was about 40 pages I had to..

Browser other questions tagged

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