How to Convert PDF to PostScript with Python

Convert PDF documents to PostScript with Python and configure page, scaling, annotation, and output settings.
Share this page

This Python walkthrough uses requests and a multipart encoder. It converts a PDF into PostScript through /postscript with visible output controls for a production workflow. The complete sample is included below so Python developers can see the file handling, request construction, and response path together.

Why PDF to PostScript with Python?

Python services can generate PostScript when a specialized renderer or a legacy production stage expects that output even though the surrounding application manages PDF documents. The original PDF remains untouched while the API creates a new PostScript resource.

A manufacturing-data pipeline could transform selected pages of an approved PDF into Level 3 PostScript with explicit scaling and annotation choices, then deliver the result to a system that does not consume PDF directly.

The response gives Python a machine-readable output ID, allowing the pipeline to download the file, save it with job metadata, or carry it into another compatible API operation.

Python Code Example for PDF to PostScript

import json
import os
import requests

# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"

# This sample converts PDF to PostScript through multipart /postscript.
# Pairing this endpoint with /pdf creates a PDF -> PostScript -> PDF workflow commonly
# called PDF refrying. Some print and prepress workflows use it to rebuild, flatten, or
# normalize page content, but the lossy roundtrip can discard PDF-specific features.
# These settings request Level 3, text-safe output, all pages at original scale,
# shrink-to-fit without rotation, and printable annotations.
input_path = "/path/to/sample.pdf"

from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file:
    fields = {
        "file": (os.path.basename(input_path), input_file, "application/pdf"),
        "ps_level": "3",
        "page_range": "all",
        "binary_output": "false",
        "scale": "1",
        "rotate": "false",
        "shrink_to_fit": "true",
        "print_annotations": "true",
        "output": "postscript_from_pdf",
    }
    form = MultipartEncoder(fields=fields)
    response = requests.post(api_url + "/postscript", data=form, headers={
        "Accept": "application/json",
        "Content-Type": form.content_type,
        "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    })

print("Response status code: " + str(response.status_code))
if response.ok:
    print(json.dumps(response.json(), indent=2))
else:
    print(response.text)
    raise SystemExit(1)

Source: View the sample on GitHub

Breaking Down the Code

Configure the service and input

# By default, we use the US-based API service. This is the primary endpoint for global use.
api_url = "https://api.pdfrest.com"

# For GDPR compliance and enhanced performance for European users, you can switch to the EU-based service by uncommenting the URL below.
# For more information visit https://pdfrest.com/pricing#how-do-eu-gdpr-api-calls-work
#api_url = "https://eu-api.pdfrest.com"

For this PDF to PostScript example, Python sends the multipart request to /postscript. The Python samples use requests and MultipartEncoder from requests-toolbelt. Install both packages when using the file outside the samples project and keep the API key in environment configuration. The sample’s placeholder path and API key make the moving pieces visible; replace them with a real local path and protected runtime credential before using the request.

Select PostScript Output Settings

with open(input_path, "rb") as input_file:
    fields = {
        "file": (os.path.basename(input_path), input_file, "application/pdf"),
        "ps_level": "3",
        "page_range": "all",
        "binary_output": "false",
        "scale": "1",
        "rotate": "false",
        "shrink_to_fit": "true",
        "print_annotations": "true",
        "output": "postscript_from_pdf",

The Python sample explicitly requests ps_level=3, all pages, binary output, no rotation, unit scale, shrink-to-fit, and printable annotations. Those choices describe the new PostScript result; they do not edit or replace the uploaded PDF.

Build the multipart request

input_path = "/path/to/sample.pdf"

from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file:
    fields = {
        "file": (os.path.basename(input_path), input_file, "application/pdf"),
        "ps_level": "3",
        "page_range": "all",
        "binary_output": "false",
        "scale": "1",
        "rotate": "false",

For the PDF to PostScript form, MultipartEncoder combines the file tuple and text fields, then exposes a content_type value that includes the generated boundary. That value must be used as the request header.

Read the output resource

"output": "postscript_from_pdf",
    }
    form = MultipartEncoder(fields=fields)
    response = requests.post(api_url + "/postscript", data=form, headers={
        "Accept": "application/json",
        "Content-Type": form.content_type,
        "Api-Key": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    })

print("Response status code: " + str(response.status_code))
if response.ok:
    print(json.dumps(response.json(), indent=2))

After the PDF to PostScript call, the sample prints formatted response JSON after a successful request and exits with an error after printing a failed response. Preserve the returned outputId when the next workflow step needs the generated resource.

Beyond the Tutorial

This Python example submitted a PDF to /postscript and selected the controls that shape the resulting PostScript file. Retain the returned resource ID whenever the output needs to be downloaded, monitored, or used by a following workflow step.

Try representative pdf to postscript inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the pdfRest API Reference Guide. The same request pattern can then be adapted to your Python application’s error handling, retention policy, and delivery workflow.

For Python applications that already hold the source as a managed resource, the repository includes a JSON-payload PDF-to-PostScript example. It uploads the PDF separately and sends the returned ID with the PostScript options. View the JSON-payload sample on GitHub.

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