phpgearbox/pdf

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

mxmrlt opened this issue · 8 comments

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

$pdf = Pdf::convert().

How to do?

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.

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
...

Have you got the latest version?

This I believe to be fixed, see: 4368b58

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.

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 ;-)

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.

It works.

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