Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 14, 2024 02:57
Show Gist options
  • Save aspose-com-gists/68d217858f0be40062562ce2bb2cd908 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/68d217858f0be40062562ce2bb2cd908 to your computer and use it in GitHub Desktop.
Aspose.TeX for Python via .NET
Gists for Aspose.TeX for Python via .NET
# Create conversion options instance.
...
# Force the TeX engine to output the specified date in the title.
options.date_time = datetime(2022, 12, 18)
# Create conversion options instance.
...
# Set to true to make the engine skip missing packages (when your file references one) without errors.
options.ignore_missing_packages = True
# Create conversion options instance.
...
# Specify the console as the input terminal.
options.terminal_in = InputConsoleTerminal() # Default. Arbitrary assignment.
# Create conversion options instance.
...
# Specify a file system working directory for the input.
options.input_working_directory = InputFileSystemDirectory(Utils.input_directory)
# Create conversion options instance.
...
# Create some device.
...
# Run LaTeX to XPS conversion.
TeXJob(BytesIO(r"\documentclass{article} \begin{document} Hello, World! \end{document}".encode("ascii")),
XpsDevice(), options).run()
# Open the stream for the ZIP archive that will serve as the input working directory.
with open(path.join(Utils.input_directory, "zip-in.zip")) as in_zip_stream:
# Create conversion options instance.
...
# Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.input_working_directory = InputZipDirectory(in_zip_stream, "in")
# Create conversion options instance.
...
# Set interaction mode.
options.interaction = Interaction.NONSTOP_MODE
# Create conversion options instance.
...
# Set the job name.
options.job_name = "my-job-name"
# Create conversion options instance.
...
# Initialize the options for saving in BMP format.
options.save_options = BmpSaveOptions()
# Create conversion options instance.
...
# Initialize the options for saving in JPEG format.
options.save_options = JpegSaveOptions()
# Create the stream to write the PDF file to.
with open(path.join(Util.output_directory, "any-name.pdf"), "wb") as pdf_stream:
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in PDF format.
options.save_options = PdfSaveOptions()
# Run LaTeX to PDF conversion.
TeXJob(path.join(Util.input_directory, "hello-world.ltx"), PdfDevice(pdf_stream), options).run()
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in PDF format.
options.save_options = PdfSaveOptions()
# Run LaTeX to PDF conversion.
TeXJob(path.join(Util.input_directory, "hello-world.ltx"), PdfDevice(), options).run()
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in PNG format.
so = PngSaveOptions()
so.device_writes_images = False # Run LaTeX to PNG conversion.
options.save_options = so
device = ImageDevice(True)
TeXJob(path.join(Util.input_directory, "hello-world.ltx"), device, options).run()
# Save pages file by file.
for i in range(len(device.result)):
with open(path.join(Util.output_directory, f"page-{(i + 1)}" + ".png"), "wb") as fs:
fs.write(device.result[i][0:len(device.result[i])])
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in PNG format.
options.save_options = PngSaveOptions()
# Run LaTeX to PNG conversion.
TeXJob(path.join(Util.input_directory, "hello-world.ltx"), ImageDevice(True), options).run()
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in SVG format.
options.save_options = SvgSaveOptions()
# Run LaTeX to SVG conversion.
TeXJob(path.join(Util.input_directory, "hello-world.ltx"), SvgDevice(), options).run()
# Create conversion options instance.
...
# Initialize the options for saving in TIFF format.
options.save_options = TiffSaveOptions()
# Create the stream to write the XPS file to.
with open(path.join(Util.output_directory, "any-name.xps"), "wb") as xps_stream:
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in XPS format.
options.save_options = XpsSaveOptions() # Default value. Arbitrary assignment.
# Run LaTeX to XPS conversion.
TeXJob(path.join(Util.input_directory, "hello-world.ltx"), XpsDevice(xps_stream), options).run()
# Create conversion options instance.
...
# Initialize the options for saving in XPS format.
so = XpsSaveOptions()
options.save_options = so # Default value. Arbitrary assignment.
# Run LaTeX to XPS conversion.
TeXJob(path.join(Util.input_directory, "sample.ltx"), XpsDevice(), options).run()
# Create conversion options instance.
...
# Run LaTeX to XPS conversion. When prompted, enter the /-separated path to the LaTeX file.
TeXJob(XpsDevice(), options).run()
# Create conversion options instance.
...
# Set to true to make the engine not construct ligatures where normally it would.
options.no_ligatures = True
# Create conversion options instance.
...
# Specify the console as the input terminal.
options.terminal_out = OutputConsoleTerminal() # Default value. Arbitrary assignment.
# Create conversion options instance.
...
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Utils.output_directory)
# 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.terminal_out = OutputFileTerminal(options.output_working_directory)
# Open the stream for the ZIP archive that will serve as the output working directory.
with open(path.join(Utils.output_directory, "zip-pdf-out.zip") as out_zip_stream:
# Create conversion options instance.
...
# Specify a ZIP archive working directory for the output.
options.output_working_directory = OutputZipDirectory(out_zip_stream)
# 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.
so.rasterize_formulas = 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.
so.rasterize_included_graphics = 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.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Specify a file system working directory for the required input.
# The directory containing packages may be located anywhere.
options.required_input_directory = InputFileSystemDirectory(path.join(Util.input_directory, "packages"))
# Initialize the options for saving in PNG format.
options.save_options = PngSaveOptions()
# Run LaTeX to PNG conversion.
TeXJob(path.join(Util.input_directory, "required-input-fs.tex"), ImageDevice(True), options).run()
# Create conversion options for Object LaTeX format upon Object TeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_latex)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Initialize the options for saving in PNG format.
options.save_options = PngSaveOptions()
# Create a file stream for the ZIP archive containing the required package.
# The ZIP archive may be located anywhere.
with open(path.join(Util.input_directory, "packages\\pgfplots.zip"), "rb") as zip_stream:
# Specify a ZIP working directory for the required input.
options.required_input_directory = InputZipDirectory(zip_stream, "")
# Run LaTeX to PNG conversion.
TeXJob(path.join(Util.input_directory, "required-input-zip.tex"), ImageDevice(True), options).run()
# 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.
so.subset_fonts = True
# Create TeX engine options for no format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_ini_tex)
# Specify a file system working directory for the input.
options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Run format creation.
TeXJob.create_format("customtex", options)
# For further output to look fine.
options.terminal_out.writer.write_line()
# Create rendering options setting the image resolution to 150 dpi.
options = PngFigureRendererOptions()
options.resolution = 150 # Specify the preamble.
options.preamble = r"\usepackage{pict2e}"
# Specify the scaling factor 300%.
options.scale = 3000
# Specify the background color.
options.background_color = Color.white
# Specify the output stream for the log file.
options.log_stream = BytesIO()
# Specify whether to show the terminal output on the console or not.
options.show_terminal = True
# Create the output stream for the figure image.
with open(path.join(Util.output_directory, "text-and-formula.png"), "wb") as stream:
# Run rendering.
size = PngFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
\begin{picture}(6,5)
\thicklines
\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$}
\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}$}
\end{picture}""", stream, options)
# Show other results.
print(options.error_report)
print()
print(f"Size: {size.width}x{size.height}")
# Create rendering options setting the image resolution to 150 dpi.
options = PngMathRendererOptions()
options.resolution = 150 # Specify the preamble.
options.preamble = r"""\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}"""
# Specify the scaling factor 300%.
options.scale = 3000
# Specify the foreground color.
options.text_color = Color.black
# Specify the background color.
options.background_color = Color.white
# Specify the output stream for the log file.
options.log_stream = BytesIO()
# Specify whether to show the terminal output on the console or not.
options.show_terminal = True
# Create the output stream for the formula image.
with open(path.join(Util.output_directory, "math-formula.png"), "wb") as stream:
# Run rendering.
size = PngMathRenderer().render(r"""\begin{equation*}
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!}
\end{equation*}""", stream, options)
# Show other results.
print(options.error_report)
print()
print(f"Size: {size.width}x{size.height}")
# Create rendering options.
options = SvgFigureRendererOptions()
# Specify the preamble.
options.preamble = r"\usepackage{pict2e}"
# Specify the scaling factor 300%.
options.scale = 3000
# Specify the background color.
options.background_color = Color.white
# Specify the output stream for the log file.
options.log_stream = BytesIO()
# Specify whether to show the terminal output on the console or not.
options.show_terminal = True
# Create the output stream for the figure image.
with open(path.join(Util.output_directory, "text-and-formula.svg"), "wb") as stream:
# Run rendering.
size = SvgFigureRenderer().render(r"""\setlength{\unitlength}{0.8cm}
\begin{picture}(6,5)
\thicklines
\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$}
\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}$}
\end{picture}""", stream, options)
# Show other results.
print(options.error_report)
print()
print(f"Size: {size.width}x{size.height}")
# Create rendering options.
options = SvgMathRendererOptions()
# Specify the preamble.
options.preamble = r"""\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{color}"""
# Specify the scaling factor 300%.
options.scale = 3000
# Specify the foreground color.
options.text_color = Color.black
# Specify the background color.
options.background_color = Color.white
# Specify the output stream for the log file.
options.log_stream = BytesIO()
# Specify whether to show the terminal output on the console or not.
options.show_terminal = True
# Create the output stream for the formula image.
with open(path.join(Util.output_directory, "math-formula.svg"), "wb") as stream:
# Run rendering.
size = SvgMathRenderer().render(r"""\begin{equation*}
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!}
\end{equation*}""", stream, options)
# Show other results.
print(options.error_report)
print()
print(f"Size: {size.width}x{size.height}")
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a file system working directory for the input.
options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Specify the console as the output terminal.
options.terminal_out = 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.TerminalOut = new OutputMemoryTerminal();
# Run the job.
job = TeXJob("hello-world", XpsDevice(), options)
job.run()
# For further output to look fine.
options.terminal_out.writer.write_line() # The same as print()
license = License()
# Set license.
license.set_license(Util.license_path)
print('License set successfully.')
license = License()
# Load license in FileStream.
with open(Util.license_path, "rb") as my_stream:
# Set license.
license.set_license(my_stream)
print('License set successfully.')
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a job name. Otherwise, the first argument of the TeXJob constructor will be taken as a job name.
options.job_name = "overridden-job-name"
# Specify a file system working directory for the input.
options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Specify that the terminal output must be written to a file in the output working directory.
# The file name is <job_name>.trm.
options.terminal_out = OutputFileTerminal(options.output_working_directory)
# Run the job.
job = TeXJob("hello-world", XpsDevice(), options)
job.run()
# Open a stream on a ZIP archive that will serve as the input working directory.
with open(path.join(Util.input_directory, "zip-in.zip"), "rb") as in_zip_stream:
# Open a stream on a ZIP archive that will serve as the output working directory.
with open(path.join(Util.output_directory, "terminal-out-to-zip.zip"), "wb") as out_zip_stream:
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a job name.
options.job_name = "terminal-output-to-zip"
# Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.input_working_directory = InputZipDirectory(in_zip_stream, "in")
# Specify a ZIP archive working directory for the output.
options.output_working_directory = OutputZipDirectory(out_zip_stream)
# Specify that the terminal output must be written to a file in the output working directory.
# The file name is <job_name>.trm.
options.terminal_out = OutputFileTerminal(options.output_working_directory)
# Define the saving options.
options.save_options = PdfSaveOptions()
# Run the job.
TeXJob("hello-world", PdfDevice(), options).run()
# Finalize output ZIP archive.
options.output_working_directory.finish()
# Set metered public and private keys.
Metered().set_metered_key('<type public key here>', '<type private key here>')
print('License set successfully.')
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a job name.
options.job_name = "stream-in-image-out"
# Specify a file system working directory for the input.
options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Specify the console as the input terminal.
options.terminal_in = InputConsoleTerminal() # Default value. Arbitrary assignment.
# Specify the console as the output terminal.
options.terminal_out = OutputConsoleTerminal() # Default value. Arbitrary assignment.
# Define the saving options.
so = PngSaveOptions()
options.save_options = so
so.resolution = 300 # Create the image device.
device = ImageDevice(True)
# Run the job.
job = TeXJob(BytesIO("\\hrule height 10pt width 95pt\\vskip10pt\\hrule height 5pt".encode('ascii')), device, options)
job.run()
# When the console prompts the input, type "ABC", press Enter, then type "\end" and press Enter again.
# For further output to look fine.
options.terminal_out.writer.write_line()
# You can alternatively get images in form of array of byte arrays.
# The first index for the page number (0-based, of course).
result = device.result
# with open(path.join(Util.output_directory, 'image.png'), 'wb') as stream:
# stream.write(result[0])
# Open a stream on a ZIP archive that will serve as the input working directory.
with open(path.join(Util.input_directory, "zip-in.zip"), "rb") as in_zip_stream:
# Open a stream on a ZIP archive that will serve as the output working directory.
with open(path.join(Util.output_directory, "typeset-pdf-to-external-stream.zip"), "wb") as out_zip_stream:
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a job name.
options.job_name = "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.input_working_directory = InputZipDirectory(in_zip_stream, "in")
# Specify a ZIP archive working directory for the output.
options.output_working_directory = OutputZipDirectory(out_zip_stream)
# Specify that the terminal output must be written to a file in the output working directory.
# The file name is <job_name>.trm.
options.terminal_out = OutputFileTerminal(options.output_working_directory)
# Define the saving options.
options.save_options = PdfSaveOptions()
# Open a stream to write the output PDF to.
# 1) A file somewhere on a local file system.
with open(path.join(Util.output_directory, "file-name.pdf"), "wb") as stream:
# 2) A file in the output ZIP. A weird feature that extends flexibility :)
# with options.output_working_directory.get_output_file("file-name.pdf").stream as stream: # writing PDF to the same ZIP
TeXJob("hello-world", PdfDevice(stream), options).run()
# Finalize output ZIP archive.
options.output_working_directory.finish()
# 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.
with FormatProvider(InputFileSystemDirectory(Util.output_directory), "customtex") as format_provider:
# Create conversion options for a custom format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex(format_provider))
options.job_name = "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.input_working_directory = InputFileSystemDirectory(Util.input_directory)
# Specify a file system working directory for the output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Run the job.
TeXJob(BytesIO("Congratulations! You have successfully typeset this text with your own TeX format!\\end".encode('ascii')),
XpsDevice(), options).run()
# For further output to look fine.
options.terminal_out.writer.write_line()
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a job name.
options.job_name = "external-file-stream"
# Specify a file system working directory the for input.
options.input_working_directory = InputFileSystemDirectory(Util.input_directory)
# Specify a file system working directory the for output.
options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
# Specify that the terminal output must be written to a file in the output working directory.
# The file name is <job_name>.trm.
options.terminal_out = OutputFileTerminal(options.output_working_directory)
# Open the stream to write typeset XPS document. The file name is not necessarily the same as the job name.
with open(path.join(Util.output_directory, options.job_name + ".xps"), "wb") as stream:
# Run the job.
TeXJob("hello-world", XpsDevice(stream), options).run()
# Open the stream on the ZIP archive that will serve as the input working directory.
with open(path.join(Util.input_directory, "zip-in.zip"), "rb") as in_zip_stream:
# Open the stream on the ZIP archive that will serve as the output working directory.
with open(path.join(Util.output_directory, "zip-pdf-out.zip"), "wb") as out_zip_stream:
# Create conversion options for default ObjectTeX format upon ObjectTeX engine extension.
options = TeXOptions.console_app_options(TeXConfig.object_tex())
# Specify a ZIP archive working directory for the input. You can also specify a path inside the archive.
options.input_working_directory = InputZipDirectory(in_zip_stream, "in")
# Specify a ZIP archive working directory for the output.
options.output_working_directory = OutputZipDirectory(out_zip_stream)
# Specify the console as the output terminal.
options.terminal_out = OutputConsoleTerminal() # Default value. Arbitrary assignment.
# Define the saving options.
options.save_options = PdfSaveOptions()
# Run the job.
job = TeXJob("hello-world", PdfDevice(), options)
job.run()
# For further output to look fine.
options.terminal_out.writer.write_line()
# Finalize output ZIP archive.
options.output_working_directory.finish()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment