Create PDF from LaTeX in Java

LaTeX is widely used for creating technical and scientific documentation. LaTeX files (TEX or LTX) are source documents filled with commands to specify the format of the document, including text, symbols, mathematical expressions, and graphics. We can easily generate a PDF document from the LaTeX source programmatically. PDF allows sharing documents in a read-only format without compromising their style or layout. In this article, we will learn how to create a PDF document from the LaTeX source file in Java.

The following topics shall be covered in this article:

Java API to Create PDF from LaTeX

For creating PDF documents from LaTeX source files, we will be using the Aspose.TeX for Java API. It allows typesetting TeX files to different file formats like PDF, XPS, or images. Please either download the JAR of the API or add the following pom.xml configuration in a Maven-based Java application.

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>

Create PDF from LaTeX TeX File in Java

We can create a PDF document from a LaTeX (TeX) file by following the steps given below:

  1. Firstly, create an instance of the TeXOptions class using the consoleAppOptions() method.
  2. Set TeX configuration as TeXConfig.objectLaTeX().
  3. Next, set input and output working directories.
  4. Then, specify console or memory stream as an output terminal.
  5. After that, set options for rendering into PDF format.
  6. Finally, call the run() method for the newly created TexJob class instance with the output PDF and the PdfDevice class object as arguments.

The following code example demonstrates how to create a PDF from a TeX file using Java.

Create PDF from LaTeX TeX File in Java

Create PDF from LaTeX TeX File in Java.

Create PDF from LTX File in Java

LaTeX uses an LTX file extension typesetting markup tags and properties that define the document structure and content. We can create a PDF from LaTeX (LTX) file as well by following the steps mentioned earlier. However, we just need to input the source file with the .ltx extension in the last step.

The following code example demonstrates how to create a PDF from an LTX file using Java.

Convert TeX to XPS using Java

We can also convert LaTeX (TeX) file to XPS by following the steps mentioned earlier. However, we just need to set the XpsSaveOptions instead of PdfSaveOptions at step # 5.

The following code example demonstrates how to convert TeX to XPS using Java.

Convert TeX ZIP Directory into PDF ZIP Directory using Java

We can convert multiple TeX files compressed in a ZIP archive to PDF at once by following the steps given below:

  1. Firstly, open streams on ZIP archives as the input and output working directory.
  2. Next, create an instance of the TeXOptions class using the consoleAppOptions() method.
  3. Then, set TeX configuration as TeXConfig.objectLaTeX().
  4. Next, set input and output ZIP directories.
  5. Then, specify console or memory stream as an output terminal.
  6. After that, set options for rendering into PDF format.
  7. Finally, call the run() method for the newly created TexJob class instance with the output PDF and the PdfDevice class object as arguments.

The following code example demonstrates how to convert a TeX ZIP archive to a PDF ZIP archive using Java.

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 try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • generate a PDF output from the LaTeX source file in Java;
  • create an XPS output document from the TeX file programmatically;
  • convert a TeX ZIP archive into a PDF ZIP archive using Java.

Besides, you can learn more about Aspose.TeX for Java API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also