Convert PS, EPS, and XPS

Unlock unparalleled convenience and efficiency as you convert your PostScript (PS), Encapsulated PostScript (EPS), XML Paper Specification (XPS), and Open XML Paper Specification (OXPS) files into popular image formats such as JPEG, PNG, TIFF, and more with Aspose.Page API Solution for Python

 

Introducing our cutting-edge API for Python, the ultimate solution for effortlessly converting PS, EPS, XPS, and OXPS files to a variety of image formats and PDFs. With our powerful converter, you can seamlessly transform your documents into high-quality images or PDFs, all with just a few lines of code.

For PS and EPS formats, the API supports PostScript operators ranging from Levels 1 to 3, along with most EPS header comments. It seamlessly transforms PostScript documents, ensuring maximum compliance with only a few exceptions, such as font cases, where the API handles fonts like Times New Roman.

Furthermore, for XPS file transformations, the API enables you to add or remove pages, manipulate canvases, paths, and glyph elements, create vector graphics shapes and text strings, convert XPS outline items, and more.

Our Python API solution facilitates the programmatic conversion of files in PDL formats such as PS, EPS, and XPS. However, you may also find it beneficial to explore and experiment with cross-platform development using these native APIs. With comprehensive documentation and support, getting started is a breeze, even for developers new to file conversion tasks. Don’t let file formats stand in the way of your productivity. Elevate your document management capabilities today with our API for Python converter.

PostScript to PDF Conversion via Python.

To convert PostScript PS and Encapsulated PostScript EPS files to PDF via Python API you need to take the next steps:

  1. Initialize an input stream for input PS file.
  2. Initialize an output stream for the output PDF document.
  3. Create an instance of PsDocument from the created input stream.
  4. Use PdfSaveOptions to specify AdditionalFontsFolder and SuppressError boolean value.
  5. Create an instance of PdfDevice from created earlier output stream.
  6. Save PostScript document as PDF with PDF save options.

Let’s consider PdfSaveOptions. Using this class we can assign different conversion parameters while converting PS to PDF.

  • AdditionalFontsFolder specifies locations where to find fonts. System font folders are always included by default.
  • SuppressError controls the behavior of the PS to PDF converter when non-critical errors appear. If the value is true then it is possible to view a list of such errors after conversion in the Exceptions field. The default value is true.
  • Debug allows the output of debug information to the console. The default value is false.

Python Code to convert PS EPS to PDF

  # The path to the documents directory.
  data_dir = Util.get_data_dir_working_with_document_conversion()
  # Initialize the image format
  image_format = aspose.pydrawing.Imaging.ImageFormat.png
  # Initialize Encapsulated PostScript input stream
  eps_stream = open(data_dir + "inputForImage.eps", "rb")

  document = PsDocument(eps_stream)

  # If you want to convert Postscript file despite of minor errors set this flag
  suppress_errors = True

  # Initialize the options object with the necessary parameters.
  options = ImageSaveOptions(suppress_errors)
  # If you want to add a special folder where fonts are stored. Default fonts folder in OS is always included.
  # options.additional_fonts_folders = [ """{FONT_FOLDER}""" ]

  # Default image size is 595x842 and it is not mandatory to set it in ImageDevice
  device = ImageDevice(image_format)
  # But if you need to specify size use constructor with two parameters
  # device = new ImageDevice(new aspose.pydrawing.Size(595, 842), aspose.pydrawing.imaging.ImageFormat.Jpeg);

  try:
    document.save(device, options)
  finally:
    eps_stream.close()

  images_bytes = device.images_bytes

  i = 0

  for image_bytes in images_bytes:
    image_path = os.path.abspath(data_dir + "out_image" + str(i) + ".png")
    with open(image_path, "wb") as fs:
        fs.write(image_bytes[0:0+len(image_bytes)])
    i += 1
 

PostScript to Images Conversion via Python.

For any EPS/PS PostScript to image converter application, the following Python code works well, so take the next steps:

  1. Initialize input stream with PS source file.
  2. Create an instance of PsDocument from the created input stream.
  3. Use ImageSaveOptions to specify AdditionalFontsFolder and SuppressError etc.
  4. Create an instance of ImageDevice object for specifying an image type and size if needed.
  5. Save PS/EPS file as and image with an image save options as an array of arrays of bytes. One array of bytes for one page of an input file.
  6. Save resulting 2-dimensional arrays of bytes to EMF files creating for every bytes array a new file output stream

The ImageSaveOptions are the same as for PS to PDF Conversion.

Python Code for PostScript to Images Conversion

  # The path to the documents directory.
  data_dir = Util.get_data_dir_working_with_document_conversion()
  # Initialize animage format
  image_format = aspose.pydrawing.Imaging.ImageFormat.png
  # Initialize the Encapsulated PostScript input stream
  ps_stream = open(data_dir + "inputForImage.ps", "rb")

  document = PsDocument(ps_stream)

  # If you want to convert the Postscript file despite of minor errors set this flag
  suppress_errors = True

  # Initialize an options object with the necessary parameters.
  options = ImageSaveOptions(suppress_errors)
  # If you want to add a special folder where fonts are stored. Default the fonts folder in OS is always included.
  # options.additional_fonts_folders = [ """{FONT_FOLDER}""" ]

  # Default the image size is 595x842 and it is not mandatory to set it in the ImageDevice
  device = ImageDevice(image_format)
  # But if you need to specify the size use the constructor with two parameters
  # device = new ImageDevice(new aspose.pydrawing.Size(595, 842), aspose.pydrawing.imaging.ImageFormat.Jpeg);

  try:
    document.save(device, options)
  finally:
    ps_stream.close()

  images_bytes = device.images_bytes

  i = 0

  for image_bytes in images_bytes:
    image_path = os.path.abspath(data_dir + "out_image" + str(i) + ".png")
    with open(image_path, "wb") as fs:
        fs.write(image_bytes[0:0+len(image_bytes)])
    i += 1
 

Convert XPS to Images JPG, PNG, BMP via Python.

Python API deals XPS format that is used for representing page layout. In any scenario, whenever there is a need to convert XPS to images BMP, JPG, PNG, and TIFF programmatically, the following code can easily be integrated within the Python application.

  1. Initialize an input stream for the input XPS document.
  2. Create an instance of XpsDocument from the created earlier input stream.
  3. Specify SmoothingMode, Resolution, and other options of PngSaveOptions .
  4. Create an instance of ImageDevice .
  5. Save resulting 2-dimensional arrays of bytes to BMP files creating for every byte array a new file output stream.

Let’s consider ImageSaveOptions. Using this class we can assign different conversion parameters while converting XPS to BMP.

  • SmoothingMode assigns a degree of smoothing for lines, curves, and edges of filled areas, which may be AntiAlias, HighQuality, HighSpeed, and Default (none). The default value is HighQuality.
  • Resolution controls the resolution of the resulting image. The default value is 96.
  • TextRenderingHint assigns the quality of text rendering, which may be AntiAlias, AntiAliasGridFit, ClearTypeGridFit, SingleBitPerPixel, SingleBitPerPixelGridFit, SystemDefault. The default value in XPS to BMP conversion is AntiAliasGridFit.
  • InterpolationMode defines an algorithm that is used when scaling or/and rotating an image, which may be Bicubic, Bilinear, High, HighQualityBicubic, HighQualityBilinear, Low, NearestNeighbor, and Default. The default value is HighQualityBicubic.
  • PageNumbers represents an array of numbers of pages that will be saved to BMP.

Python Code for XPS to Image(.png) Conversion

  # The path to the documents directory.
  data_dir = Util.get_data_dir_working_with_document_conversion()
  # Load XPS document
  document = XpsDocument(dataDir + "input.xps", XpsLoadOptions())
  # Initialize the options object with the necessary parameters.
  options = PngSaveOptions()
  options.smoothing_mode = aspose.pydrawing.drawing2d.SmoothingMode.HIGH_QUALITY
  options.resolution = 300
  options.page_numbers = [ 1, 2, 6 ]

  # Create a rendering device for image
  device = ImageDevice()
            
  document.save(device, options)

  # Iterate through document partitions (fixed documents, in XPS terms)
  for i in range(len(device.result)):
    # Iterate through partition pages
    for j in range(len(device.result[i])):
        # Initialize image output stream
        with open(os.path.splitext(output_file_name)[0] + "_" + str(i + 1) + "_" + str(j + 1) +
                  os.path.splitext(output_file_name)[1], "wb") as image_stream:
            # Write image
            image_stream.write(device.result[i][j][0:0+len(device.result[i][j])])
    }
  }
 

Convert XPS to PDF via Python.

The process of converting XPS to PDF documents programmatically is simple having high fidelity results among the input and output files.

  1. Initialize an input stream for the input XPS document.
  2. Initialize an output stream for output PDF document.
  3. Create an instance of XpsDocument from the created earlier input stream.
  4. Specify TextCompression, ImageCompression, JpegQualityLevel, and other options of PdfSaveOptions .
  5. Save the XPS document as a PDF with PDF save options.

Let’s consider PdfSaveOptions. Using this class we can assign different conversion parameters while converting XPS to PDF.

  • JpegQualityLevel controls the quality of images in PDF documents if the ImageCompression algorithm is JPEG and can be from 0 to 100.
  • ImageCompression algorithm encapsulated in PdfImageCompression Enumeration may be Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW) with base or optimized predictor, JPEG, none (raw image bytes) and auto (the most appropriate compression for each image). The default is auto compression.
  • TextCompression algorithm encapsulated in PdfTextCompression Enumeration may be Run Length Encoding (RLE), Flate, Lempel-Ziv-Welch (LZW), and none. The default value in XPS to PDF conversion is Flate.
  • EncryptionDetails encapsulated in PdfEncryptionDetails . It allows setting encryption algorithms, permissions, and owner and user passwords for PDF documents.
  • PageNumbers represents an array of numbers of pages that will be saved to PDF.
  • OutlineTreeExpansionLevel and OutlineTreeHeight control view of the document outline.

Python Code for XPS to PDF Conversion

  # The path to the documents directory.
  String dataDir = Utils.getDataDir();
  # Initialize a PDF output stream
  FileOutputStream pdfStream = new FileOutputStream(dataDir + "XPStoPDF.pdf");

  # Load an XPS document
  XpsDocument document = new XpsDocument(dataDir + "input.xps");

  # Initialize an options object with the necessary parameters.
  com.aspose.xps.rendering.PdfSaveOptions options = new com.aspose.xps.rendering.PdfSaveOptions();
  options.setJpegQualityLevel(100);
  options.setImageCompression(com.aspose.xps.rendering.PdfImageCompression.Jpeg);
  options.setTextCompression(com.aspose.xps.rendering.PdfTextCompression.Flate);
  options.setPageNumbers(new int[] { 1, 2, 6 });

  # Create a rendering device for the PDF format
  com.aspose.xps.rendering.PdfDevice device = new com.aspose.xps.rendering.PdfDevice(pdfStream);

  document.save(device, options);
 



FAQ

1. Can I convert Postscript with this API solution?

Aspose.Page has functionality that allows you to convert PS, XPS, and EPS files to other formats online or programmatically. If you need to transform your files instantly online you may like to use Page Description Language format files Converter cross-platform application.

2. What Page description Languages are supported by the converter?

This conversion functionality supports files with .ps, .eps, and .xps extensions. Such famous PDLs as PDF and SVG are represented as separate solutions in Aspose.products

3. Is the functionality free?

The cross-platform converters are free, when for the API solution you can get a free Trial and then buy the product if needed.

  

Support and Learning Resources