Skip to main content

Converting to PDF

caution

Services need to be mocked within the context of (automated) testing.

Various formats can be converted to PDF using VIKTOR's built-in services. Converters for the following file types are currently supported:

Word to PDF

A Word file can be converted to PDF using the convert_word_to_pdf function:

from viktor.utils import convert_word_to_pdf

# using File object
file1 = File.from_path(Path(__file__).parent / "mydocument.docx")
with file1.open_binary() as f1:
pdf = convert_word_to_pdf(f1)

# using built-in `open()`
with open(Path(__file__).parent / "mydocument.docx", "rb") as f1:
pdf = convert_word_to_pdf(f1)

Excel to PDF

An Excel file can be converted to PDF using the convert_excel_to_pdf function:

from viktor.utils import convert_excel_to_pdf

# using File object
file1 = File.from_path(Path(__file__).parent / "mydocument.xlsx")
with file1.open_binary() as f1:
pdf = convert_excel_to_pdf(f1)

# using built-in `open()`
with open(Path(__file__).parent / "mydocument.xlsx", "rb") as f1:
pdf = convert_excel_to_pdf(f1)

SVG to PDF

An SVG image file can be converted to PDF using the convert_svg_to_pdf function:

from viktor.utils import convert_svg_to_pdf

# using File object
file1 = File.from_path(Path(__file__).parent / "mydocument.svg")
with file1.open_binary() as f1:
pdf = convert_svg_to_pdf(f1)

# using built-in `open()`
with open(Path(__file__).parent / "mydocument.svg", "rb") as f1:
pdf = convert_svg_to_pdf(f1)