Introduction
Aspose.TeX FOSS for Python exposes a concise engine API built around three concepts:
a unified entry point (TeXJob), pluggable output devices (PdfDevice, DviDevice,
SvgDevice), and two input source types (FileInputSource, StringInputSource).
This article covers each component and shows how they combine to produce PDF, DVI,
and SVG output from TeX markup.
TeXJob — The Entry Point
TeXJob is the main class for driving the TeX engine. It accepts an input source, an
output device, and optional TeXOptions, then runs the typesetting pipeline when
run() is called. The return value is the output bytes for device types that produce
a single output (PDF, DVI); for SVG output, pages are retrieved from the device object
after run() completes.
TeXOptions configures engine behavior. Constructing it with defaults (TeXOptions())
uses the standard TeX settings suitable for most documents.
Output Devices
PdfDevice
PdfDevice writes the typeset document to a PDF byte stream. It is the standard
output device for producing portable, printable output:
from aspose_tex import TeXJob, TeXOptions, PdfDevice, StringInputSource
pdf_bytes = TeXJob(StringInputSource("Hello, world!"), PdfDevice(), options=TeXOptions()).run()
with open("output.pdf", "wb") as f:
f.write(pdf_bytes)
After run(), the returned bytes contain the complete PDF document.
DviDevice
DviDevice produces Device Independent (DVI) format output — the native intermediate
format of TeX. DVI output can be consumed by downstream tools for rendering or
further processing:
from aspose_tex import TeXJob, TeXOptions, DviDevice, StringInputSource
dvi_bytes = TeXJob(StringInputSource("Hello!"), DviDevice(), options=TeXOptions()).run()
with open("output.dvi", "wb") as f:
f.write(dvi_bytes)
SvgDevice
SvgDevice renders each typeset page as an SVG vector graphic. After the job runs,
get_all_pages() returns the list of per-page SVG byte strings:
from aspose_tex import TeXJob, TeXOptions, SvgDevice, StringInputSource
device = SvgDevice()
TeXJob(StringInputSource("Hello!"), device, options=TeXOptions()).run()
for i, svg in enumerate(device.get_all_pages()):
with open(f"page_{i}.svg", "wb") as f:
f.write(svg)
Input Sources
FileInputSource
FileInputSource reads TeX markup from a .tex file on disk. This is the
standard input mode when typesetting existing documents:
from aspose_tex import TeXJob, TeXOptions, PdfDevice, FileInputSource
pdf_bytes = TeXJob(FileInputSource("document.tex"), PdfDevice(), options=TeXOptions()).run()
StringInputSource
StringInputSource accepts TeX markup as a Python string. This input mode is
used for dynamic markup generation — constructing TeX programmatically and
typesetting it without writing to disk:
from aspose_tex import TeXJob, TeXOptions, PdfDevice, StringInputSource
tex = "Hello, world!"
pdf_bytes = TeXJob(StringInputSource(tex), PdfDevice(), options=TeXOptions()).run()
Combining Components
The three components work together to form a complete typesetting pipeline. Select the input source that matches your data flow and the device that matches your output format:
from aspose_tex import TeXJob, TeXOptions, SvgDevice, FileInputSource
device = SvgDevice()
TeXJob(FileInputSource("report.tex"), device, options=TeXOptions()).run()
pages = device.get_all_pages()
print(f"{len(pages)} SVG pages produced")
Tips and Best Practices
- Use
StringInputSourcefor short or dynamically generated TeX fragments; useFileInputSourcewhen working with existing.texfiles. PdfDeviceis the most portable output format; useSvgDevicewhen embedding TeX output in web pages or vector graphics workflows.- Reuse
TeXOptionsinstances across jobs if processing multiple documents with the same settings. - The
run()return value isbytesfor PDF and DVI; for SVG, the bytes are retrieved fromSvgDevice.get_all_pages()after the job completes.
API Reference Summary
| Class | Role |
|---|---|
TeXJob | Entry point; drives the TeX typesetting pipeline |
TeXOptions | Engine configuration; defaults work for most documents |
PdfDevice | Output device producing PDF bytes |
DviDevice | Output device producing DVI bytes |
SvgDevice | Output device producing per-page SVG via get_all_pages() |
FileInputSource | Input from a .tex file on disk |
StringInputSource | Input from a Python string |