乳胶到PDF | Aspose.TeX for Python

将乳胶转换为PDF

现在,让我们研究Python代码,该代码提供了将乳胶转换为PDF格式的最直接方法。

1# Create conversion options for Object LaTeX format upon Object TeX engine extension.
2options = TeXOptions.console_app_options(TeXConfig.object_latex)
3# Specify a file system working directory for the output.
4options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
5# Initialize the options for saving in PDF format.
6options.save_options = PdfSaveOptions()
7# Run LaTeX to PDF conversion.
8TeXJob(path.join(Util.input_directory, "hello-world.ltx"), PdfDevice(), options).run()

首先,我们需要创建一个 Texoptions类的实例。 console_app_options()方法是实现此目的的唯一静态方法,因此不必担心其名称。此方法采用 texconfig类的 Object_Latex实例,该实例非常适合转换 latex文件。该配置指示对象Tex引擎加载对象乳胶格式,并准备好处理乳胶文件。对象乳胶格式与 latex格式基本相同,但它利用 object tex特定的原始图来定义页面指标。

第一个需要指定的选项是 output_working_directory,它决定了 TeX 输出的保存位置。如有需要,您可以在 Aspose.TeX 中找到更多关于输出目录概念的信息。在本例中,我们使用 OutputFileSystemDirectory 类,它使我们能够将输出写入指定的目录或文件夹。

第二个选项需要 SaveOptions类的实例,该实例确定了 对象模型的方式如何转换为所需的格式。在我们的情况下,当我们将乳胶转换为PDF时,它是指 PDFSaveOptions类的实例。

要转换存储在文件系统中的乳胶文件,我们需要使用 constructor将文件的完整路径作为参数创建 texjob类的实例。如果我们没有指定完整的路径,则引擎将在当前目录中查找文件(这是脚本的工作目录),并且可能找不到它。但是,如果我们的文件具有“ .tex”扩展名,我们可以省略扩展名,因为引擎将自动将其附加。构造函数的第二个参数是 设备类的实例。在我们的情况下,由于我们将乳胶转换为pdf,因此是 pdfdevice类的一个实例。最后,我们将最近准备的转换选项作为最后一个参数。

现在,剩下的就是 运行工作。

执行后,无论它是否成功,我们将看到的第一件事是终端输出。如果执行成功,输出将看起来与此相似:

 1This is ObjectTeX, Version 3.1415926-1.0 (Aspose.TeX 21.8)
 2entering extended mode
 3
 4(<input_directory>\hello-world.ltx
 5LaTeX2e <2011/06/27>
 6(article.cls
 7Document Class: article 2007/10/19 v1.4h Standard LaTeX document class
 8(size10.clo))
 9No file hello-world.aux.
10[1]
11(<output_directory>\hello-world.aux) )
12Output written on hello-world.pdf (1 page).
13Transcript written on hello-world.log.

在我们指定为输出目录的文件夹中,我们将找到成绩单文件,它是!,主要输出PDF文件以及引擎工作的其他结果。

编写主输出PDF文件的另一种方法

 1# Create the stream to write the PDF file to.
 2with open(path.join(Util.output_directory, "any-name.pdf"), "wb") as pdf_stream:
 3    # Create conversion options for Object LaTeX format upon Object TeX engine extension.
 4    options = TeXOptions.console_app_options(TeXConfig.object_latex)
 5    # Specify a file system working directory for the output.
 6    options.output_working_directory = OutputFileSystemDirectory(Util.output_directory)
 7    # Initialize the options for saving in PDF format.
 8    options.save_options = PdfSaveOptions()
 9    # Run LaTeX to PDF conversion.
10    TeXJob(path.join(Util.input_directory, "hello-world.ltx"), PdfDevice(pdf_stream), options).run()

主输出PDF文件将命名为任何name.pdf,并将位于指定目录中。但是,与 图像输出不同,转换选项指定的输出目录中没有其他PDF文件。例外是使用 outputfilesystemdirectory分配给 *output_working_directory *选项的同一文件系统目录

关于输入选项

如果我们的主要输入文件需要依赖项,例如未包含在基本乳胶系统和受支持的软件包中的软件包,则有必要以类似的方式设置 queld_input_directory选项,因为我们设置 output_working_directory选项,并将依赖项放在该目录中。依赖项可以根据需要在子目录中组织。此外,如果我们有自己的文件要包含在排版过程中,例如外部图形文件,我们还必须使用通往存储这些文件的位置的路径来设置 input_working_directory。我们还可以将主输入文件放置在输入目录内,并在run()方法中指定相对路径,或者如果主输入文件在root中,则根本没有指定路径。有关python aspose.tex中输入目录概念的更多详细信息以及提供的实现,请参阅 此处

也讨论了 其他TEX的工作选择

您还可以探索免费的 用于 LaTeX 到 PDF 转换的 Web 应用程序,它是使用 Aspose.TeX for .NET API 开发的。

Have any questions about Aspose.TeX?



Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.