How to reduce the size of a PDF file with a Python 3 script

It is common to find upload forms that require PDF documents under a strict size limit. In those situations a local compression workflow is often the simplest solution.

Instead of uploading the file to a random website, this article shows how to compress PDFs locally with Python and Ghostscript. Repository: https://github.com/al118345/pdfconpython3

Why compress PDFs locally

Local compression is useful when the document contains IDs, invoices, contracts or personal information that should not be sent to third-party upload tools. Python orchestrates the workflow and Ghostscript performs the PDF rewrite, so the process can be repeated over a full folder with the same settings.

The important decision is the quality profile: /screen creates smaller files but may degrade images, while /prepress preserves more quality and produces heavier documents. For scanned files, always keep the original and compare the result before deleting anything.

Video: https://www.youtube.com/watch?v=Qc6WG2k5tFk

File compression in Python

The following script contains the required methods to execute PDF compression with Python 3:

compress method

The compress function receives the input path, the output path and the compression level. A value of 4 applies stronger compression, while 1 preserves more quality.

The method relies on Ghostscript with these flags:

  • -sDEVICE=pdfwrite
  • -dCompatibilityLevel=1.4
  • -dPDFSETTINGS=[0-4]
  • dNOPAUSE
  • -dQUIET
  • -dBATCH

Important: use a different output path and do not overwrite the original file directly.

get_ghostscript_path method

This helper returns the path to the Ghostscript executable available in the system.

Main method

A basic batch usage example looks like this:

The script loops through all files in a folder and stores the compressed PDFs in a different destination folder.

Safe compression checklist

  • Write compressed files to a different output folder.
  • Measure the original and final file size before replacing anything.
  • Open a sample result manually to verify text readability and image quality.
  • Use batch mode only after testing one document with the chosen profile.

When this approach is especially useful

This workflow is a good fit for repetitive administrative tasks: reducing a folder of scanned forms, preparing documents for a public upload portal, or normalizing attachments before sending them by email. If the PDF is generated by your own application, it is usually better to optimize image resolution and export settings at the source; if the PDF already exists, Ghostscript becomes a practical post-processing step.

Related Angular and backend examples: compress PDFs from Angular, protect an Angular API with Nginx and FastAPI and build a Python API client.