forked from styluslabs/templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf2write.sh
executable file
·78 lines (69 loc) · 2.93 KB
/
pdf2write.sh
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# Create a Write document from a PDF by generating page images
FOREGROUND=false
COMPRESS=true
PDFIN=''
# https://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
-f|--fg|--foreground)
FOREGROUND=true
shift # past argument
;;
--nozip)
COMPRESS=false
shift # past argument
;;
#-d|--dpi)
#DPI="$2"
#shift # past argument
#shift # past value
#;;
*) # unknown option
PDFIN="$1"
shift # past argument
;;
esac
done
if [ -z "$PDFIN" ]
then
echo "pdf2write.sh: Convert PDF to svg document for Stylus Labs Write"
echo "Usage: pdf2write.sh [options] [PDF-file]"
echo " -f,--fg,--foreground: place page images in editable layer instead of ruling layer"
echo " --nozip: generate uncompressed svg instead of svgz"
exit 1
fi
echo "Converting $PDFIN to images..."
(command -v pdftoppm >/dev/null 2>&1 && pdftoppm -png -r 300 "$PDFIN" out) || convert -density 300 -scene 1 "$PDFIN" out-%03d.png
if [ ! -f "out-1.png" ] && [ ! -f "out-01.png" ] && [ ! -f "out-001.png" ]; then
echo "No page images found: make sure pdftoppm (from poppler-utils) or imagemagick and ghostscript are installed"
exit 1
fi
SVGOUT=$(basename "$PDFIN" pdf)svg
echo "Generating Write document..."
printf '<svg id="write-document" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\n' > "$SVGOUT"
printf '<rect id="write-doc-background" width="100%%" height="100%%" fill="#808080"/>\n' >> "$SVGOUT"
for PNGPAGE in out-*.png
do
read WIDTH2 HEIGHT2 < <(file $PNGPAGE | cut -d "," -f 2 | cut -d " " -f 2,4)
# page images generated at 300 DPI but Write uses 150 DPI as reference
WIDTH=$((WIDTH2/2))
HEIGHT=$((HEIGHT2/2))
printf '<svg class="write-page" color-interpolation="linearRGB" x="10" y="10" width="%dpx" height="%dpx" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">\n' $WIDTH $HEIGHT >> "$SVGOUT"
printf ' <g class="write-content write-v3" xruling="0" yruling="40" marginLeft="100" papercolor="#FFFFFF" rulecolor="#9F0000FF">\n' >> "$SVGOUT"
printf ' <g class="ruleline write-no-dup" shape-rendering="crispEdges">\n' >> "$SVGOUT"
printf ' <rect class="pagerect" fill="#FFFFFF" stroke="none" x="0" y="0" width="%d" height="%d" />\n' $WIDTH $HEIGHT >> "$SVGOUT"
[ "$FOREGROUND" = true ] && printf ' </g>\n' >> "$SVGOUT" # end ruleline <g>
printf ' <image x="0" y="0" width="%d" height="%d" xlink:href="data:image/png;base64,' $WIDTH $HEIGHT >> "$SVGOUT"
# doesn't seem to be a way to prevent base64 from appending newline
base64 $PNGPAGE | tr -d '\n' >> "$SVGOUT"
printf '"/>\n' >> "$SVGOUT" # end <image>
[ "$FOREGROUND" = true ] || printf ' </g>\n' >> "$SVGOUT" # end ruleline <g>
printf ' </g>\n</svg>' >> "$SVGOUT" # end content <g> and page
done
printf "\n</svg>" >> "$SVGOUT"
[ "$COMPRESS" = true ] && gzip -S z "$SVGOUT"
rm out-*.png
echo "Finished creating $SVGOUT"z