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
- Create PDF from TeX (LaTeX) File in Java
- Create PDF from LTX (LaTeX) File in Java
- Convert TeX to XPS using Java
- Convert TeX ZIP Directory into PDF ZIP Directory using Java
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>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
<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:
- Firstly, create an instance of the TeXOptions class using the consoleAppOptions() method.
- Set TeX configuration as TeXConfig.objectLaTeX().
- Next, set input and output working directories.
- 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.
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:
- Firstly, open streams on ZIP archives as the input and output working directory.
- Next, create an instance of the TeXOptions class using the consoleAppOptions() method.
- Then, set TeX configuration as TeXConfig.objectLaTeX().
- Next, set input and output ZIP directories.
- 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.
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.