-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert_html_to_pdf.sh
executable file
·60 lines (52 loc) · 1.17 KB
/
convert_html_to_pdf.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
#!/usr/bin/env bash
# Converts an HTML file to a PDF with standard formatting
#
# Usage: convert_html_to_pdf.sh [-r] <html_file>
# -r: remove the HTML file after creating the PDF
# <html_file>: the path to the HTML file to convert to PDF
if ! command -v wkhtmltopdf &> /dev/null
then
echo "wkhtmltopdf could not be found. Please install it first."
exit 1
fi
if [ $# -eq 0 ]; then
echo "No HTML file provided. Usage: $0 [-r] <html_file>"
exit 1
fi
remove_html=false
while getopts "r" opt; do
case "$opt" in
r )
remove_html=true
;;
\? )
echo "Invalid option: -$OPTARG" >&2
exit 1
;;
esac
done
shift $((OPTIND -1))
html_file="$1"
if [ ! -f "$html_file" ]; then
echo "File not found: $html_file"
exit 1
fi
output_pdf="${html_file%.html}.pdf"
wkhtmltopdf \
--page-size "Letter" \
--margin-top "0mm" \
--margin-right "0mm" \
--margin-bottom "0mm" \
--margin-left "0mm" \
--quiet \
"$html_file" "$output_pdf"
if [ $? -eq 0 ]; then
echo "PDF created successfully: $output_pdf"
open "$output_pdf"
if $remove_html; then
rm "$html_file"
echo "HTML file removed: $html_file"
fi
else
echo "Failed to create PDF."
fi