How to use LibreOffice and unoserver to convert docx to pdf in Ubuntu/Debian
2023年8月29日 / kirito / 3 Comments
- Install LibreOffice and unoserver with the following commands:
|
apt install libreoffice python3 python3-pip pip install unoserver |
- Make sure the environment is ok:
|
wget -O find_uno.py https://gist.githubusercontent.com/regebro/036da022dc7d5241a0ee97efdf1458eb/raw/find_uno.py python3 find_uno.py |
- Start unoserver and take a conversion in diffrerent command line prompt:
|
python3 -m unoserver.server |
|
python3 -m unoserver.converter --convert-to pdf my.docx my.pdf |
- If you encounter the font problem, you should add windows fonts to Linux with the following steps.
- Copy all fonts files under C:\Windows\Fonts
- Remove the .fon files
- Copy the font files to Linux and paste the files in /usr/share/fonts/win/. and you should create win directory by yourself.
- Update the font cache with the following commands in /usr/share/fonts/win/ :
|
mkfontscale mkfontdir fc-cache |
If you cannot find the commands, run
|
apt-get install xfonts-utils |
- You can also check the font status via fc-list
- Create a system service for unoserver in /etc/systemd/system/unoserver.service:
|
[Unit] Description=unoserver After=network.target [Install] WantedBy=multi-user.target [Service] Type=simple User=root Group=root ExecStart=/usr/local/bin/unoserver Environment=PYTHONUNBUFFERED=true Restart=on-failure RestartSec=3 |
You should also use systemctl daemon-reload to take effect. And if you want to make the service on startup, run systemctl enable daemon-reload
asnikypl
2023年10月14日 - 20:26
Hii,
After unoserver installation I have start unoserver, and it’s done but after convert file that raised error. I have given details below that how I have performed commands and which error raised :
This is using in docker because my project has docker configuration.
#python3 -m unoserver.converter –convert-to pdf my.docx my.pdf
Traceback (most recent call last):
File “/usr/lib/python3.8/site-packages/unoserver/converter.py”, line 2, in
import uno
ModuleNotFoundError: No module named ‘uno’
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/usr/bin/unoconvert”, line 5, in
from unoserver.converter import main
File “/usr/lib/python3.8/site-packages/unoserver/converter.py”, line 4, in
raise ImportError(
ImportError: Could not find the ‘uno’ library. This package must be installed with a Python installation that has a ‘uno’ library. This typically means you should install it with the same Python executable as your Libreoffice installation uses.
Please help me and solve this out, if solved it is big thing for me.
Thank you
kirito
2023年10月14日 - 20:32
could you please post your Dockerfile?
Jose del rio
2024年8月1日 - 05:41
It’s clear that the environment where you have installed unoserver it is not the python used by libreoffice (yes, libreoffice uses the system env) so what you see is a consequence of that. I know because I had the same issue.
You can see a working dockerfile here: https://github.com/jgdelrio/unoserver-fastapi
Pay special attention to the lines:
RUN mkdir /environments
RUN virtualenv –python=/usr/bin/python3 –system-site-packages /environments/virtenv
RUN /environments/virtenv/bin/pip install unoserver
which are basically making sure the python used by libreoffice has the unoserver installed.