Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a PDF that has already compressed and somewhat artifact-y images, and I'm using Ghostscript to prepend a title page to that PDF.

However, I cannot find any way to tell GS to just use the existing images as-is without reprocessing them, and now I'm feeling as if it's something to do with how GS works, i.e. you can't recompile/link a PDF without reprocessing its images.. Is that true?

I can raise the DPI setting in GS, but it'll go from 5MB to 60MB while still looking worse.

Is there any better alternative to GS that'll do what I need (preferably that will compile on OS X)?

share|improve this question
    
Can you edit your question and quote the exact commandline you are using to prepend your title page to the original PDF? Then I could tell you what exactly to change or add to the commandline in order to get a better output for images... –  Kurt Pfeifle Nov 25 '11 at 12:46
    
I don't want to just have it look better, I want to merge without reprocessing. This will a) result in better quality (lossless transforms), and b) not waste hours of CPU time processing my 1000+ page document. –  Mahmoud Al-Qudsi Jan 2 '12 at 4:44
    
Hey, you didn't answer my question and you didn't quote the exact GS commandline you are using. Which means: you'll not be getting the help regarding GS you're looking for... –  Kurt Pfeifle Jan 2 '12 at 9:07
add comment

1 Answer 1

up vote 8 down vote accepted

If you just want to concatenate two PDF files without any reprocessing of its content, pdftk is for you. (On Mac OS X this should be available via MacPorts or Fink, for Linux, there are native packages for all major distributions; for Windows, look here.) Try this:

 pdftk title.pdf content.pdf cat output book.pdf

This will prepend the title.pdf to the content.pdf and write the result into book.pdf.

pdftk is a "dumb", but very fast way to concatenate two (or more) PDF files. "Dumb" in so far, as pdftk does not in any way interprete the PDF data stream, it just makes sure that the internal object numbers are re-reshuffled as needed and appear in the PDF xref structure (which basically is a sort of PDF ToC for objects).

Ghostscript:

If you want to use Ghostscript, the basic command to concatenate the same two files would be:

 gs \
  -o book.pdf \
  -sDEVICE=pdfwrite \
   title.pdf \
   content.pdf

However, as you experienced, this simple commandline which will be using default settings for a lot of details in the overall processing may mess up your image quality. The solution is to not let Ghostscript use its defaults, by adding more custom parameters to the commandline.

Ghostscript uses a much more "intelligent" (but also much slower) method to create the new book.pdf output than pdftk does.

Because when Ghostscript reads an input PDF, it 'interprets' all of the file and its contents (objects, streams, fonts, images,...) and holds these in its own internal representation, before spitting out the resulting PDF with its PDF objects again. However, when 'spitting out', Ghostscript will apply all of its internal default settings for the hundreds of parameters [*] which there are available.

Unfortunately, this causes your "reprocessing" of images according to these default settings -- which can only be avoided or overridden by adding your own (desired) commandline parameters.

Your image problems could be caused by Ghostscript's need (due to licensing issues) to re-encode JPEG2000 images to JPEG encoding. If you want to avoid this, add the following to your commandline:

-dAutoFilterColorImages=false \
-dAutoFilterGrayImages=false \
-dColorImageFilter=/FlateEncode \
-dGrayImageFilter=/FlateEncode \

Other image-related commandline options to consider for including are:

-dColorConversionStrategy=/LeaveColorUnchanged \
-dDownsampleMonoImages=false \
-dDownsampleGrayImages=false \
-dDownsampleColorImages=false \

So the complete Ghostscript commandline that could make you happy should read:

 gs \
  -o book.pdf \
  -sDEVICE=pdfwrite \
  -dColorConversionStrategy=/LeaveColorUnchanged \
  -dDownsampleMonoImages=false \
  -dDownsampleGrayImages=false \
  -dDownsampleColorImages=false \
  -dAutoFilterColorImages=false \
  -dAutoFilterGrayImages=false \
  -dColorImageFilter=/FlateEncode \
  -dGrayImageFilter=/FlateEncode \
   title.pdf \
   content.pdf

You could also tell Ghostscript NOT to compress images at all in the output PDF, by using this commandline:

 gs \
  -o book.pdf \
  -sDEVICE=pdfwrite \
  -dColorConversionStrategy=/LeaveColorUnchanged \
  -dEncodeColorImages=false \
  -dEncodeGrayImages=false \
  -dEncodeMonoImages=false \
   title.pdf \
   content.pdf

.


[*]:
If you are interested to learn about a complete list of default settings which Ghostscript's pdfwrite device is using, run the following command. It returns you the full list:

 gs \
   -sDEVICE=pdfwrite \
   -o /dev/null \
   -c "currentpagedevice { exch ==only ( ) print == } forall"

For explanations about what exactly all these parameters do mean, you'll have to read up in the Adobe documentation about "Distiller Parameters". Ghostscript tries very hard to mimic all these...

share|improve this answer
    
outstanding answer! –  matt wilkie Jan 8 '13 at 21:20
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.