Monday, July 18, 2011

Making thumnails of all pdfs present in a folder structure

So I wanted to share a tip, it combines some bash scripting and ghostscript - for searching for all files recusively and then creating a thumbnail from the pdf (one for each page) and placing them in a newly create .thumb folder. This will overwrite any previous images - beware!





#!/bin/bash
# Loop over resources in directory, looking for pdf and image files to create thumbnails of
thumbdir="/.thumb"
thumbprefix=".thumbfile"
find . -name \*.pdf | while read file; do
        echo "Processing $file"
        base=${file##*/}
        directory=${file%/*}
        mkdir "${directory}${thumbdir}"
        gs -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dNOPAUSE -dBATCH -sDEVICE=jpeg  -g450x600 -dPDFFitPage  -r9.06531732174037 -sOutputFile=${directory}${thumbdir}/${thumbprefix}.%d.jpg -c "save$
currentglobal true
setglobal false/product where{pop product(Ghostscript)search{pop pop pop revision 600 ge{pop true}if}{pop}ifelse}if{/pdfdict where{pop pdfdict
begin/pdfshowpage_setpage[pdfdict/pdfshowpage_setpage get{dup type/nametype eq{dup/OutputFile eq{pop/AntiRotationHack}{dup/MediaBox eq revision 650 ge and{/THB.CropHack{1 index/CropBox
pget{2 index exch/MediaBox exch put}if}def/THB.CropHack cvx}if}ifelse}if}forall]cvx def end}if}if setglobal" -f $file
done


Now this could be done in a more compact script, but I re-use this for-each file structure in a bunch of like scripts.

No comments:

Post a Comment