Last active
July 10, 2020 17:22
-
-
Save AndersonTorres/a6fd6dc7862c93e4fdbe023176902dc1 to your computer and use it in GitHub Desktop.
Fish script to convert PDF to ZIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/fish | |
# pdf2zip.fish | |
function usage | |
printf " | |
Usage: pdf2zip.fish [Only one PDF file] | |
Transforms a single PDF file, passed under cmdline, to an equivalent ZIP file, | |
storing it in the current directory" | |
end | |
# TODO: test for only one directory | |
function cleanup | |
echo "Cleaning $argv.." | |
rm -rfv $argv | |
end | |
trap 'cleanup $TMPDIR' 2 15 | |
if test (count $argv) != 1 > /dev/null | |
usage | |
exit 1 | |
end | |
set CWD (pwd) | |
set TMPDIR (mktemp -d -p /tmp/ pdf2zip.XXXXXX) | |
if test "$TMPDIR" = "" | |
echo "Error creating temporary dir!" | |
exit 1 | |
end | |
set pdffile (realpath $argv) | |
set BASENAME (basename $pdffile) | |
set DIRNAME (dirname $pdffile) | |
set FILENAME "$DIRNAME/$BASENAME" | |
set ZIPNAME ""(echo $BASENAME|sed -e's/\.[a-zA-Z0-9]\{1,\}$/\.zip/g')"" | |
if [ (file $FILENAME | grep -c -e'PDF') != 1 ] | |
echo "$FILENAME doesn't appear to be a PDF document!" | |
cleanup $TMPDIR | |
exit 1 | |
end | |
cd $TMPDIR | |
rm -fr "$BASENAME"-dir | |
mkdir -p "$BASENAME"-dir | |
cd "$BASENAME"-dir | |
pdfimages -png "$FILENAME" (echo "$BASENAME" | sed 's|\....|-image|') | |
zip -0 -r ../"$ZIPNAME" . | |
cd .. | |
mv $ZIPNAME $CWD | |
cleanup $TMPDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment