How to Convert PNG to PDF in Python

Learn how to use Python to convert PNG images into PDF documents by calling the Convert to PDF API Tool from pdfRest.
Share this page

Why Convert PNG to PDF with Python?

The pdfRest Convert to PDF API Tool offers a convenient way to convert various file types to PDF. This tutorial will guide you through the process of sending an API call to Convert to PDF using Python.

This can be particularly useful in scenarios where you need to standardize document formats, such as converting invoices, reports, or images to PDF for archival, sharing, or printing purposes. The below example will show how to convert a PNG file to a PDF document.

Convert PNG to PDF with Python Code Example

from requests_toolbelt import MultipartEncoder
import requests
import json

pdf_endpoint_url = 'https://api.pdfrest.com/pdf'

mp_encoder_pdf = MultipartEncoder(
    fields={
        'file': ('file_name.png', open('/path/to/file', 'rb'), 'image/png'),
        'output' : 'example_pdf_out',
    }
)

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

print("Sending POST request to pdf endpoint...")
response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)

print("Response status code: " + str(response.status_code))

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

Reference: pdfRest API Python samples on GitHub.

Breaking Down the Code

Let's break down each part of the code:

from requests_toolbelt import MultipartEncoder
import requests
import json

This imports the necessary libraries. MultipartEncoder is used for creating multipart/form-data payloads for the request.

pdf_endpoint_url = 'https://api.pdfrest.com/pdf'

This sets the API endpoint URL for the Convert to PDF service.

mp_encoder_pdf = MultipartEncoder(
    fields={
        'file': ('file_name.png', open('/path/to/file', 'rb'), 'image/png'),
        'output' : 'example_pdf_out',
    }
)

This creates a MultipartEncoder object with the file to convert and the desired output filename.

headers = {
    'Accept': 'application/json',
    'Content-Type': mp_encoder_pdf.content_type,
    'Api-Key': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
}

Headers include the API key for authentication and content type for the request.

response = requests.post(pdf_endpoint_url, data=mp_encoder_pdf, headers=headers)

This sends the POST request to the pdfRest API with the file data and headers.

if response.ok:
    response_json = response.json()
    print(json.dumps(response_json, indent = 2))
else:
    print(response.text)

Finally, the script checks the response status and prints out the JSON response or error message.

Beyond the Tutorial

By following the steps above, you've learned how to use Python to convert files to PDF using the pdfRest API. To explore and demo all of the pdfRest API Tools, visit the API Lab at https://pdfrest.com/apilab/. For comprehensive documentation, refer to the API Reference at https://pdfrest.com/documentation/.

Note: This is an example of a multipart API call. Code samples using JSON payloads can be found at https://github.com/datalogics/pdf-rest-api-samples/tree/main/Python/Endpoint%20Examples/JSON%20Payload/pdf.py.

Generate a self-service API Key now!
Create your FREE API Key to start processing PDFs in seconds, only possible with pdfRest.