-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfflatten.sh
79 lines (60 loc) · 2.04 KB
/
pdfflatten.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
79
#!/bin/bash
# Author: Damodar Rajbhandari (2020)
# Email: [email protected]
#--------------------------------------
# Defining Shells coloring
#----------------------------
NONE='\033[00m'
RED='\033[01;31m'
CYAN='\033[01;36m'
UNDERLINE='\033[4m'
# Writing Help message
#-----------------------
if [ "$1" == "-h" ]; then
echo "Author: Damodar Rajbhandari (2020)"
echo "Usage: ./pdflatten.sh file.pdf"
echo "For more pdf files, Usage: ./pdflatten.sh file1.pdf file2.pdf <and so on>"
exit 0
fi
# Checking Ghostscript program install or not
# ---------------------------------------------
if ! which pdf2ps &> /dev/null; then
echo -e "${RED}Namaste, Please install the Ghostscript in your machine!${NONE}"
exit 1
fi
# Abort the program if no pdf file provided!
#---------------------------------------------
if [ $# -eq 0 ]; then
echo -e "${CYAN}Namaste, Please provide the pdf file!${NONE}"
echo -e "${RED}No pdf file provided!${NONE}"
echo "For help: ./pdflatten.sh -h"
exit 1
fi
for var in "$@"
do
echo "Flattening the pdf:" $var"."
if [[ "$var" =~ ( |\') ]];
then
# If pdf with spaces provided, copy it by renaming.
# Because pdf2ps and ps2pdf in Ghostscript cannot handle
#--------------------------------------------------------
$(cp "$var" "${var// /_}_renamed.pdf")
# The real things happen from below
#------------------------------------
start=`date +%s`
pdf2ps ${var// /_}_renamed.pdf - | ps2pdf - ${var// /_}_Flattened.pdf
end=`date +%s`
echo "Time taken to finish flattening:" $(date -ud "@$((end-start))" +'%H hours %M minutes %S seconds').
# Delete the copied then renamed pdf
#-------------------------------------
$(rm "${var// /_}_renamed.pdf")
else
# The real things happen from below
#------------------------------------
start=`date +%s`
pdf2ps ${var} - | ps2pdf - ${var}_Flattened.pdf
end=`date +%s`
echo "Time taken to finish flattening:" $(date -ud "@$((end-start))" +'%H hours %M minutes %S seconds').
fi
done
echo "Finished flattening the given pdf(s)!"