Sometimes, especially when I create PDF files with lots of images, I end up with huge PDF files, sometimes in the gigabyte range, even though the actual images themself were just a couple of megabytes.
These files aren't just impractical, but most printers can't handle them so basically they are useless.
While it's probably better to tackle the problem where it begins (in my case, the PDF file printer driver) I decided to go for a more 'hacky' approach: I wrote a script which cleans and recompresses the PDF files. This is useful because I noticed that quite a lot of PDF files (especially ones I didn't create myself) are unnecessarily huge and contain PDF errors.
This script passes the PDFs through all available ghostscript filters until it ends up with a nice new and shiny PDF.
Warning: the process is ressource-consuming, can take a while and will definitely take up some disk space.
By now way do I guarantee for any file loss or damage. USE AT YOUR OWN RISK.
<br />
#!/bin/bash<br />
<br />
if [ -z $1 ]; then<br />
echo "Usage: cleanpdf infile.pdf [outfile.pdf]"<br />
exit 1<br />
fi<br />
<br />
infile=$1<br />
wipe=false<br />
<br />
if [ -z $2 ]; then<br />
echo no thingy given<br />
outfile=$1<br />
wipe=true<br />
else<br />
if [ -e $2 ]; then<br />
echo "Error: file $2 already exists, won't overwrite."<br />
exit 2<br />
fi<br />
outfile=$2<br />
fi<br />
<br />
tmpfile1=tmpfilecleanpdf123131212.ps<br />
tmpfile2=tmpfilecleanpdf123131412.ps<br />
<br />
echo "Converting to PostScript..."<br />
pdf2ps $infile $tmpfile1<br />
echo "Cleaning PostScript..."<br />
ps2ps $tmpfile1 $tmpfile2<br />
if [ $wipe = true ]; then<br />
echo "Wiping original..."<br />
rm $infile<br />
fi<br />
echo "Converting clean PostScript to PDF..."<br />
ps2pdf $tmpfile2 $outfile<br />
rm $tmpfile1 $tmpfile2<br />
echo "Done."<br />