PS and EPS Conversion

PS and EPS Conversion

PsDocument is the entry point for loading PostScript (.ps) and Encapsulated PostScript (.eps) files. Both formats are handled by the same class through the same interface.


Loading a PS or EPS File

Use PsDocument.from_file() to load a document from a file path. The is_eps property indicates whether the loaded file is an EPS file.

from aspose.page.ps.document import PsDocument

ps = PsDocument.from_file("document.ps")
eps = PsDocument.from_file("illustration.eps")
print("Is EPS:", eps.is_eps)

To load from raw bytes (e.g. an upload or network stream), use from_bytes():

from aspose.page.ps.document import PsDocument

with open("document.ps", "rb") as f:
    data = f.read()
doc = PsDocument.from_bytes(data)

Exporting to PDF

Call to_pdf() on a loaded PsDocument to obtain a PDF byte array. Write the result to disk or return it directly from an HTTP handler.

from pathlib import Path
from aspose.page.ps.document import PsDocument

ps = PsDocument.from_file("input.ps")
pdf_bytes = ps.to_pdf()
Path("output.pdf").write_bytes(pdf_bytes)

Exporting to PNG or JPEG

Pass an ImageSaveOptions instance to to_image(). Set format to "png" or "jpeg" and dpi to the desired resolution.

from pathlib import Path
from aspose.page.ps.document import PsDocument
from aspose.page.ps.output import ImageSaveOptions

eps = PsDocument.from_file("illustration.eps")
opts = ImageSaveOptions(format="png", dpi=150)
Path("output.png").write_bytes(eps.to_image(opts))

Reading EPS DSC Metadata

PsDocument.dsc returns a DscMetadata instance when DSC header comments are present in the EPS file. Use it to read bounding box, title, and creator fields before converting.

from aspose.page.ps.document import PsDocument

eps = PsDocument.from_file("illustration.eps")
dsc = eps.dsc
if dsc is not None:
    print("Title:", dsc.title)
    print("Bounding box:", dsc.bounding_box)
    print("Creator:", dsc.creator)

Tips and Best Practices

  • Use from_bytes() when processing uploads to avoid writing temporary files.
  • Check is_eps to branch logic when processing a mixed batch of PS and EPS files.
  • Set dpi=150 or higher for raster output used in document preview systems.
  • All conversion methods return bytes — store or stream them without disk I/O if needed.

Common Issues

IssueCauseFix
FileNotFoundErrorPath passed to from_file() does not existVerify the file path before calling from_file()
Empty PDF outputPS file contains no renderable contentInspect the PS source for missing showpage operators
Low-resolution rasterDefault DPI is 96Pass ImageSaveOptions(dpi=150) or higher

FAQ

Can I convert EPS files to PDF?

Yes. PsDocument.from_file() accepts both .ps and .eps extensions. Call to_pdf() on the returned document to obtain PDF bytes.

Does conversion require Ghostscript?

No. The library has no dependency on Ghostscript or any native runtime.

Can I process PS files without writing to disk?

Yes. Use from_bytes() to load from a bytes object and use the returned bytes from to_pdf() or to_image() directly — no files need to be written.


API Reference Summary

Class / MethodDescription
PsDocument.from_file(path)Load PS or EPS from a file path
PsDocument.from_bytes(data)Load PS or EPS from raw bytes
PsDocument.to_pdf(options)Export to PDF bytes
PsDocument.to_image(options)Export to raster image bytes
PsDocument.is_epsTrue if the loaded file is EPS
PsDocument.dscDSC header metadata (EPS files)
DscMetadata.bounding_boxEPS bounding box as (x0, y0, x1, y1)
DscMetadata.titleTitle from EPS DSC header
ImageSaveOptions.formatOutput format: “png” or “jpeg”
ImageSaveOptions.dpiOutput resolution in DPI

See Also