Search code examples
ghostscriptimage-compression

Prevent GhostScript to turn PNGs into JPEGs


I use GhostScript to fix/repair non-compliant/corrupted PDFs in order to let them be successfully opened by PDF readers and be edited with Acrobat Pro without errors or warnings.

gs \
  -o repaired.pdf \
  -sDEVICE=pdfwrite \
  -dPDFSETTINGS=/default \
   corrupted.pdf

I noticed however that PNGs images into the PDF are turned into JPEGs with a loss of quality.

Is there a way or specific option to avoid that?

I searched into the documentation without success.


Solution

  • PDF cannot contain PNG images, because the PDF format does not support PNG. Images can be compressed with a variety of algorithms and the options are documented. See:

    https://ghostscript.readthedocs.io/en/latest/VectorDevices.html#distiller-parameters

    You will want to alter the AutoFilter...Images switches and then the ColorImageFilter, MonoImageFilter and GrayImageFilter settings.

    And there's really no point in putting -dPDFSETTINGS=/default :-)