Skip to content

Instantly share code, notes, and snippets.

@jrk
Created April 1, 2009 19:34
Show Gist options
  • Save jrk/88855 to your computer and use it in GitHub Desktop.
Save jrk/88855 to your computer and use it in GitHub Desktop.
Quick and dirty PDF slide sharing: converts a PDF to PNGs and spits out HTML+JS to browse them.
#!/bin/sh
# Quick and Dirty PDF Sharing
# Converts a PDF to PNGs (using Image Magick)
# and spits out HTML+JS to browse them.
# By John Resig (ejohn.org)
# Distributed under the MIT License
# Home: http://ejohn.org/projects/easy-pdf-sharing/
# Input:
# ./pdfshare.sh some.pdf
# Output:
# some/*.png (PDF Images)
# some.html (HTML snippet)
# some.zip (Zip file of Images + PDF)
# Change this line to match your final upload destination
PREFIX="http://ejohn.org/files/"
# Leave the rest alone
PDF=$1
PDFPATH=`echo "${PDF}"|sed s/[^\\\/]*$//`
PDFNAME=`echo "${PDF}"|sed s/.pdf$//|sed s/^.*\\\///`
PNGDIR=${PDFPATH}${PDFNAME}
mkdir -p ${PNGDIR}
rm -f ${PNGDIR}/*.png
rm -f ${PNGDIR}.zip
convert -resize 400x400 ${PDF} ${PNGDIR}/%03d.png &> /dev/null
zip -r ${PNGDIR}.zip ${PNGDIR} ${PDF} &> /dev/null
NUM=`ls -la ${PNGDIR}/*.png | wc -l`
echo "<div id='${PDFNAME}' style='text-align:center;'><a href='${PREFIX}${PDF}'><img src='${PREFIX}${PNGDIR}/000.png'/><br/>Download PDF</a></div>"\
"<script>(function(){ var elem = document.getElementById('${PDFNAME}'), cur=0, max=${NUM}, d='${PREFIX}${PNGDIR}'; elem.innerHTML+=\""\
"<br/><a href='#${PDFNAME}' style='visibility:hidden;'>&laquo; Previous</a> <span>1/${NUM}</span> <a href='#${PDFNAME}'>Next &raquo;</a>"\
"\"; function pad(){ return cur < 10 ? '00' + cur : cur < 100 ? '0' + cur : '' + cur; }"\
"var a = elem.getElementsByTagName('a'); for(var i=1;i<3;i++){a[i].onclick=function(e){"\
"e=e||window.event; var dir = this.innerHTML.indexOf('Next')>=0?1:-1; cur+=dir;"\
"a[1].style.visibility=(cur==0?'hidden':'visible'); a[2].style.visibility=(cur==max-1?'hidden':'visible');"\
"elem.getElementsByTagName('img')[0].src=d+'/'+pad()+'.png';"\
"elem.getElementsByTagName('span')[0].innerHTML=(cur+1)+'/'+max;"\
"if (e.preventDefault) e.preventDefault(); e.returnValue = false;"\
"};}})();</script>" > ${PNGDIR}.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment