Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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>
share|improve this question
    
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. –  Gilles Oct 10 '12 at 23:14
2  
@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 '12 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. –  Gilles Oct 11 '12 at 9:51
1  
ImageMagick uses ghostscript for processing PDFs. –  Marco Oct 11 '12 at 9:56
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
pdftks dump_data(_utf8) literally outputs Chinese labels. I couldn't find a forum for pdftk either :( –  Stefan K. Apr 17 '13 at 10:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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