Alternative ways of specifying TeX main input in Java

Explore flexible ways to feed main TeX and LaTeX source content into the typesetting engine using Aspose.TeX for Java. Beyond loading static .tex files from a file system or ZIP archive, developers can pass raw TeX string variables, memory streams, byte arrays, or custom readers directly into TeXJob for real-time document compilation and server-rendered report generation.

 

Depending on your application architecture (e.g., REST API, database-driven publishing, cloud microservice), choose the main input method that best fits your workflow:

  • For standard file-based projects where the main .tex file resides in IInputWorkingDirectory use a named file as input and new TeXJob(‘main’, device, options) construction.
  • For the dynamic document generation from template engines use a string as input source and new TeXJob(new StringReader(latexCode), device, options) construction.
  • For streaming input directly from network requests, database BLOBs, or cloud storage (S3/GCS) use a InputStream as input source and new TeXJob(inputStream, device, options) construction.
  • For the high-performance in-memory processing without disk I/O bottlenecks use a memory buffer as input source and new new TeXJob(new ByteArrayInputStream(bytes), device, options) construction.

Passing TeX source as a Java string variable

Generate TeX documents directly from raw strings or dynamic string templates without saving temporary .tex files to disk.

import com.aspose.tex.*;
import java.io.StringReader;

public class TeXInputFromString {
    public static void main(String[] args) throws Exception {
        // Create a dynamic LaTeX string
        String latexContent = "\\documentclass{article}\n" +
                             "\\begin{document}\n" +
                             "\\title{Dynamic Invoice}\n" +
                             "\\author{Automated System}\n" +
                             "\\maketitle\n" +
                             "\\section{Summary}\n" +
                             "This document was compiled directly from a Java String variable.\n" +
                             "\\end{document}";

        // Initialize LaTeX conversion options
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Run TeX job passing StringReader as the main input
        TeXJob job = new TeXJob(new StringReader(latexContent), new PdfDevice(), options);
        job.run();

        System.out.println("Document compiled successfully from String input.");
    }
}

Passing TeX Source from an InputStream

Read TeX source code directly from input streams provided by HTTP requests, database fields, or resource bundles.

import com.aspose.tex.*;
import java.io.FileInputStream;
import java.io.InputStream;

public class TeXInputFromInputStream {
    public static void main(String[] args) throws Exception {
        // Open an input stream from a remote or custom source
        InputStream texStream = new FileInputStream("C:/TeXProject/Templates/report.tex");

        // Initialize options
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Execute TeX job directly with InputStream
        TeXJob job = new TeXJob(texStream, new PdfDevice(), options);
        job.run();

        texStream.close();
        System.out.println("TeX compilation from InputStream finished.");
    }
}

Combining Stream Input with Local Working Directory Assets

When passing main TeX content via stream or string, secondary assets (like external images \includegraphics{} or style packages \usepackage{}) can still be resolved from an IInputWorkingDirectory.

import com.aspose.tex.*;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;

public class TeXStreamWithAssetDirectory {
    public static void main(String[] args) throws Exception {
        // LaTeX string referencing an external image asset 'logo.png'
        String latexCode = "\\documentclass{article}\n" +
                           "\\usepackage{graphicx}\n" +
                           "\\begin{document}\n" +
                           "\\includegraphics{logo.png}\n" +
                           "\\end{document}";

        ByteArrayInputStream inputStream = new ByteArrayInputStream(latexCode.getBytes(StandardCharsets.UTF_8));

        // Configure options and set directory for resolving assets (logo.png)
        TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
        options.setInputWorkingDirectory(new InputFileSystemDirectory("C:/TeXProject/Assets/"));
        options.setOutputWorkingDirectory(new OutputFileSystemDirectory("C:/TeXProject/Output/"));

        // Run TeX job
        TeXJob job = new TeXJob(inputStream, new PdfDevice(), options);
        job.run();

        System.out.println("Dynamic stream compiled with external assets resolved successfully.");
    }
}

Installation and Setup

Integrate Aspose.TeX for Java into your project using Maven:

Package Manager Console Command

<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>Latest</version>
</dependency>

FAQ

1. Do I need to create temporary disk files when converting LaTeX strings to PDF?

No. You can pass string content using StringReader or ByteArrayInputStream directly to TeXJob and stream the output to ByteArrayOutputStream. This allows 100% in-memory processing without touching the disk.

2. How are relative file references inside a stream-based TeX source resolved?

When main input is passed as a stream or string, relative paths inside \input{}, \include{}, or \includegraphics{} are resolved against the directory configured via options.setInputWorkingDirectory().

3. What happens to output naming when input is provided as a stream?

When main input is a stream, Aspose.TeX uses the default job name (or the job name explicitly configured using options.setJobName(‘custom_name’)) to name output PDF/XPS files and auxiliary log files.