Generate PDF from LaTeX in Java

LaTeX remains the standard for technical and scientific publishing. LaTeX source files—either .tex or .ltx—contain markup that defines text, symbols, equations, and graphics. Using Aspose.TeX for Java, you can programmatically convert these sources into a PDF document that preserves the original layout and styling. This article demonstrates how to generate PDFs from LaTeX files in Java, covering both single‑file and bulk ZIP conversions.

The following topics are covered:

Java API to Generate PDF from LaTeX

To convert LaTeX files to PDF (or XPS) in Java, we use the Aspose.TeX for Java library. It provides a robust typesetting engine that supports TeX, LaTeX, and LTX formats and can output PDF, XPS, or image files. Add the library to your Maven project by including the repository and dependency below, or download the JAR directly.

Repository:

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>

Dependency:

<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-tex</artifactId>
    <version>21.11</version>
    <type>jar</type>
</dependency>

Generate PDF from LaTeX TeX File in Java

The steps below illustrate how to convert a standard .tex file to PDF:

  1. Create a TeXOptions instance via the consoleAppOptions() method.
  2. Apply the LaTeX configuration using TeXConfig.objectLaTeX().
  3. Define the input folder (containing the .tex file) and the output folder for the PDF.
  4. Choose a console or memory stream as the output terminal.
  5. Set the rendering options to produce a PDF file.
  6. Execute the job with run(), passing a TexJob object and a PdfDevice instance.

The code snippet below demonstrates the complete process.

Create PDF from LaTeX TeX File in Java

Create PDF from LaTeX TeX File in Java.

Generate PDF from LTX File in Java

An .ltx file follows the same LaTeX syntax as a .tex file but uses a different extension. The conversion workflow is identical; you only need to point the TeXOptions to the .ltx source in the final step. This allows seamless handling of both file types within the same Java application.

The example below shows how to generate a PDF from an LTX source.

Convert TeX to XPS using Java

If you need an XPS output instead of PDF, replace the PdfSaveOptions with XpsSaveOptions at step 5. The rest of the workflow remains unchanged, giving you a flexible way to produce either format from the same LaTeX source.

The following snippet converts a TeX file to XPS.

Convert TeX ZIP Directory into PDF ZIP Directory using Java

For batch processing, you can convert multiple TeX files packaged in a ZIP archive to a single PDF‑ZIP archive. The procedure mirrors the single‑file conversion with two additional steps:

  1. Open input and output ZIP streams to serve as working directories.
  2. Configure TeXOptions and LaTeX settings as before.
  3. Set the ZIP directories as the source and destination.
  4. Run the job, which extracts each .tex file, renders it to PDF, and repackages the results into the output ZIP.

The code below implements this bulk conversion.

Convert a TeX ZIP archive to a PDF ZIP archive

Convert a TeX ZIP archive to a PDF ZIP archive.

Get a Free License

You can get a free temporary license to evaluate Aspose.TeX without any usage restrictions.

Conclusion

In this guide we demonstrated how to:

  • Convert a single LaTeX (.tex or .ltx) file to PDF using Java.
  • Produce XPS output from a TeX source.
  • Batch‑process a ZIP archive of TeX files into a PDF‑ZIP archive.

These examples showcase the power of Aspose.TeX for Java for automated document generation. For deeper integration details, explore the full Aspose.TeX for Java documentation or ask questions on the forum.

See Also