Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 17, 2024 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/67385c777283964d328086603f691ac9 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/67385c777283964d328086603f691ac9 to your computer and use it in GitHub Desktop.
Gists for Aspose.TeX for Java
// Create conversion options instance.
...
// Force the TeX engine to output the specified date in the title.
options.setDateTime(new GregorianCalendar(2022, Calendar.DECEMBER, 18).getTime());
// Create conversion options instance.
...
// Set to true to make the engine skip missing packages (when your file references one) without errors.
options.ignoreMissingPackages(true);
// Create conversion options instance.
...
// Specify the console as the input terminal.
options.setTerminalIn(new InputConsoleTerminal()); // Default value. Arbitrary assignment.
// Create conversion options instance.
...
// Specify a file system working directory for the input.
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Create conversion options instance.
...
// Create some device.
...
// Run LaTeX to XPS conversion.
new TeXJob(new ByteArrayInputStream(
"\\documentclass{article} \\begin{document} Hello, World! \\end{document}".getBytes("ASCII")),
new XpsDevice(), options).run();
// Open the stream for the ZIP archive that will serve as the input working directory.
final Stream inZipStream = File.Open(Path.Combine(RunExamples.InputDirectory, "zip-in.zip"), FileMode.Open))
{
// Create conversion options instance.
...
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.setInputWorkingDirectory(new InputZipDirectory(inZipStream, "in"));
} finally {
if (inZipStream != null)
inZipStream.close();
}
// Create conversion options instance.
...
// Set the interaction mode.
options.setInteraction(Interaction.NonstopMode);
// Create conversion options instance.
...
// Set the job name.
options.setJobName("my-job-name");
// Create conversion options instance.
...
// Initialize the options for saving in BMP format.
options.setSaveOptions(new BmpSaveOptions());
// Create conversion options instance.
...
// Initialize the options for saving in JPEG format.
options.setSaveOptions(new JpegSaveOptions());
// Create the stream to write the PDF file to.
final OutputStream pdfStream = new FileOutputStream(Utils.getOutputDirectory() + "any-name.pdf");
try {
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
...
// Run LaTeX to PDF conversion.
new TeXJob(Utils.getInputDirectory() + "hello-world.ltx", new PdfDevice(pdfStream), options).run();
} finally {
if (pdfStream != null)
pdfStream.close();
}
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Initialize the options for saving in PDF format.
options.setSaveOptions(new PdfSaveOptions());
// Run LaTeX to PDF conversion.
new TeXJob(Utils.getInputDirectory() + "hello-world.ltx", new PdfDevice(), options).run();
// Create conversion options instance.
...
// Initialize the options for saving in PNG format.
PngSaveOptions saveOptions = new PngSaveOptions();
// Set this property to instruct the device not to output images as you will access them alternatively.
saveOptions.deviceWritesImages(false);
options.setSaveOptions(saveOptions);
// Create the image device.
ImageDevice device = new ImageDevice();
// Run LaTeX to PNG conversion.
new TeXJob(Utils.getInputDirectory() + "hello-world.ltx", device, options).run();
// Save pages file by file.
for (int i = 0; i < device.getResult().length; i++)
{
final OutputStream fs = new FileOutputStream(Utils.getOutputDirectory() + MessageFormat.format("page-{0}.png", (i + 1)));
try {
fs.write(device.getResult()[i], 0, device.getResult()[i].length);
} finally {
if (fs != null)
fs.close();
}
}
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Initialize the options for saving in PNG format.
options.setSaveOptions(new PngSaveOptions());
// Run LaTeX to PNG conversion.
new TeXJob(Utils.getInputDirectory() + "hello-world.ltx", new ImageDevice(), options).run();
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Initialize the options for saving in SVG format.
options.setSaveOptions(new SvgSaveOptions());
// Run LaTeX to SVG conversion.
new TeXJob(Utils.getInputDirectory() + "hello-world.ltx", new SvgDevice(), options).run();
// Create conversion options instance.
...
// Initialize the options for saving in TIFF format.
options.setSaveOptions(TiffSaveOptions());
// Create the stream to write the XPS file to.
final OutputStream xpsStream = new FileOutputStream(Utils.getOutputDirectory() + "any-name.xps");
{
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
...
// Initialize the options for saving in XPS format.
options.setSaveOptions(new XpsSaveOptions()); // Default value. Arbitrary assignment.
// Run LaTeX to XPS conversion.
new TeXJob(Utils.getInputDirectory() + "hello-world.ltx", new XpsDevice(xpsStream), options).run();
} finally {
if (xpsStream != null)
xpsStream.close();
}
// Create conversion options instance.
...
// Initialize the options for saving in XPS format.
options.setSaveOptions(new XpsSaveOptions()); // Default value. Arbitrary assignment.
// Run LaTeX to XPS conversion.
new TeXJob(Utils.getInputDirectory() + "sample.ltx", new XpsDevice(), options).run();
// Create conversion options instance.
...
// Run LaTeX to XPS conversion.
new TeXJob(new XpsDevice(), options).run();
// Create conversion options instance.
...
// Set to true to make the engine not construct ligatures where normally it would.
options.noLigatures(true);
// Create conversion options instance.
...
// Specify the console as the input terminal.
options.setTerminalOut(new OutputConsoleTerminal()); // Default value. Arbitrary assignment.
// Create conversion options instance.
...
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Create conversion options instance.
...
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.setTerminalOut(new OutputFileTerminal(options.getOutputWorkingDirectory()));
// Open the stream for the ZIP archive that will serve as the output working directory.
final OutputStream outZipStream = new FileOutputStream(Utils.getOutputDirectory() + "zip-pdf-out.zip");
{
// Create conversion options instance.
...
// Specify a ZIP archive working directory for the output.
options.setOutputWorkingDirectory(new OutputZipDirectory(outZipStream));
} finally {
if (outZipStream != null)
outZipStream.close();
}
// Create conversion options instance.
...
// Create and assign saving options instance if needed.
...
// Set to true if you want math formulas to be converted to raster images.
options.getSaveOptions().rasterizeFormulas(true);
// Create conversion options instance.
...
// Create and assign saving options instance if needed.
...
// Set to true if you want included graphics (if it contains vector elements) to be converted to raster images.
options.getSaveOptions().rasterizeIncludedGraphics(true);
// Create conversion options instance.
...
// Ask the engine to repeat the job.
options.repeat(true);
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Specify a file system working directory for the required input.
// The directory containing packages may be located anywhere.
options.setRequiredInputDirectory(new InputFileSystemDirectory(Utils.getInputDirectory() + "packages"));
// Initialize the options for saving in PNG format.
options.setSaveOptions(new PngSaveOptions());
// Run LaTeX to PNG conversion.
new TeXJob(Utils.getInputDirectory() + "required-input-fs.tex", new ImageDevice(), options).run();
// Create conversion options for Object LaTeX format upon Object TeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectLaTeX());
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Initialize the options for saving in PNG format.
options.setSaveOptions(new PngSaveOptions());
// Create a file stream for the ZIP archive containing the required package.
// The ZIP archive may be located anywhere.
final InputStream stream = new FileInputStream(Utils.getInputDirectory() + "packages\\pgfplots.zip");
try {
// Specify a ZIP working directory for the required input.
options.setRequiredInputDirectory(new InputZipDirectory(stream, ""));
// Run LaTeX to PNG conversion.
new TeXJob(Utils.getInputDirectory() + "required-input-zip.tex", new ImageDevice(), options).run();
} finally {
if (stream != null)
stream.close();
}
// This is an implementation of IInputWorkingDirectory that is suitable for the TeX job's RequiredInputDirectory option
// in case required input contains fonts provided by external packages.
// The class additionally implements IFileCollector, which provides access to file collections by extension.
// This is necessary to load external font maps, which are files (outside TeX syntax) that map TeX's
public class RequiredInputDirectory implements IInputWorkingDirectory, IFileCollector
{
private Map<String, Map<String, String>> _fileNames = new HashMap<String, Map<String, String>>();
public RequiredInputDirectory()
{
}
// This method should preliminarily be called for each file entry that is supposed to be located inside
// the required input directory. Inside is an example of how the dictionary of file names could be organized
// for easy collection of file names by extension.
// Here fileName is a full file name. This can be a file path on a file system, a URL, or whatever else (theoretically).
public void storeFileName(String fileName)
{
String extension = getExtension(fileName);
String name = getFileNameWithoutExtension(fileName);
Map<String, String> files = _fileNames.get(extension);
if (files == null)
_fileNames.put(extension, files = new HashMap<String, String>());
files.put(name, fileName);
}
// The IInputWorkingDirectory implementation.
public TeXInputStream getFile(String fileName, boolean searchSubdirectories)
{
return new TeXInputStream(null, fileName); // Here we actually return a stream for the file requested by its name.
}
// Here is how we gather file collections by extension.
public String[] getFileNamesByExtension(String extension)
{
return getFileNamesByExtension(extension, null);
}
// Here is how we gather file collections by extension.
public String[] getFileNamesByExtension(String extension, String path)
{
Map<String, String> files = _fileNames.get(extension);
if (files == null)
return new String[0];
return files.values().toArray(new String[0]);
}
private String getExtension(String fileName)
{
int pos = fileName.indexOf('.');
if (pos < 0)
return "";
return fileName.substring(pos);
}
private String getFileNameWithoutExtension(String fileName)
{
int pos = fileName.lastIndexOf('/');
if (pos >= 0)
fileName = fileName.substring(pos + 1);
pos = fileName.indexOf('.');
if (pos < 0)
return fileName;
return fileName.substring(0, pos);
}
public void close()
{
_fileNames.clear();
}
}
// Create conversion options instance.
...
// Create and assign saving options instance if needed.
...
// Set to true to make the device subset fonts used in the document.
options.getSaveOptions().subsetFonts(true);
// Create TeX engine options for no format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectIniTeX());
// Specify a file system working directory for the input.
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Run format creation.
TeXJob.createFormat("customtex", options);
// For further output to look fine.
options.getTerminalOut().getWriter().newLine();
// Create rendering options setting the image resolution to 150 dpi.
PngFigureRendererOptions options = new PngFigureRendererOptions();
options.setResolution(150);
// Specify the preamble.
options.setPreamble("\\usepackage{pict2e}");
// Specify the scaling factor 300%.
options.setScale(3000);
// Specify the background color.
options.setBackgroundColor(Color.WHITE);
// Specify the output stream for the log file.
options.setLogStream(new ByteArrayOutputStream());
// Specify whether to show the terminal output on the console or not.
options.showTerminal(true);
// Create the output stream for the figure image.
final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "text-and-formula.png");
try {
// Run rendering.
com.aspose.tex.Size2D size = new PngFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
"\\begin{picture}(6,5)\r\n" +
"\\thicklines\r\n" +
"\\put(1,0.5){\\line(2,1){3}} \\put(4,2){\\line(-2,1){2}} \\put(2,3){\\line(-2,-5){1}} \\put(0.7,0.3){$A$} \\put(4.05,1.9){$B$} \\put(1.7,2.95){$C$}\r\n" +
"\\put(3.1,2.5){$a$} \\put(1.3,1.7){$b$} \\put(2.5,1.05){$c$} \\put(0.3,4){$F=\\sqrt{s(s-a)(s-b)(s-c)}$} \\put(3.5,0.4){$\\displaystyle s:=\\frac{a+b+c}{2}$}\r\n" +
"\\end{picture}", stream, options);
// Show other results.
System.out.println(options.getErrorReport());
System.out.println();
System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
} finally {
if (stream != null)
stream.close();
}
// Create rendering options setting the image resolution 150 dpi.
PngMathRendererOptions options = new PngMathRendererOptions();
options.setResolution(150);
// Specify the preamble.
options.setPreamble("\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
// Specify the scaling factor 300%.
options.setScale(3000);
// Specify the foreground color.
options.setTextColor(Color.BLACK);
// Specify the background color.
options.setBackgroundColor(Color.WHITE);
// Specify the output stream for the log file.
options.setLogStream(new ByteArrayOutputStream());
// Specify whether to show the terminal output on the console or not.
options.showTerminal(true);
// Create the output stream for the formula image.
final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "math-formula.png");
try {
// Run rendering.
com.aspose.tex.Size2D size = MathRenderer.render("\\begin{equation*}\r\n" +
"e^x = x^{\\color{red}0} + x^{\\color{red}1} + \\frac{x^{\\color{red}2}}{2} + \\frac{x^{\\color{red}3}}{6} + \\cdots = \\sum_{n\\geq 0} \\frac{x^{\\color{red}n}}{n!}\r\n" +
"\\end{equation*}", stream, options);
// Show other results.
System.out.println(options.getErrorReport());
System.out.println();
System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
} finally {
if (stream != null)
stream.close();
}
// Create rendering options.
SvgFigureRendererOptions options = new SvgFigureRendererOptions();
// Specify the preamble.
options.setPreamble("\\usepackage{pict2e}");
// Specify the scaling factor 300%.
options.setScale(3000);
// Specify the background color.
options.setBackgroundColor(Color.WHITE);
// Specify the output stream for the log file.
options.setLogStream(new ByteArrayOutputStream());
// Specify whether to show the terminal output on the console or not.
options.showTerminal(true);
// Create the output stream for the figure image.
final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "text-and-formula.svg");
try {
// Run rendering.
com.aspose.tex.Size2D size = new SvgFigureRenderer().render("\\setlength{\\unitlength}{0.8cm}\r\n" +
"\\begin{picture}(6,5)\r\n" +
"\\thicklines\r\n" +
"\\put(1,0.5){\\line(2,1){3}} \\put(4,2){\\line(-2,1){2}} \\put(2,3){\\line(-2,-5){1}} \\put(0.7,0.3){$A$} \\put(4.05,1.9){$B$} \\put(1.7,2.95){$C$}\r\n" +
"\\put(3.1,2.5){$a$} \\put(1.3,1.7){$b$} \\put(2.5,1.05){$c$} \\put(0.3,4){$F=\\sqrt{s(s-a)(s-b)(s-c)}$} \\put(3.5,0.4){$\\displaystyle s:=\\frac{a+b+c}{2}$}\r\n" +
"\\end{picture}", stream, options);
// Show other results.
System.out.println(options.getErrorReport());
System.out.println();
System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
} finally {
if (stream != null)
stream.close();
}
// Create rendering options.
MathRendererOptions options = new SvgMathRendererOptions();
// Specify the preamble.
options.setPreamble("\\usepackage{amsmath}\r\n\\usepackage{amsfonts}\r\n\\usepackage{amssymb}\r\n\\usepackage{color}");
// Specify the scaling factor 300%.
options.setScale(3000);
// Specify the foreground color.
options.setTextColor(Color.BLACK);
// Specify the background color.
options.setBackgroundColor(Color.WHITE);
// Specify the output stream for the log file.
options.setLogStream(new ByteArrayOutputStream());
// Specify whether to show the terminal output on the console or not.
options.showTerminal(true);
// Create the output stream for the formula image.
final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "math-formula.svg");
try {
// Run rendering.
com.aspose.tex.Size2D size = new SvgMathRenderer().render("\\begin{equation*}\r\n" +
"e^x = x^{\\color{red}0} + x^{\\color{red}1} + \\frac{x^{\\color{red}2}}{2} + \\frac{x^{\\color{red}3}}{6} + \\cdots = \\sum_{n\\geq 0} \\frac{x^{\\color{red}n}}{n!}\r\n" +
"\\end{equation*}", stream, options, size);
// Show other results.
System.out.println(options.getErrorReport());
System.out.println();
System.out.println("Size: " + size.getWidth() + "x" + size.getHeight()); // Dimensions of the resulting image.
} finally {
if (stream != null)
stream.close();
}
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a file system working directory for the input.
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Specify the console as the output terminal.
options.setTerminalOut(new OutputConsoleTerminal()); // Default value. Arbitrary assignment.
// Specify a memory terminal as output terminal, if you don't want the terminal output to be written to the console.
// options.setTerminalOut(new OutputMemoryTerminal());
// Run the job.
TeXJob job = new TeXJob("hello-world", new XpsDevice(), options);
job.run();
// For further output to look fine.
options.getTerminalOut().getWriter().newLine(); // The same as System.out.println();
// Initialize license object.
License license = new License();
// Set license.
license.setLicense("D:\\Aspose.Total.Java.lic");
System.out.println("License set successfully.");
// Initialize license object.
License license = new License();
// Load license in FileStream.
InputStream myStream = new FileInputStream("D:\\Aspose.Total.Java.lic");
// Set license.
license.setLicense(myStream);
System.out.println("License set successfully.");
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a job name. Otherwise, the first argument of the TeXJob constructor will be taken as a job name.
options.setJobName("overridden-job-name");
// Specify a file system working directory for the input.
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.setTerminalOut(new OutputFileTerminal(options.getOutputWorkingDirectory()));
// Run the job.
TeXJob job = new TeXJob("hello-world", new XpsDevice(), options);
job.run();
// Open a stream on a ZIP archive that will serve as the input working directory.
final InputStream inZipStream = new FileInputStream(Utils.getInputDirectory() + "zip-in.zip");
try {
// Open a stream on a ZIP archive that will serve as the output working directory.
final OutputStream outZipStream = new FileOutputStream(Utils.getOutputDirectory() + "terminal-out-to-zip.zip");
try {
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a job name.
options.setJobName("terminal-output-to-zip");
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.setInputWorkingDirectory(new InputZipDirectory(inZipStream, "in"));
// Specify a ZIP archive working directory for the output.
options.setOutputWorkingDirectory(new OutputZipDirectory(outZipStream));
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.setTerminalOut(new OutputFileTerminal(options.getOutputWorkingDirectory()));
// Define the saving options.
options.setSaveOptions(new PdfSaveOptions());
// Run the job.
new TeXJob("hello-world", new PdfDevice(), options).run();
// Finalize output ZIP archive.
((OutputZipDirectory)options.getOutputWorkingDirectory()).finish();
}
finally {
outZipStream.close();
}
}
finally {
inZipStream.close();
}
// Set metered public and private keys.
new com.aspose.tex.Metered().setMeteredKey(
"<type public key here>",
"<type private key here>");
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a job name.
options.setJobName("stream-in-image-out");
// Specify a file system working directory for the input.
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Specify the console as the input terminal.
options.setTerminalIn(new InputConsoleTerminal()); // Default value. Arbitrary assignment.
// Specify the console as the output terminal.
options.setTerminalOut(new OutputConsoleTerminal()); // Default value. Arbitrary assignment.
// Define the saving options.
PngSaveOptions pngOptions = new PngSaveOptions();
pngOptions.setResolution(300);
options.setSaveOptions(pngOptions);
// Create the image device.
ImageDevice device = new ImageDevice();
// Run the job.
TeXJob job = new TeXJob(new ByteArrayInputStream(
"\\hrule height 10pt width 95pt\\vskip10pt\\hrule height 5pt".getBytes("ASCII")),
device, options);
job.run();
// When console prompts the input, type "ABC", press Enter, then type "\end" and press Enter again.
// For further output to look fine.
options.getTerminalOut().getWriter().newLine();
// You can alternatively get images in form of array of byte arrays.
// The first index for the page number (0-based, of course).
byte[][] result = device.getResult();
// Open a stream on a ZIP archive that will serve as the input working directory.
final InputStream inZipStream = new FileInputStream(Utils.getInputDirectory() + "zip-in.zip");
try {
// Open a stream on a ZIP archive that will serve as the output working directory.
final OutputStream outZipStream = new FileOutputStream(Utils.getOutputDirectory() + "typeset-pdf-to-external-stream.zip");
try {
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a job name.
options.setJobName("typeset-pdf-to-external-stream"); // does NOT define the name of the output PDF.
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.setInputWorkingDirectory(new InputZipDirectory(inZipStream, "in"));
// Specify a ZIP archive working directory for the output.
options.setOutputWorkingDirectory(new OutputZipDirectory(outZipStream));
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.setTerminalOut(new OutputFileTerminal(options.getOutputWorkingDirectory()));
// Define the saving options.
options.setSaveOptions(new PdfSaveOptions());
// Open a stream to write the output PDF to.
// 1) A file somewhere on a local file system.
final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + "file-name.pdf"); // writing PDF somewhere else
// 2) A file in the output ZIP. A weird feature that extends flexibilty :)
//final OutputStream stream = options.getOutputWorkingDirectory().getFile("file-name.pdf", new String[]{ null }); // writing PDF to the same ZIP
try {
new TeXJob("hello-world", new PdfDevice(stream), options).run();
}
finally {
stream.close();
}
// Finalize output ZIP archive.
((OutputZipDirectory)options.getOutputWorkingDirectory()).finish();
} finally {
outZipStream.close();
}
} finally {
inZipStream.close();
}
// Create the format provider using the file system input working directory.
// We use the project output directory as our custom format file is supposed to be located there.
final FormatProvider formatProvider = new FormatProvider(
new InputFileSystemDirectory(Utils.getOutputDirectory()), "customtex");
try {
// Create conversion options for a custom format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX(formatProvider));
options.setJobName("typeset-with-custom-format");
// Specify the input working directory. This is not required here as we are providing the main input as a stream.
// But it is required when the main input has dependencies (e.g. images).
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Run the job.
new TeXJob(new ByteArrayInputStream(
"Congratulations! You have successfully typeset this text with your own TeX format!\\end".getBytes("ASCII")),
new XpsDevice(), options).run();
// For further output to look fine.
options.getTerminalOut().getWriter().newLine();
} finally {
formatProvider.close();
}
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a job name.
options.setJobName("external-file-stream");
// Specify a file system working directory for the input.
options.setInputWorkingDirectory(new InputFileSystemDirectory(Utils.getInputDirectory()));
// Specify a file system working directory for the output.
options.setOutputWorkingDirectory(new OutputFileSystemDirectory(Utils.getOutputDirectory()));
// Specify that the terminal output must be written to a file in the output working directory.
// The file name is <job_name>.trm.
options.setTerminalOut(new OutputFileTerminal(options.getOutputWorkingDirectory()));
// Open the stream to write typeset XPS document. The file name is not necessarily the same as the job name.
final OutputStream stream = new FileOutputStream(Utils.getOutputDirectory() + options.getJobName() + ".xps");
try {
// Run the job.
new TeXJob("hello-world", new XpsDevice(stream), options).run();
}
finally {
stream.close();
}
// Open the stream on the ZIP archive that will serve as the input working directory.
final InputStream inZipStream = new FileInputStream(Utils.getInputDirectory() + "zip-in.zip");
try {
// Open the stream on the ZIP archive that will serve as the output working directory.
final OutputStream outZipStream = new FileOutputStream(Utils.getOutputDirectory() + "zip-pdf-out.zip");
try {
// Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
TeXOptions options = TeXOptions.consoleAppOptions(TeXConfig.objectTeX());
// Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.setInputWorkingDirectory(new InputZipDirectory(inZipStream, "in"));
// Specify a ZIP archive working directory for the output.
options.setOutputWorkingDirectory(new OutputZipDirectory(outZipStream));
// Specify the console as the output terminal.
options.setTerminalOut(new OutputConsoleTerminal()); // Default value. Arbitrary assignment.
// Define the saving options.
options.setSaveOptions(new PdfSaveOptions());
// Run the job.
TeXJob job = new TeXJob("hello-world", new PdfDevice(), options);
job.run();
// For further output to look fine.
options.getTerminalOut().getWriter().newLine();
// Finalize output ZIP archive.
((OutputZipDirectory)options.getOutputWorkingDirectory()).finish();
} finally {
outZipStream.close();
}
} finally {
inZipStream.close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment