使用 soffice 转换为 pdf 会添加空白页

Converting to pdf using soffice adds blank page

我正在尝试使用 soffice & python:

将 .ods 文件转换为 pdf
import os
import subprocess

def ods_to_pdf(ods_filename):
    file = os.path.join(os.getcwd(), ods_filename)
    path_to_soffice = "path/to/soffice"
    subprocess.run([path_to_soffice, "--headless", "--convert-to", "pdf", file], check=True)

它工作正常,但生成的 pdf 文件末尾有一个空白页(有时是两页)。有谁知道我怎样才能防止这种行为?该代码在 Docker 容器中运行,以 Ubuntu 18.04 作为基础映像。 LibreOffice版本:7.1.0(我也试过6.1.6.3,结果一样)。

我找不到如何防止 LibreOffice 添加空白页,但通过在转换后删除空白页解决了这个问题:

import PyPDF2

output = PyPDF2.PdfFileWriter()
    
input = PyPDF2.PdfFileReader(open("file.pdf", "rb"))
number_of_pages = input.getNumPages()

for current_page_number in range(number_of_pages):
    page = input.getPage(current_page_number)
    if page.extractText() != "":
        output.addPage(page)
    
output_stream = open("output.pdf", "wb")
output.write(output_stream)