How to Convert Text Files to PDF using File Conversion API in Python

Convert Text Files to PDF using File Conversion API in Python

Notepad is windows text editor and word processing program to create quick notes in a text file while PDFs are one of the most important and widely used digital media. Converting text or txt file to PDF document is one of the basic requirements in real life. Online Text to PDF is used to present and exchange documents reliably, independent of software, or operating system. To convert TXT files to PDF programmatically, this article demonstrates how to convert Text files to PDF using file conversion API in Python.

The following topics shall be covered in this article:

Text to PDF Conversion REST API and Python SDK

For converting Text to PDF, we will be using the Python SDK of GroupDocs.Conversion Cloud API. It is a platform independent documents and images conversion solution. It allows you to quickly and reliably convert images and documents of any supported file format to any format you need.

You can install GroupDocs.Conversion Cloud to your Python application using the following command in the console:

pip install groupdocs_conversion_cloud

Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:

# Load Python SDK http://api.groupdocs.cloud in your python application
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get different configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
my_storage = "LocalStorage"

How to Convert Text to PDF using REST API in Python

You can convert your text files to PDF programmatically on the cloud by following the simple steps given below:

  1. Upload the TXT file to the cloud
  2. Convert Text to PDF using Python
  3. Download the converted PDF file

Upload the TXT File

Firstly, upload the text file to the cloud using the following code sample:

# Upload sample .txt file on the cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-text-file.txt", "H:\\groupdocs-cloud-data\\sample-text-file.txt", my_storage)
# Upload sample text file
response = file_api.upload_file(request)

As a result, the uploaded text file will be available in the files section of your dashboard on the cloud.

Convert TXT to PDF using Python

You can easily convert TXT to PDF document programmatically by following the steps mentioned below:

  • Firstly, create an instance of the ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, provide the input text file path
  • Set output file format as the “pdf”
  • Next, provide the output file path
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert text file by calling the convert_document() method with ConvertDocumentRequest.

The following code example shows how to convert TEXT to PDF using REST API in Python:

# How to Convert Text to PDF using REST API in Python
try:
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert .txt file to PDF document
result = convert_api.convert_document(request)
print("TXT File converted to PDF successfully: " + result[0].path)
except groupdocs_conversion_cloud.ApiException as e:
print("Exception when calling convert_document: {0}".format(e.message))
How to Convert Text to PDF using REST API in Python.

Convert Text to PDF using REST API in Python.

Download the Converted File

The above code sample will save the converted PDF file on the cloud. You can download it using the following code sample:

# API initialization to download converted file
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-text-file.pdf", my_storage)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")

Convert Text to PDF with Advanced Options in Python

You can convert text documents to PDF files using advanced settings by following the steps given below:

  • Firstly, create an instance of the ConvertApi.
  • Now, create an instance of the ConvertSettings.
  • Then, provide the text file path.
  • Next, set the “pdf” as format.
  • Now, provide the output file path.
  • Now, create an instance of the TextLoadOptions
  • Optionally set various load options such as encoding etc.
  • Now, create an instance of the PdfConvertOptions
  • Then, set various convert options such as center_window, display_doc_title, margins (top, left, right, bottom), etc.
  • Next, set convert_options value with pdf convertOptions
  • Now, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert text by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert text file to PDF document using advanced options. Please follow the steps mentioned earlier to upload and download files from the cloud:

# Convert Text to PDF using Advanced Options in Python
# Create an instance of the API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# Text load options
loadOptions = groupdocs_conversion_cloud.TxtLoadOptions()
loadOptions.encoding = "shift_jis"
# Set PDF convert options
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.center_window = True
convertOptions.display_doc_title = True
convertOptions.dpi = 1024.0
convertOptions.fit_window = False
convertOptions.grayscale = False
convertOptions.linearize = False
convertOptions.margin_top = 5
convertOptions.margin_left = 5
convertOptions.unembed_fonts = True
convertOptions.remove_pdfa_compliance = False
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert pages of text file to PDF file
result = convert_api.convert_document(request)
print("Successfully Converted TXT to PDF with advanced options: " + result[0].path)

Convert Range of Pages from Text to PDF in Python

You can convert a range of pages from text file to PDF file programmatically by following the steps mentioned below:

  • Firstly, create an instance of the ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, provide the input text file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Now, create an instance of the PdfConvertOptions
  • Then, provide a page range to convert from start page number and total pages to convert
  • Now, assign _PdfConvertOptions_ to ConvertSettings
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert by calling the convert_document() method with ConvertDocumentRequest

The following code sample shows how to convert a range of pages from TXT to PDF document using REST API in Python. Please follow the steps mentioned earlier to upload and download resultant pdf file:

# How to Convert Range of Pages from Text to PDF in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# PDF convert options: start page number and total pages to convert
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.from_page = 1
convertOptions.pages_count = 2
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert pages of text file to PDF file
result = convert_api.convert_document(request)
print("Converted range of pages from Text file to PDF: " + result[0].path)

Convert Specific Pages of Text to PDF in Python

You can convert specific pages of a text document to a PDF file programmatically by following the steps mentioned below:

  • Firstly, create an instance of the ConvertApi
  • Now, create an instance of the ConvertSettings
  • Then, provide the input text file path
  • Assign “pdf” to the format
  • Provide the output file path
  • Now, create an instance of the PdfConvertOptions
  • Then, provide specific page numbers in a comma-separated array to convert
  • Now, assign _PdfConvertOptions_ to ConvertSettings
  • Then, create ConvertDocumentRequest with ConvertSettings
  • Finally, convert by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert specific pages of text file to PDF using REST API in Python. Please follow the steps mentioned earlier to upload and download output pdf file:

# How to Convert Specific Pages of Text to PDF in Python
# Create an instance of API
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-text-file.txt"
settings.format = "pdf"
settings.output_path = "python-testing"
# PDF convert options: page numbers to convert
convertOptions = groupdocs_conversion_cloud.PdfConvertOptions()
convertOptions.pages = [1, 3]
settings.convert_options = convertOptions
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert text file to PDF file
result = convert_api.convert_document(request)
print("Successfully converted Text file pages to PDF: " + result[0].path)

Try Online

Do you want to convert text to pdf online? Please try the following free text to pdf converter online, which is developed using the above API. You can easily convert text to pdf online free using this text to pdf maker online.

Summing up

In this article, you have learned:

  • how to convert plain text to PDF documents on the cloud;
  • how to programmatically upload the text file using python;
  • how to download the converted PDF file from the cloud in python;
  • how to convert specific pages or a range of pages from text notepad to PDF in Python;

Besides, you can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Ask a question

If you have any questions about how to change text file to pdf, please feel free to ask in Free Support Forum and it will be answered within a few hours.

See Also