在 Java 中指定 TeX 主输入的替代方式
探索使用 Aspose.TeX for Java 将 TeX 和 LaTeX 源内容灵活传递到排版引擎的方法。除了从文件系统或 ZIP 存档加载静态 .tex 文件之外,开发人员还可以直接将原始 TeX 字符串变量、内存流、字节数组或自定义读取器传递给 TeXJob,从而实现实时文档编译和服务器端报告生成。
根据您的应用程序架构(例如 REST API、数据库驱动的发布系统、云微服务),选择最适合您工作流程的主输入方法:
- 对于标准的基于文件的项目,如果主
.tex文件位于IInputWorkingDirectory中,请使用命名文件作为输入,并使用新的TeXJob(‘main’, device, options) 构造。 - 对于使用模板引擎动态生成文档的场景,请使用字符串作为输入源,并使用新的
TeXJob(new StringReader(latexCode), device, options) 构造。 - 对于直接从网络请求、数据库 BLOB 或云存储(S3/GCS)进行流式输入的场景,请使用
InputStream作为输入源,并使用新的TeXJob(inputStream, device, options) 构造。 - 对于无需磁盘 I/O 且追求高性能的内存处理,请使用内存缓冲区作为输入源,并使用新的
TeXJob(new ByteArrayInputStream(bytes), device, options) 构造。
将 TeX 源代码作为 Java 字符串变量传递
直接从原始字符串或动态字符串模板生成 TeX 文档,无需将临时 .tex 文件保存到磁盘。
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.");
}
}从 InputStream 传递 TeX 源代码
直接从 HTTP 请求、数据库字段或资源包提供的输入流中读取 TeX 源代码。
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.");
}
}将流输入与本地工作目录资源结合使用
通过流或字符串传递主要 TeX 内容时,辅助资源(例如外部图像 \includegraphics{} 或样式包 \usepackage{})仍然可以从 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.");
}
}安装和设置
使用 Maven 将 Aspose.TeX for Java 集成到您的项目中:
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. 将 LaTeX 字符串转换为 PDF 时,我是否需要创建临时磁盘文件?
不需要。您可以使用 StringReader 或 ByteArrayInputStream 将字符串内容直接传递给 TeXJob,并将输出流式传递到 ByteArrayOutputStream。这样可以实现 100% 的内存处理,无需访问磁盘。
2. 基于流的 TeX 源中的相对文件引用如何解析?
当主输入以流或字符串形式传递时,\input{}、\include{} 或 \includegraphics{} 中的相对路径会根据通过 options.setInputWorkingDirectory() 配置的目录进行解析。
3. 当以流的形式提供输入时,输出命名会发生什么变化?
当主输入为流时,Aspose.TeX 使用默认作业名称(或通过 using options.setJobName(‘custom_name’) 显式配置的作业名称)来命名输出 PDF/XPS 文件和辅助日志文件。