Issue
using debian 9.5, python 3.5, libreoffice 5.2, x86_64 arch.
I have a word file (docx) of 22 pages, which contains several charts.
When run from terminal using bash, the following command works correctly i.e. generates a pdf file of 22 pages:
/usr/bin/libreoffice --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx
output:
convert /tmp/docx5/output.docx -> /tmp/docx5//output.pdf using filter : writer_pdf_Export
The issue is the following: the same external command executed from python using subprocess.run produces a pdf file of only one page, instead of 22 pages, with no error message.
No other instances of libreoffice are running.
cmd = '/usr/bin/libreoffice --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx'
print(subprocess.run(cmd, shell=True, check=True))
this is the output of this python script:
convert /tmp/docx5/output.docx -> /tmp/docx5//output.pdf using filter : writer_pdf_Export
CompletedProcess(args='/usr/bin/libreoffice --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx', returncode=0)
Apparently, pdf generation was successfull, but only the first page of docx file was converted.
It seems that the generation of the pdf terminates when libreoffice, started from python, encounters the first chart.
Does libreoffice require the java runtime for generating pdf?
Could there be an issue with headless operations of libreoffice?
Any hint?
Update:
added the 'env:UserInstallation' option, when running from python the modified script:
cmd = '/usr/bin/libreoffice -env:UserInstallation=file:///home/marco/ --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx'
print(subprocess.run(cmd, shell=True, check=True))
The output is the following, now it contains a warning about not finding a java runtime environment:
javaldx: Could not find a Java Runtime Environment!
Warning: failed to read path from javaldx
convert /tmp/docx5/output.docx -> /tmp/docx5//output.pdf using filter : writer_pdf_Export
CompletedProcess(args='/usr/bin/libreoffice -env:UserInstallation=file:///home/marco/ --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx', returncode=0)
Any idea on how to specify thorugh command line parameters where libreoffice can find the java runtime environment it needs?
Solution
I found a solution, although it is not clear to me the technical reason:
this WORKS (complete pdf generation using libreoffice of docx file with charts):
PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-10-oracle/bin:/usr/lib/jvm/java-10-oracle/db/bin /usr/bin/libreoffice -env:UserInstallation=file:///tmp/docx5/ --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx
this DOES NOT WORK (partial pdf generation using libreoffice of docx file with charts):
PATH=/home/marco/venv/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/lib/jvm/java-10-oracle/bin:/usr/lib/jvm/java-10-oracle/db/bin /usr/bin/libreoffice -env:UserInstallation=file:///tmp/docx5/ --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx
It seems that python virtualenv causes some sort of conflict with libreoffice. I used strace but found nothing useful.
So the solution for my case is to remove the virtualenv path from PATH environment variable when calling libreoffice from python, and this can be achieved by deactivating virtualenv:
[email protected]:~$ source venv/bin/activate
...
(venv) [email protected]:~$ deactivate && /usr/bin/libreoffice -env:UserInstallation=file:///tmp/docx5/ --headless --convert-to pdf --outdir /tmp/docx5/ /tmp/docx5/output.docx
Answered By - mrtexaz Answer Checked By - Gilberto Lyons (WPSolving Admin)