LaTeX to XPS | Python

Another target format is XPS. An XPS file is essentially a ZIP package that contains еру paginated content of a document, along with the necessary metadata for proper display by specific viewers (such as Windows XPS Viewer) and printing. The package consists of various files, some of which are binary and contain resources like images, fonts, and ICC profiles. Others are XML files in specific schemas, including files that contain the document data itself. The document data is divided into multiple files, with each file representing an individual page of the document. These files consist of a single page element and a tree of child elements, including Canvas, Path, and Glyphs. The Canvas element is a grouping element that can contain other Canvases, Paths, and Glyphs, and it controls the appearance of all child elements as a group. Path elements are used to define vector graphics paths, while Glyphs elements are used to define textual content. All three elements have properties that define various aspects of appearance.

There exists the Aspose.Page library, which offers an API for manipulating XPS documents and also enables the conversion of these documents to PDF and raster image formats.

Converting LaTeX to XPS

The process of converting LaTeX to XPS is equally straightforward, similar to the conversion to raster image formats. However, it requires setting the save_options to an instance of the XpsSaveOptions class (either by default or explicitly), and changing the device to an instance of the XpsDevice class.

Set ASPOSE_TEX_INPUT_FILE to your LaTeX file and ASPOSE_TEX_OUTPUT_DIR to the output directory, or edit the paths in the example. The defaults are hello-world.ltx and output. Apply a license first as described in Licensing. The examples resolve additional input files beside the main document and use noninteractive processing.

 1# Convert LaTeX to XPS.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 6from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 7
 8# Set these paths to your LaTeX document and output directory.
 9input_file = Path(os.environ.get("ASPOSE_TEX_INPUT_FILE", "hello-world.ltx")).resolve()
10output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
11if not input_file.is_file():
12    raise FileNotFoundError(input_file)
13output_dir.mkdir(parents=True, exist_ok=True)
14
15# Use the Object LaTeX format and resolve additional inputs beside the main file.
16options = TeXOptions.console_app_options(TeXConfig.object_latex)
17options.input_working_directory = InputFileSystemDirectory(str(input_file.parent))
18options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
19options.interaction = Interaction.NONSTOP_MODE
20options.terminal_out = OutputMemoryTerminal()
21options.save_options = XpsSaveOptions()
22device = XpsDevice()
23job_result = TeXJob(str(input_file), device, options).run()

An alternative way to write the main output XPS file

There is an alternative constructor available for the XpsDevice class, which allows for obtaining the resulting XPS file in a different manner.

 1# Convert LaTeX to XPS using an explicitly named binary output stream.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from aspose.tex.io import InputFileSystemDirectory, OutputFileSystemDirectory, OutputMemoryTerminal
 6from aspose.tex.presentation.xps import XpsDevice, XpsSaveOptions
 7
 8# Set these paths to your LaTeX document and output directory.
 9input_file = Path(os.environ.get("ASPOSE_TEX_INPUT_FILE", "hello-world.ltx")).resolve()
10output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
11if not input_file.is_file():
12    raise FileNotFoundError(input_file)
13output_dir.mkdir(parents=True, exist_ok=True)
14
15# Use the Object LaTeX format and resolve additional inputs beside the main file.
16options = TeXOptions.console_app_options(TeXConfig.object_latex)
17options.input_working_directory = InputFileSystemDirectory(str(input_file.parent))
18options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
19options.interaction = Interaction.NONSTOP_MODE
20options.terminal_out = OutputMemoryTerminal()
21options.save_options = XpsSaveOptions()
22
23# Supply a binary stream to choose the output filename.
24with (output_dir / "any-name.xps").open("wb") as output_stream:
25    device = XpsDevice(output_stream)
26    job_result = TeXJob(str(input_file), device, options).run()

The binary stream writes any-name.xps into the configured output directory without creating an additional default-named XPS file, just as in the PDF stream example.

You can also explore the free web app for LaTeX-to-XPS conversion, which is developed using the Aspose.TeX for .NET API.