Last active
July 27, 2019 11:42
-
-
Save originalsouth/fc66da2544f7733e074e8904f09557aa to your computer and use it in GitHub Desktop.
pdf2websvg.zsh: mobile browsers do not support rendering PDFs but they do render SVGs so why not convert? (Oy! think about the bandwidth; httpd compression?)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
function convert | |
{ | |
PDF=$1 | |
FNAME=$(echo $PDF | cut -d\. -f1) | |
OUTPUT=$(echo $FNAME.svg) | |
PAGES=$(pdftk $PDF dump_data | grep NumberOfPages| awk '{print $2}') | |
for i in $(seq 1 $PAGES) | |
do | |
pdf2svg $PDF $FNAME-$i.svg $i | |
done | |
HEADER=$(tail -n +2 $FNAME-1.svg | head -n 1 | sed -e 's/>/\/>/g') | |
WIDTH=$(echo 'cat //@width' | xmllint --shell <(echo $HEADER) | awk -F'[="]' '!/>/{print $(NF-1)}' | tail -n 1 | sed -e 's/pt//g') | |
HEIGHT=$(echo 'cat //@height' | xmllint --shell <(echo $HEADER) | awk -F'[="]' '!/>/{print $(NF-1)}' | tail -n 1 | sed -e 's/pt//g') | |
printf '<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMinYMin meet" viewBox="0 0 %g %g" version="1.1">\n' $WIDTH $((PAGES*HEIGHT)) > $OUTPUT | |
for i in $(seq 1 $(($PAGES-1))) | |
do | |
k=$(($i-1)) | |
printf '<image id="%d" x="0" y="%g" width="%g" height="%g" href="%s-%d.svg"/>\n' $i $(($k*$HEIGHT)) $WIDTH $HEIGHT $FNAME $i >> $OUTPUT | |
printf '<line x1="0" y1="%g" x2="%g" y2="%g" stroke="black"/>\n' $(($i*$HEIGHT)) $WIDTH $(($i*$HEIGHT)) >> $OUTPUT | |
done | |
k=$(($PAGES-1)) | |
printf '<image id="%d" x="0" y="%g" width="%g" height="%g" href="%s-%d.svg"/>\n' $PAGES $(($k*$HEIGHT)) $WIDTH $HEIGHT $FNAME $PAGES >> $OUTPUT | |
printf '</svg>' >> $OUTPUT | |
} | |
if [[ "$#" -lt "1" ]] | |
then | |
echo "Please specify an input file" | |
else | |
for file in "$@" | |
do | |
convert $file | |
done | |
fi | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment