-
Notifications
You must be signed in to change notification settings - Fork 0
/
make-pdf
executable file
·123 lines (113 loc) · 3.36 KB
/
make-pdf
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
# Prerequisites: xsltproc, prince (v13)
# Usage: ./make-pdf [-p EXPORTPATH] [-t TOOL] [-b BOOK_TYPE] FILE
# Examples: ./make-pdf A\ béke\ napja\ közel.xml
# ./make-pdf -p ../song "A béke napja közel.xml"
# ./make-pdf -p ../songs -t pagedjs "A béke napja közel.xml"
# ./make-pdf -p ../songs -t pagedjs -b public "A béke napja közel.xml"
# Exit immediately if a command exits with a non-zero status.
set -e
# Prerequisites
###############
if [ $(prince --version | grep -oP "Prince \K(\d+)") -lt 13 ]; then
echo "Minimum Prince version is 13. Please upgrade prince package."
exit 1
fi
# Params and variables
######################
help=$(cat <<'EOT'
Usage: ./make-pdf [options] FILE
-t Processing tool: prince, weasyprint, pagedjs
-p Path for export
-b Type of the book if any: 'public' or 'band'
EOT
)
exportpath="../export-pdf"
tool="prince"
while getopts "t:p:b:h" opt; do
case ${opt} in
t)
tool=$OPTARG
if ! [[ $tool =~ ^prince|weasyprint|pagedjs$ ]]; then
echo "Supported HTML2PDF converters are: prince, weasyprint and pagedjs."
echo "$help"
exit 1
fi
;;
p)
exportpath=$OPTARG
;;
b)
booktype=$OPTARG
;;
h)
echo "$help"
exit 0
;;
*)
echo "$help"
exit 1
;;
esac
done
file=${@:$OPTIND:1}
if [ -z "$file" ]; then
echo "FILE is not specified."
echo "$help"
exit 1
fi
if [ ! -f "$file" ]; then
echo "\"$1\" doesn't exist. Enter a valid XML file."
echo "$help"
exit 1
fi
name="$(basename "$file" .xml)"
pdfname=$name
pdftitle=$name
dir="$(dirname "$file")"
author="Gyuris Gellért" # Author for PDF
if [[ -n $booktype ]]; then
pdfname="${name/.xsl/ \($booktype\).xsl}"
pdftitle=$(xmllint --xpath "/*[local-name()='book']/*[local-name()='title']/text()" "$file") # xpath -e "/book/title/text()" -q "$file"
fi
# Running
#########
if [[ $file = *".xsl.xml" ]]; then
# Generate PDF from XSL(HTML) version
case "$tool" in
prince)
echo -n "Making PDF [Prince]... $file... "
cd $dir && xsltproc "$name.xml" $( [[ -n $booktype ]] && printf %s " --stringparam book-type $booktype" ) | prince -o "$exportpath/$pdfname.pdf" --pdf-author="$author" --pdf-title="$pdftitle" --no-warn-css-unsupported -
echo "done"
;;
weasyprint)
echo -n "Making PDF [WeasyPrint]... $file... "
cd $dir && xsltproc "$name.xml" $( [[ -n $booktype ]] && printf %s " --stringparam book-type $booktype" ) | weasyprint --quiet - "$exportpath/$pdfname.pdf"
echo "done"
;;
pagedjs)
echo -n "Making PDF [Paged.js]... $file... "
cd $dir
xsltproc "$name.xml" $( [[ -n $booktype ]] && printf %s " --stringparam book-type $booktype" ) > "$name.html"
pagedjs-cli -o "$exportpath/$pdfname.pdf" "$name.html"
rm -f "$name.html"
cd ..
echo "done"
;;
esac
else
# Generate PDF from original XML
case "$tool" in
prince)
echo -n "Making PDF [Prince]... $file... "
cd $dir && prince "$name.xml" -o "$exportpath/$pdfname.pdf" --pdf-author="$author" --pdf-title="$pdftitle" --no-warn-css-unsupported
echo "done"
;;
weasyprint)
echo "Weasyprint doesn't support processing XML... $file"
;;
pagedjs)
echo "Paged.js doesn't support processing XML... $file"
;;
esac
fi