api.utils.pdf_compression

Shrinking a PDF with Ghostscript, for uploads that exceed a size limit (Shopify’s Files storage refuses anything over 20 MB).

Manufacturer manuals are usually heavy because of high-resolution scans and photos: re-rendering them with Ghostscript’s /ebook preset (images downsampled to 150 dpi) typically divides their size by 3 to 10 with no visible loss on screen. Text and vector drawings are kept as is.

Ghostscript (gs) must be installed – it is in the Docker image. When it isn’t, or when it fails, compress_pdf returns None rather than raising: compression is a best-effort attempt and the caller reports the original size problem instead.

Attributes

Functions

compress_pdf(content) → bytes | None

The PDF re-rendered with Ghostscript's /ebook preset, or None

Module Contents

api.utils.pdf_compression.logger
api.utils.pdf_compression.GHOSTSCRIPT_TIMEOUT_SECONDS = 180
api.utils.pdf_compression.compress_pdf(content: bytes) → bytes | None

The PDF re-rendered with Ghostscript’s /ebook preset, or None when Ghostscript is missing, fails, or doesn’t produce a smaller file.