I am using this docker command to create a pdf file in headless mode.
It is working as expected.
# docker run --rm -v .:/workspace shantanuo/libreoffice-converter /workspace/pm_in_paris.odt --outdir /workspace
This is my docker file:
# vi Dockerfile
FROM ubuntu:latest
RUN apt-get update &&
apt-get install -y libreoffice
WORKDIR /workspace
ENTRYPOINT ["libreoffice", "--headless", "--convert-to", "pdf"]
But I am not able to apply a template before creating a pdf file.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y libreoffice python3 python3-venv
RUN python3 -m venv /workspace/venv
RUN /workspace/venv/bin/pip install --upgrade pip
RUN /workspace/venv/bin/pip install unotools
COPY * /workspace/
WORKDIR /workspace
CMD ["/workspace/venv/bin/python3", "/workspace/convert_to_pdf.py", "/workspace/paris.odt", "/workspace/template.ott", "/workspace/output.pdf"]
I built docker image using the above mentioned file and run it:
docker run -it shantanuo/libreoffice-converter2 bash
I execute this command from the docker container:
root@8a6d5682dcfa:/workspace# soffice --headless --accept="pipe,name=libreoffice;urp;StarOffice.ComponentContext" & python3 /workspace/convert_to_pdf.py /workspace/in_paris.odt /workspace/maaya.ott /workspace/
And get this error:
Traceback (most recent call last):
File "/workspace/convert_to_pdf.py", line 42, in <module>
apply_template_and_convert_to_pdf(args.document, args.template, args.output)
File "/workspace/convert_to_pdf.py", line 22, in apply_template_and_convert_to_pdf
document.applyTemplate(template)
^^^^^^^^^^^^^^^^^^^^^^
AttributeError: applyTemplate
It looks like applyTemplate property is not available.
cat convert_to_pdf.py
import uno
import argparse
def apply_template_and_convert_to_pdf(document_path, template_path, output_path):
# Create the LibreOffice UNO connection
local_context = uno.getComponentContext()
resolver = local_context.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local_context)
...
...
document.applyTemplate(template)
...
...
Any suggestion will be appreciated.
Update:
If I run these 3 commands, I get the expected output.
docker run -it shantanuo/libreoffice-converter2 bash
soffice --headless --accept="socket,host=localhost,port=2002;urp;" &
soffice --headless --accept="pipe,name=libreoffice;urp;StarOffice.ComponentContext" & python3 /workspace/updated3.py /workspace/ra.txt /workspace/prajakta.ott
I just need them to be part of dockerfile.
Update 2:
I expect the converted PDF file to be available in the current directory if I use this command…
docker run -v .:/workspace/ -d shantanuo/libreoffice-converter4
Even if I have this Dockerfile, I still need to run last 3 commands manually.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y libreoffice python3 python3-venv
RUN python3 -m venv /workspace/venv
RUN /workspace/venv/bin/pip install --upgrade pip
RUN /workspace/venv/bin/pip install unotools
COPY * /workspace/
WORKDIR /workspace
CMD soffice --headless --accept="socket,host=localhost,port=2002;urp;" &
CMD soffice --headless --accept="pipe,name=libreoffice;urp;StarOffice.ComponentContext" &
CMD ["python3", "/workspace/updated3.py", "/workspace/ra.txt", "/workspace/prajakta.ott"]
I need to start the container, connect to “bash” and then execute the commands one by one:
docker run -it shantanuo/libreoffice-converter3 bash
Not able to achieve complete automation.
1
chatGPT provided this Dockerfile that is working as expected.
FROM ubuntu:latest
RUN apt-get update && apt-get install -y libreoffice python3 python3-venv
RUN python3 -m venv /workspace/venv
RUN /workspace/venv/bin/pip install --upgrade pip
RUN /workspace/venv/bin/pip install unotools
COPY * /workspace/
WORKDIR /workspace
# Start LibreOffice in headless mode in the background and run the Python script after it is started
ENTRYPOINT soffice --headless --accept="pipe,name=libreoffice;urp;StarOffice.ComponentContext" &
sleep 5 &&
python3 /workspace/updated3.py /workspace/ra.txt /workspace/prajakta.ott
I can create an image and it converts the text file to PDF correctly.
docker build -t shantanuo/libreoffice-converter4 .
The raw text file and template is available in current directory. The generated PDF is also available in the same place after running this command:
docker run -v .:/workspace/ --rm shantanuo/libreoffice-converter4
3