-
Notifications
You must be signed in to change notification settings - Fork 88
/
pdf2pptx.sh
executable file
·139 lines (118 loc) · 3.7 KB
/
pdf2pptx.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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
# Alireza Shafaei - [email protected] - Jan 2016
resolution=1024
density=300
#colorspace="-depth 8"
colorspace="-colorspace sRGB -background white -alpha remove"
makeWide=true
if [ $# -eq 0 ]; then
echo "No arguments supplied!"
echo "Usage: ./pdf2pptx.sh file.pdf"
echo " Generates file.pdf.pptx in widescreen format (by default)"
echo " ./pdf2pptx.sh file.pdf notwide"
echo " Generates file.pdf.pptx in 4:3 format"
exit 1
fi
if [ $# -eq 2 ]; then
if [ "$2" == "notwide" ]; then
makeWide=false
fi
fi
echo "Doing $1"
tempname="$1.temp"
if [ -d "$tempname" ]; then
echo "Removing ${tempname}"
rm -rf "$tempname"
fi
mkdir "$tempname"
# Set return code of piped command to first nonzero return code
set -o pipefail
n_pages=$(identify "$1" | wc -l)
returncode=$?
if [ $returncode -ne 0 ]; then
echo "Unable to count number of PDF pages, exiting"
exit $returncode
fi
if [ $n_pages -eq 0 ]; then
echo "Empty PDF (0 pages), exiting"
exit 1
fi
for ((i=0; i<n_pages; i++))
do
convert -density $density $colorspace -resize "x${resolution}" "$1[$i]" "$tempname"/slide-$i.png
returncode=$?
if [ $returncode -ne 0 ]; then break; fi
done
if [ $returncode -eq 0 ]; then
echo "Extraction succ!"
else
echo "Error with extraction"
exit $returncode
fi
if (which perl > /dev/null); then
# https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac#comment47931362_1115074
mypath=$(perl -MCwd=abs_path -le '$file=shift; print abs_path -l $file? readlink($file): $file;' "$0")
elif (which python > /dev/null); then
# https://stackoverflow.com/questions/1055671/how-can-i-get-the-behavior-of-gnus-readlink-f-on-a-mac#comment42284854_1115074
mypath=$(python -c 'import os,sys; print(os.path.realpath(os.path.expanduser(sys.argv[1])))' "$0")
elif (which ruby > /dev/null); then
mypath=$(ruby -e 'puts File.realpath(ARGV[0])' "$0")
else
mypath="$0"
fi
mydir=$(dirname "$mypath")
pptname="$1.pptx.base"
fout=$(basename "$1" .pdf)".pptx"
rm -rf "$pptname"
cp -r "$mydir"/template "$pptname"
mkdir "$pptname"/ppt/media
cp "$tempname"/*.png "$pptname/ppt/media/"
function call_sed {
if [ "$(uname -s)" == "Darwin" ]; then
sed -i "" "$@"
else
sed -i "$@"
fi
}
function add_slide {
pat='slide1\.xml\"\/>'
id=$1
id=$((id+8))
entry='<Relationship Id=\"rId'$id'\" Type=\"http:\/\/schemas\.openxmlformats\.org\/officeDocument\/2006\/relationships\/slide\" Target=\"slides\/slide-'$1'\.xml"\/>'
rep="${pat}${entry}"
call_sed "s/${pat}/${rep}/g" ../_rels/presentation.xml.rels
pat='slide1\.xml\" ContentType=\"application\/vnd\.openxmlformats-officedocument\.presentationml\.slide+xml\"\/>'
entry='<Override PartName=\"\/ppt\/slides\/slide-'$1'\.xml\" ContentType=\"application\/vnd\.openxmlformats-officedocument\.presentationml\.slide+xml\"\/>'
rep="${pat}${entry}"
call_sed "s/${pat}/${rep}/g" ../../\[Content_Types\].xml
sid=$1
sid=$((sid+256))
pat='<p:sldIdLst>'
entry='<p:sldId id=\"'$sid'\" r:id=\"rId'$id'\"\/>'
rep="${pat}${entry}"
call_sed "s/${pat}/${rep}/g" ../presentation.xml
}
function make_slide {
cp ../slides/slide1.xml ../slides/slide-$1.xml
cat ../slides/_rels/slide1.xml.rels | sed "s/image1\.JPG/slide-${slide}.png/g" > ../slides/_rels/slide-$1.xml.rels
add_slide $1
}
pushd "$pptname"/ppt/media/
count=`ls -ltr | wc -l`
for (( slide=$count-2; slide>=0; slide-- ))
do
echo "Processing "$slide
make_slide $slide
done
if [ "$makeWide" = true ]; then
pat='<p:sldSz cx=\"9144000\" cy=\"6858000\" type=\"screen4x3\"\/>'
wscreen='<p:sldSz cy=\"6858000\" cx=\"12192000\"\/>'
call_sed "s/${pat}/${wscreen}/g" ../presentation.xml
fi
popd
pushd "$pptname"
rm -rf ../"$fout"
zip -q -r ../"$fout" .
popd
rm -rf "$pptname"
rm -rf "$tempname"