Flatten PDFs almost painlessly.

I do a lot of presentations. When I export to PDF from OpenOffice, things usually work great. When I do these presentations online, though, the web tool will sometimes wreck the design: fonts get dropped, transparent backgrounds turn white… it’s a mess. So I need a way of turning each page of the PDF into an image so that it looks exactly the way I intend it to. That’s where this script comes from.

It splits each page of the PDF into PNGGIF files, and then re-packages the GIF files as a single PDF. Note that I chose 200125dpi resolution — that’s about as low as I could get without it ruining the antialiasing. The resulting file will be, of course, much larger than the original.

[Update: Dave Egts updated the script to clean up after itself and use an even smarter resolution. Thanks, Dave!]

#!/bin/sh

if [ -z "$1" ]; then echo $0 '<input file>'; exit 1; fi;

filename=$(basename "$1")
extension=${filename##*.}
filename=${filename%.*}

echo Creating images... && 
convert -verbose -density 125 -quality 10 -antialias $1 $filename-%03d.gif && 
echo Creating PDF from images... && 
convert -verbose $filename*.gif $filename-flat.pdf && 
echo Removing images... && 
rm $filename-*.gif

6 thoughts on “Flatten PDFs almost painlessly.

  1. Nice trick. I suspect you lose the ability to search the text of the document though, right?

    Like

  2. Oh, definitely. I usually keep a more sane PDF, and a “web-friendly” version. That's why I append “-flat” to the filename.

    Like

  3. Judging by the script, you mean that you convert the PDF to GIF files and then back to a PDF… or is the script typo-ed?

    Like

    1. Counterintuitive, but that’s what it does. Online presentation tools have a tendency to distort or “re-interpret” my PDFs, so I flatten the PDF into a series of bitmaps which I re-assemble into a new PDF. It’s the only way to ensure that the presentation will look exactly the way I want, no matter what fonts are available, etc.

      Like

      1. Yes, but the text of the article says that it converts it to PNG, not GIF. The script converts to GIF… Somehow I think I failed at my attempt to point out a typo.

        Like

Comments are closed.