File can be converted to PDF using GUI but not with cmd

WSL2 Ubuntu 24.04

LibreOffice 24.2.7.2 ee3885777aa7032db5a9b65deec9457448a91162

There are some Word files, if I open them with LibreOffice GUI, click “Export Directly as PDF”, and specify output directory, the conversion works just fine.

If I try to do the exactly thing with cmd, aka.

import subprocess

cmd = [
    'soffice',
    '--headless',
    '--convert-to', 
    'pdf',
    '--outdir', str(file.parent),
    str(file)
]
process = subprocess.run(cmd)

This will stuck at the conversion, where ~.lock file and .tmp file both visible but latter not accessible (Permission denied).

FYI, I installed LibreOffice through cmd and manually installed GUI myself. I added GUI just to test if the file itself is corrupt/broken and ideally I don’t want to use GUI by all means. I guess reinstalling LibreOffice with GUI in the first place is the best approach but since I need to make sure this conversion works for my non-graphical servers, I am more inclined to solve it through code/cmd.

Any idea why is this happening? Any help will be appreciated.

assuming your »file« is actually a valid »pathlib.Path«? It works for me … it doesnt matter if the file in question is already opened by whatever instance of Libreoffice

## no need for »--headless« !
run(['soffice', '--convert-to',  'pdf',
     '--outdir', f'{fp.parent}', f'{fp}'])

Could you clarify what does that mean? Given that you mentioned WSL2, could cmd mean Windows’ cmd.exe? is “GUI” different from the LibreOffice installed “through cmd”?

Hello.

For cmd, I basically meant calling Libreoffice using Python’s Subprocess library with the code I provided.

For GUI, I meant executing LibreOffice by inputting “libreoffice” in WSL terminal to launch LibreOffice and then opening the fille and converting it by clicking the PDF icon on the top left of LibreOffice’s graphical interface.

The issue is, for only SOME files, LibreOffice will create tmp file and lock file but stuck there forever.

Hope it helps.

It does not. I asked what does “installed LibreOffice through cmd” mean (emphasis on “installed”), and what does “manually installed GUI” mean (again, the “installed” is crucial). Because it seems to hint, that your “LibreOffice” from the first phrase is something different from “GUI” in the second, installed differently - so testing that “GUI” works might mean nothing WRT the failure in “cmd”.

I expect something like:

  • “I installed these Ubuntu packages, that allow headless operation, using apt-get: … . Then, when I realized I need GUI, I also added these packages, using the same apt-get: …”

  • or “I installed LibreOffice on Linux (WSL2) using snap. Then I added LibreOffice on my main Windows box, using MSI inastaller, which I call “GUI” here…”

  • or “the two instances of LibreOffice are installed in parallel from packages obtained from TDF, as explained on this wiki page: …”

or something in the line. Thus far, it is unclear what are you using, when and where.

I installed LibreOffice on my Linux WSL2 following this documentation: Installing LibreOffice on Linux - The Document Foundation Wiki

This documentation is not complete in a sense that after installation I had to install several other dependencies to make LibreOffice runnable, as listed below:

sudo dpkg -i *.deb
sudo apt-get install libxinerama1 -y
sudo apt-get update && sudo apt-get install --reinstall openssl
sudo apt install default-jre -y
sudo apt install default-jdk -y
sudo apt-get install libcairo2 -y
sudo apt-get install libxslt1.1 -y
sudo apt install libreoffice-java-common -y
sudo apt-get install unoconv -y

To resolve your confusion, no, there is one single LibreOffice throughout my words. For GUI, I essentially meant open LibreOffice without headless/invisible so that a LibreOffice would actually pop up(i.e., run libreoffice and work from there). For cmd, I meant explicitly sending LibreOffice a complete command in the terminal in headless mode(e.g., soffice --headless --convert-to pdf --outdir ./ path/to/file/to/convert)

Thank you.

sometimes or every single time ?
an example your can upload here ?

like crash ? stalled ? or running in a loop ?

Most files work 100% of time, but some files just never work. Due to security concern, I cannot upload those files, but as I compared them with “successful” files, they looks pretty much the same.

It did not crash or stalled, it just stuck in the process and never move on. I have waited for like 20min and nothing happened.

Just like the screenshot above, I type the command, press enter and wait. After a long time, nothing happened so I had to ^C it myself.

:thinking:
check CPU usage ?

do you also “sudo” for GUI test ?

maybe try Safe Mode

CPU usage did not even change a bit, always at a healthy ratio of 5% used.

GUI test works just fine with or without sudo.

And nope, safe mode did not help either.

the quoted part above does completly install into /opt/libreoffice…/

in this case, in python with subprocess.run you should use

run(['/opt/libreoffice…/program/soffice',   # !! put in the whole path to `soffice` !!
     '--convert-to',  'pdf',
     '--outdir', f'{fp.parent}', f'{fp}'])

here ⇑you messsed up your installation with things from your wsl-subsystem-provider (?ubuntu?) which do NOT work together with the package below /opt/…

at this stage I would suggest to remove anything above with:

sudo apt-get remove libxinerama1 \
openssl \
default-jre \
default-jdk \
libcairo2 \
libxslt1.1 \
libreoffice-java-common \
unoconv 

the best way is IMHO: set up a complete NEW wsl2, and install exclusivly with

sudo apt-get install libreoffice 

!! nothing else, no manual downloads etcpp !!

Hello, just set up a brand-new WSL and installed LibreOffice 24.2.7.2 420 (Build:2) by executing sudo apt-get install libreoffice as you suggested, unfortunately it still stuck at the same file before. Does LibreOffice have any debug/dev feature where I can see how/what is going on behind the scene?

Thank you.

I am more interested about the different behavior between using and not using GUI. Can anyone tell where might be the reason?

Yes, that seems the interesting part. Can you share a sample-file, not convertable on your command-line.

It is open source, so you are welcome to dive in …
But it is no small codebase…

Hello folks, a quite update on this issue.

It turns out to be triggered by the content inside. There is a table that takes up to 3 pages, with lots of empty rows attached to its end. If I remove some rows, no matter they are empty or not, to the point where the table length is kept under 2 pages, the conversion will work just fine. The other workaround is I noticed a manual line break ^l followed by the table, instead of a normal paragraph mark ^p, if I replace the line break with paragraph mark, the conversion will work too.

Sounds like a miss of internal logic of LibreOffice?

Anyway, this is still quite counter-intuitive to me, does anyone would like to contribute their thoughts from here?

Unfortunately I cannot provide the file due to security concern, plus I am not quite familiar with c++ so I am not confident debugging LibreOffice’s source code…

The usual suggestion is to replace (in a copy) all letters with a marker like - or letter x, to remove information, but leaving the file intact. Obviously you should not activate “track changes”.
.
If the redacted file shows the error, post it.

good idea, I just made up a sample file with same issue, please check it out in the attachment!
sample.docx (91.8 KB)

first, try from command line :
soffice --convert-to pdf your.docx

your.pdf should be produced in the same directory as your.docx

Please avoid posting nonsense. The commend puts the result to the current directory, not where you imagine that. --outdir controls that.

1 Like