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:
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.
Then, specify console or memory stream as an output terminal.
After that, set options for rendering into PDF format.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Then, specify console or memory stream as an output terminal.
After that, set options for rendering into PDF format.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.