1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
|
#!/bin/bash
#
# Convert PDF to hybrid HTML output with images and line art rendered to a
# background image, and text overlaid on top as absolutely positioned HTML
# text.
input=$1
out=${2:-out}
fmt=${3:-png}
dpi=${4:-96}
scale=$(expr 72 '*' $dpi / 96)
if test -f "$1"
then
echo Processing "$input" out=$out fmt=$fmt dpi=$dpi
else
echo "usage: pdftohtml.sh input.pdf output-stem image-format dpi"
echo " example: pdftohtml.sh input.pdf output png 96"
exit
fi
title=$(basename "$input" | sed 's/.pdf$//')
mutool convert -Oresolution=$dpi -o $out.html "$input"
sed -i -e "/<head>/a<title>$title</title>" $out.html
sed -i -e "/^<div/s/page\([0-9]*\)\" style=\"/page\1\" style=\"background-image:url('$out\1.$fmt');/" $out.html
mutool draw -K -r$dpi -o$out%d.png "$input"
echo Converting to $fmt
for png in $out*.png
do
xxx=$(basename $png .png).$fmt
case $fmt in
png)
if command -v optipng >/dev/null
then
optipng -silent -strip all $png
fi
;;
jpg)
if command -v mozjpeg >/dev/null
then
mozjpeg -outfile $xxx $png
else
convert -format $fmt $png $xxx
fi
;;
webp)
if command -v cwebp >/dev/null
then
cwebp -quiet -o $xxx $png
else
convert -format $fmt $png $xxx
fi
;;
*)
convert -format $fmt $png $xxx
;;
esac
done
|