convert latex to pdf

LaTeX is a powerful tool for creating complex documents, especially in science and math. In this tutorial, we’ll learn how to convert LaTeX documents into PDFs using Python. We’ll use the Python LaTeX Converter REST API, making it easy to turn your LaTeX work into neatly formatted PDFs. By following these steps, you’ll quickly master the process of converting your LaTeX documents to PDFs with Python. Let’s begin!

Prerequisites:

Before we begin, please make sure you have the following requirements prepared:

  1. Python installed on your machine (version 3.x is recommended).
  2. GroupDocs.Conversion Cloud SDK for Python installed. You can also find installation instructions in the official GroupDocs.Conversion Cloud documentation.

Step 1: Configure the Python LaTeX Converter SDK

To get started, add GroupDocs.Conversion Cloud to your Python project using pip (package installer for Python) by running the following command in your console:

pip install groupdocs_conversion_cloud

Step 2: Launch the API Client

Now, retrieve your Client ID and Client Secret from the dashboard, and incorporate the provided code as demonstrated below:

import groupdocs_conversion_cloud
# Get app_sid & app_key from https://dashboard.groupdocs.cloud after free registration.
app_sid = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
app_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations.
configuration = groupdocs_conversion_cloud.Configuration(app_sid, app_key)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "LocalStorage"

Step 3: Upload the LaTeX File

To begin, upload the LaTeX document to the cloud using the following code example:

# Create an instance of the file API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\input-sample-file.tex", "H:\\groupdocs-cloud-data\\input-sample-file.tex", storage_name)
# Upload file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
view raw upload-file.py hosted with ❤ by GitHub

As a result, the LaTeX file you uploaded will be accessible in the files section of your cloud dashboard.

Step 4: LaTeX to PDF Conversion using Python

To convert Tex to PDF, please follow the steps given below:

  1. Create a ConvertApi instance using your credentials.
  2. Set the file path to LaTeX/Sample.tex and choose the output format (PDF).
  3. Configure additional conversion options, including start page, page count, and fixed layout with borders.
  4. Execute the conversion using the convert_document method, storing the result in the result variable.

The code below demonstrates how to use a LaTeX Converter REST API to convert your LaTeX document into PDF format.

import groupdocs_conversion_cloud
client_id = "XXXX-XXXX-XXXX-XXXX" = Get ClientId and ClientSecret from https://dashboard.groupdocs.cloud
client_secret = "XXXXXXXXXXXXXXXX" = Get ClientId and ClientSecret from https://dashboard.groupdocs.cloud
# Create necessary API instances
apiInstance = groupdocs_conversion_cloud.ConvertApi.from_keys(Common.client_id, Common.client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "LaTex/Sample.tex"
settings.format = "pdf"
convertOptions = groupdocs_conversion_cloud.WebConvertOptions()
convertOptions.from_page = 1
convertOptions.pages_count = 1
convertOptions.fixed_layout = True
convertOptions.fixed_layout_show_borders = True
settings.convert_options = convertOptions
settings.output_path = "converted"
# Convert
result = apiInstance.convert_document(groupdocs_conversion_cloud.ConvertDocumentRequest(settings))

Step 5: Download PDF File

The code provided in the prior step stores the converted PDF file in the cloud. To retrieve and download it, you can utilize the following code snippet.

# Create instance of the API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
request = groupdocs_conversion_cloud.DownloadFileRequest("converted/sample.pdf", my_storage)
response = file_api.download_file(request)
# Move downloaded file to your working directory
shutil.move(response, "C:\\Files\\")

Conclusion

In this blog post, we’ve outlined a step-by-step guide for converting LaTeX documents to PDF using the GroupDocs.Conversion Cloud SDK for Python. By adhering to these instructions, you can seamlessly incorporate LaTeX to PDF conversion capabilities into your Python applications.

Additionally, you can explore the GroupDocs.Conversion Cloud API further by referring to our comprehensive documentation. We offer an API reference section that allows you to interact with and visualize our APIs directly through your web browser. The complete source code for the Python SDK is openly accessible on GitHub.

Lastly, we continually publish fresh blog articles covering various file formats and parsing techniques using our REST API. Feel free to reach out for the most recent updates. Happy coding!

Free Online LaTeX Converter

For a free online LaTeX to PDF conversion, you can experiment with an online LaTeX converter app. This app is built using the converter REST API mentioned earlier.

Ask a question

If you have any questions or concerns regarding the LaTeX converter, don’t hesitate to reach out to us through our forum. We’re here to assist you.

See Also

Below, you’ll find some related articles that could prove useful: