How to Convert PostScript to PDF with Python
This Python walkthrough uses requests and a multipart encoder. It converts a PostScript (.ps) file into PDF through the pdfRest Convert to PDF API Tool and includes a custom .joboptions profile. The complete sample is included below so Python developers can see the file handling, request construction, and response path together.
Why PostScript to PDF with Python?
Python automation can convert the PostScript outputs of established reporting or scientific workflows into PDFs that are easier to archive, inspect, and share. The request fits naturally into a pipeline that already works with files and HTTP responses.
A research publishing process might produce a .ps figure package, apply a maintained .joboptions profile, and send the resulting PDF to an approval or distribution stage. The conversion settings live in the profile rather than in ad hoc rendering code.
The profile is optional, so a simpler pipeline can submit only the PostScript source and rely on default conversion settings when there is no named production configuration to apply.
Python Code Example for PostScript to PDF
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 a PostScript (.ps) file to PDF with a custom .joboptions profile.
# A .joboptions file contains Adobe Distiller-compatible conversion settings. The
# profile is optional; omit job_options to use default settings. pdfRest applies a
# supplied profile with Datalogics PDF Converter SDK. Datalogics maintains the SDK
# in partnership with Adobe, using the same Adobe technology that powers Distiller.
# Pairing this /pdf call with /postscript creates a PDF -> PostScript -> PDF workflow
# commonly called PDF refrying. Some print and prepress workflows use it to rebuild or
# normalize page content, but the lossy roundtrip can discard PDF-specific features.
input_path = "/path/to/sample.ps"
job_options_path = "/path/to/custom.joboptions"
from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file, open(job_options_path, "rb") as job_options_file:
fields = {
"file": (os.path.basename(input_path), input_file, "application/postscript"),
"job_options": (
os.path.basename(job_options_path),
job_options_file,
"application/octet-stream",
),
"output": "pdf_from_postscript",
}
form = MultipartEncoder(fields=fields)
response = requests.post(api_url + "/pdf", 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 PostScript to PDF example, Python sends the multipart request to /pdf. 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.
Apply the PostScript Conversion Profile
with open(input_path, "rb") as input_file, open(job_options_path, "rb") as job_options_file:
fields = {
"file": (os.path.basename(input_path), input_file, "application/postscript"),
"job_options": (
os.path.basename(job_options_path),
job_options_file,
"application/octet-stream",
),
"output": "pdf_from_postscript",
}
form = MultipartEncoder(fields=fields)
Here, Python sends the PostScript source and the optional profile as separate files. A .joboptions file contains Adobe Distiller-compatible conversion settings; pdfRest applies a supplied profile with Datalogics PDF Converter SDK, maintained in partnership with Adobe and using the same Adobe technology that powers Distiller. Omitting job_options uses default settings.
Build the multipart request
job_options_path = "/path/to/custom.joboptions"
from requests_toolbelt import MultipartEncoder
with open(input_path, "rb") as input_file, open(job_options_path, "rb") as job_options_file:
fields = {
"file": (os.path.basename(input_path), input_file, "application/postscript"),
"job_options": (
os.path.basename(job_options_path),
job_options_file,
"application/octet-stream",
),
For the PostScript to PDF 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": "pdf_from_postscript",
}
form = MultipartEncoder(fields=fields)
response = requests.post(api_url + "/pdf", 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 PostScript to PDF 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
You have now used Python to turn a PostScript source into a managed PDF and make its conversion profile explicit. That gives the surrounding application a deliberate choice between a team-maintained .joboptions file and the service defaults.
Try representative postscript to pdf inputs in API Lab. For the authoritative request fields, accepted values, and response contract, consult the Convert to PDF API Tool documentation. The same request pattern can then be adapted to your Python application’s error handling, retention policy, and delivery workflow.
A JSON-payload PostScript-to-PDF example is available for Python as well. That form uploads the .ps and optional .joboptions files first, then passes their resource IDs to /pdf. View the JSON-payload sample on GitHub.