apply a template before creating a pdf file in libreoffice headless mode

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật