Aspose.TeX's output interface | Python

For formal definitions of I/O implementation, please refer to the API reference of Aspose.TeX for Python.

The idea behind the output directory

Aspose.TeX defines a directory as a mapping between names and bulks of data, where the bulks of data can be files, streams, arrays, or other types of data. The API allows for the specification of separate input and output working directories. For the output, the API provides the general IOutputWorkingDirectory interface, which can be implemented by the user for their specific needs. Additionally, the API provides ready-to-use implementations, which will be discussed below. The IOutputWorkingDirectory interface extends IInputWorkingDirectory, as the engine may first create and write a file, and then read it back. The interface includes the get_output_file() method, which returns the stream to write to, in contrast to the get_file() method, which returns the stream to read from.

Saving the output as a file in the disk file system

As mentioned above, the output_working_directory is commonly set as an instance of the OutputFileSystemDirectory class.

Here’s how we would set it:

Set ASPOSE_TEX_OUTPUT_DIR to an output directory, or use the default output. Apply a license first as described in Licensing.

 1# Write an XPS document and its transcript to a file system directory.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from io import BytesIO
 6from aspose.tex.io import OutputFileSystemDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice
 8
 9output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
10output_dir.mkdir(parents=True, exist_ok=True)
11
12options = TeXOptions.console_app_options(TeXConfig.object_tex())
13options.interaction = Interaction.NONSTOP_MODE
14options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
15options.terminal_out = OutputMemoryTerminal()
16device = XpsDevice()
17with BytesIO(b"Hello, World!\\end") as input_stream:
18    job_result = TeXJob(input_stream, device, options).run()

The example processes an inline plain TeX stream and creates texput.xps and texput.log in the output directory.

Saving the output as a ZIP archive file.

Another option is to create a file or stream and use it as a ZIP archive to store the output files. Here is an example:

 1# Write an XPS document and transcript into a finalized ZIP archive.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from io import BytesIO
 6from aspose.tex.io import OutputZipDirectory, OutputMemoryTerminal
 7from aspose.tex.presentation.xps import XpsDevice
 8
 9output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
10output_dir.mkdir(parents=True, exist_ok=True)
11
12with (output_dir / "zip-xps-out.zip").open("wb") as output_stream:
13    directory = OutputZipDirectory(output_stream)
14    options = TeXOptions.console_app_options(TeXConfig.object_tex())
15    options.interaction = Interaction.NONSTOP_MODE
16    options.output_working_directory = directory
17    options.terminal_out = OutputMemoryTerminal()
18    try:
19        device = XpsDevice()
20        with BytesIO(b"Hello, World!\\end") as input_stream:
21            job_result = TeXJob(input_stream, device, options).run()
22    finally:
23        # Write the ZIP directory before closing its underlying stream.
24        directory.finish()

The example opens zip-xps-out.zip in binary write mode (wb). Call finish() on OutputZipDirectory before closing its underlying stream. The archive contains texput.xps and texput.log.

To begin, we create an output stream for the ZIP file. Then, after creating the conversion options, we set the output_working_directory option to be an instance of the OutputZipDirectory class.

The idea behind the output terminal

The terminal output is another important aspect of the output. For this, Aspose.TeX for Python defines the general IOutputTerminal interface, which has a single property that returns an instance of the TerminalWriter implementation. The available implementations will be discussed below.

Writing the terminal output to the console

To achieve this, we need to set the terminal_out option as an instance of the OutputConsoleTerminal class.

 1# Write TeX terminal output to the console.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from io import BytesIO
 6from aspose.tex.io import OutputFileSystemDirectory, OutputConsoleTerminal
 7from aspose.tex.presentation.xps import XpsDevice
 8
 9output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
10output_dir.mkdir(parents=True, exist_ok=True)
11
12options = TeXOptions.console_app_options(TeXConfig.object_tex())
13options.interaction = Interaction.NONSTOP_MODE
14options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
15options.terminal_out = OutputConsoleTerminal()
16device = XpsDevice()
17with BytesIO(b"Hello, World!\\end") as input_stream:
18    job_result = TeXJob(input_stream, device, options).run()

Once again, this is the default value of the option, so there is no real need to specify it. Therefore, this section is provided solely for demonstration purposes.

Writing terminal output to a file

In contrast to the input terminal, Aspose.TeX for Python includes an implementation of the IOutputTerminal interface that enables us to write the terminal output to a file within a specified output directory.

 1# Write TeX terminal output to a .trm file.
 2import os
 3from pathlib import Path
 4from aspose.tex import Interaction, TeXConfig, TeXJob, TeXOptions
 5from io import BytesIO
 6from aspose.tex.io import OutputFileSystemDirectory, OutputFileTerminal
 7from aspose.tex.presentation.xps import XpsDevice
 8
 9output_dir = Path(os.environ.get("ASPOSE_TEX_OUTPUT_DIR", "output")).resolve()
10output_dir.mkdir(parents=True, exist_ok=True)
11
12options = TeXOptions.console_app_options(TeXConfig.object_tex())
13options.interaction = Interaction.NONSTOP_MODE
14options.output_working_directory = OutputFileSystemDirectory(str(output_dir))
15options.terminal_out = OutputFileTerminal(options.output_working_directory)
16device = XpsDevice()
17with BytesIO(b"Hello, World!\\end") as input_stream:
18    job_result = TeXJob(input_stream, device, options).run()

OutputFileTerminal writes texput.trm into the configured output working directory alongside texput.xps and texput.log. Its constructor receives the output working directory.