Convert PS to PDF in Python

This quick guide explains how to convert PS to PDF in Python. It covers the system configuration, stepwise algorithm, and a runnable sample code for PS to PDF conversion in Python. Furthermore, it includes various overload methods to improvise the PostScript file conversion according to your requirements.

Steps to Convert PS to PDF in Python

  1. Install Aspose.Page API to render the EPS files on your end
  2. Initialize the PDF output stream
  3. Initialize the PostScript input stream with the PsDocument class
  4. Create an object of the PdfSaveOptions class
  5. Render the output PDF file

These steps outline the process of exporting PS to PDF in Python. Create the source and output streams to work with PostScript files. Next, you may set custom PDF parameters and render the output PDF document.

Code to Change PS to PDF in Python

import aspose.page
from aspose.page import *
from aspose.page.eps.device import *
path = "C://"
# Initialize PDF output stream
pdf_stream = open("output.pdf", "wb")
# Initialize PostScript input stream
ps_stream = open(“input.ps", "rb",)
document = aspose.page.eps.PsDocument(ps_stream)
# Ignore minor errors
suppress_errors = True
# Initialize options object
options = aspose.page.eps.device.PdfSaveOptions(suppress_errors)
device = PdfDevice(pdf_stream)
try:
document.save(device, options)
finally:
ps_stream.close()
pdf_stream.close()

This code snippet is sufficient to convert PostScript to PDF in Python. It mainly works with the PsDocument class to load the input file and set custom options. Subsequently, export the output PDF file to a stream or the disk. Moreover, you can improvise the conversion by setting the page size, and margins along with various other properties.

This tutorial includes all the basic details to convert PS file to PDF in Python. If you want to convert XPS to PDF, read the article on Convert XPS to PDF in Python.

 English