How to invert a PDF using ImageMagick
The problem:
I wanted to print the extremely-twisted and NSFW Cards Against Humanity deck (available as a free PDF). The last 5 pages have white text on a black background, which kills my toner and looks awful (e.g. uneven toner placement, smudging, etc). I wanted to print these as black-on-white instead, but couldn’t find a black-on-white version of the PDF. Furthermore, I couldn’t figure out how to do it at print time (my laser printer doesn’t seem to have an “invert” option).
My solution:
I used ImageMagick’s command line “convert” tool to make an inverted version of the last 5 pages of the PDF. Assuming the original PDF is at “in.pdf”, we want to process pages 25-29, and spew the output into “out.pdf” the syntax is:
convert -density 300 -negate "in.pdf[24-28]" out.pdf
Here’s what it does:
-density 300- This tells “convert” to use a 300 DPI resolution when rendering the PDF as a raster image (i.e. reading the image). If the density is low, the output will look really blocky. 300 DPI looked good enough for my purposes. Yes, this means we’re converting a vector format to raster, and we should avoid lossy techniques. Again, this was good enough for me.
-negate- This tells “convert” to flip the color of the image (i.e., black becomes white and vice versa)
"in.pdf[24-28]"- This tells “convert” which file to read as input (in.pdf), and specifically which frames to read (24-28). In this case, a frame correlates to a PDF page number. Frames are 0-based (i.e. the first frame is “0″), so this really means to read pages 25-29.
out.pdf- This tells “convert” to write the output to a file called “out.pdf”.
And how it looks:

As you can see, I’m still left with a black blob on the last page of the PDF (which was previously white), but I’ll be tossing that anyway, so there’s no fear of smudging.
Would you have addressed the issue in a smoother way? Drop me a comment!
