11

Ghostscript wipes the PDF metadata like author, title, subject etc. How can I tell ghostscript not to touch the metadata? I invoke it as follows:

gs \
  -dBATCH                    \
  -dNOPAUSE                  \
  -sOutputFile=<output_file> \
  -sDEVICE=pdfwrite          \
  -dPDFSETTINGS=/ebook       \
  <input_file>
5
  • Why are you using Ghostscript? There's probably another way to do whatever you're doing. It's uncommon to use Ghostscript when the input isn't a Postscript file. Oct 10, 2012 at 23:14
  • 4
    @Gilles To down-sample the images in the PDF and thus reducing the size. I don't think it's uncommon to use PDF as input. The tool pdf2ps e.g. uses ghostscript for the conversion and is shipped together with gs.
    – Marco
    Oct 11, 2012 at 7:08
  • Ok, Ghostscript does seem to be the usual way to downsample images in a PDF file, the open source native PDF manipulation tools seem to be lacking in that regard. That or ImageMagick, which only looks at the images and won't care about PDF metadata. Oct 11, 2012 at 9:51
  • 3
    ImageMagick uses ghostscript for processing PDFs.
    – Marco
    Oct 11, 2012 at 9:56
  • @Gilles: "It's uncommon to use Ghostscript when the input isn't a Postscript file." -- Not at all, Gilles! I use it all the time, every day to do PDF->PDF processing. Jan 28, 2015 at 20:13

1 Answer 1

6

Apparently it's not possible to keep the PDF metadata when using ghostscript. Here is a workaround which first saves the metadata to a file using pdftk, then compresses the file with ghostscript and finally writes back the metadata also using pdftk.

INPUTPDF=<input_file>
OUTPUTPDF=<output_file>
TMPPDF=$(mktemp)
METADATA=$(mktemp)

# save metadata
pdftk "$INPUTPDF" dump_data_utf8 > "$METADATA"

# compress
gs                       \
  -q                     \
  -sOutputFile="$TMPPDF" \
  -sDEVICE=pdfwrite      \
  -dNOPAUSE              \
  -dBATCH                \
  -dPDFSETTINGS=/ebook   \
  "$INPUTPDF"

# restore metadata
pdftk "$TMPPDF" update_info_utf8 "$METADATA" output "$OUTPUTPDF"

# clean up
rm -f "$TMPPDF" "$METADATA"

Edit: This is a bug in ghostscript, see Bug report and the confirmation that this is not supposed to happen.

1
  • pdftks dump_data(_utf8) literally outputs Chinese labels. I couldn't find a forum for pdftk either :(
    – Stefan K.
    Apr 17, 2013 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.