phpgearbox / pdf

A PDF builder using HTML or DOCX templates.
MIT License
180 stars 112 forks source link

How to change default LibreOffice binary path? Windows OS for instance. #1

Closed mxmrlt closed 9 years ago

mxmrlt commented 9 years ago

I can't find how to change the default LibreOffice binary path when I do:

$pdf = Pdf::convert().

How to do?

brad-jones commented 9 years ago

Firstly this has not been tested on a Windows platform at all but by all means give it a shot and hopefully it works for you.

Currently there isn't a nice direct way to manage the configuration of the backend classes from the main Gears\Pdf class. It's sort of half done at the moment.

To do what you want here is an example:

Gears\Pdf::convert('C:\Path\To\Document.docx', 'C:\Path\To\Document.pdf',
[
    'converter' => function()
    {
        return new Gears\Pdf\Docx\Converter\LibreOffice
        ([
            'binary' => 'C:\Program Files\Libre Office\etc...'
        ]);
    }
]);

NOTE: I have used windows paths as examples for you but like I say this has not been tested.

mxmrlt commented 9 years ago

Thanks for your response.

Unfortunately here's the error I get while using your piece of code:

RuntimeException in Pdf.php line 301:
Backend Class not created yet!
in Pdf.php line 301
at Pdf->__set('injectConverter', object(Closure)) in Container.php line 234
at Container->offsetSet('converter', object(Closure)) in Container.php line 88
at Container->__construct(array('converter' => object(Closure))) in Pdf.php line 99
at Pdf->__construct('C:\Developpement\Programmes\www\intranet\storage\app\QLFN EN 36 IR 02 - Convention PSA_1.docx', array('converter' => object(Closure))) in Pdf.php line 161
at Pdf::convert('C:\Developpement\Programmes\www\intranet\storage\app\QLFN EN 36 IR 02 - Convention PSA_1.docx', 'C:\Developpement\Programmes\www\intranet\storage\app\QLFN EN 36 IR 02 - Convention PSA_1.pdf', array('converter' => object(Closure))) in TemplateController.php line 69
at TemplateController->index()
at call_user_func_array(array(object(TemplateController), 'index'), array()) in Controller.php line 246
...
brad-jones commented 9 years ago

Have you got the latest version?

This I believe to be fixed, see: https://github.com/phpgearbox/pdf/commit/4368b58d5e14f86e34efd6c9ba16830c0a749438

brad-jones commented 9 years ago

To confirm this is fixed in the master branch but I have not yet tagged a new release. Please try with the master branch, if it works for you I will tag a new release.

mxmrlt commented 9 years ago

Yeah I've switch to dev-master.

But here's what I needed to do to make it work:

$path = 'ANY_PATH';
Pdf::convert("$file.docx", "$file.pdf",
[
    'converter' => function() use ($path)
    {
        return new LibreOffice
        ([
            'binary' => 'C:\Servers\LibreOffice\App\libreoffice\program\soffice.exe',
            'output' => $path
        ]);
    }
]);

and in the LibreOffice class:

// Build the cmd to run
$cmd =
    'start /wait ' .
    $this->binary.' '.
    '--headless '.
    '--convert-to pdf:writer_pdf_Export '.
    '--outdir "'.$this->output.'" '.
    '"'.$docx->getPathname().'"'
;

Parameter '-env:UserInstallation=' didn't work for me.

of course:

exec('rm -rf '.$this->profile);

does'nt work under Windows ;-)

brad-jones commented 9 years ago

Oh well that pretty easy to fix up then.

Obviously we don't need to remove the temp profile if we don't create one :)

Out of interest can you try setting the --outdir path to say:

C:\Windows\Temp\gears-pdf-libreoffice\generated

If that works and doesn't require the user to have any elevated privileges then I might just create a simple switch statement to build the different versions of the $cmd.

mxmrlt commented 9 years ago

It works.

pascaldls commented 6 years ago

hi seems that 'start /wait ' . is esssential for build line 122 LibreOffice.php please add it on master