Wednesday, July 4, 2012

ps2pdf with bounding box

To convert ps file to pdf using non-standard file size (default is A4) first read off the bounding box from the ps file :
%%BoundingBox: 0 0 1275 236

Then :
ps2pdf -dDEVICEWIDTHPOINTS=1275 -dDEVICEHEIGHTPOINTS=236 file.ps file.pdf


For ps files with offset or in general easier method:

ps2pdf file1.ps file2.pdf (give device width and height with added space if needed)
pdfcrop file2.pdf file3.pdf


For color images, default setting is to resort to lossy JPEG compression. To set to lossless compression for better images set options:
 -dAutoFilterColorImages=false  -dColorImageFilter=/FlateEncode

Expanded options list:
ps2pdf -sPAPERSIZE=a4  -dCompatibilityLevel=1.3  \
 -dEmbedAllFonts=true  -dSubsetFonts=true  -dMaxSubsetPct=100  \
 -dAutoFilterColorImages=false  -dColorImageFilter=/FlateEncode  \
 -dAutoFilterGrayImages=false  -dGrayImageFilter=/FlateEncode  \
 -dAutoFilterMonoImages=false  -dMonoImageFilter=/CCITTFaxEncode  \
 document.ps  document.pdf

The important thing for the image quality is AutoFilter...Images=false and ...ImageFilter=/FlateEncode. The first disables the automatic determination by Ghostscript of the "best" compression format, which tends to favour /DCTEncode, lossy JPEG encoding. The second set of options manually set the compression method to the lossless (de)flate encoding for colour and greyscale images and to CCITT encoding for monochrome images.