I'm trying to make a function that will make my life easier converting images to pdf. I'm using img2pdf
which is a Python script. I want to be able to pass files and directory to my function (with wild mark for expansion like: directory/*
).
I come to a point where I fix the issues with Windows path conversion, but then realize now the function doesn't work if the path has spaces, or it comes to a point where Python will just error on pathing.
I tried writing the function, making a string that contains all the files:
imgtopdf(){
list=""
while [[ "$1" != "" ]] ; do
list+=" '$(cygpath -w "$1")'" # Tried $(echo "$list" | xargs -0 echo) but it removes back-slashes!
shift
done
list="${list:1}"
echo $list
img2pdf -o img2pdf.pdf --pagesize A4 $list
}
Then tried using an array to hold all the files:
imgtopdf(){
list=()
while [[ "$1" != "" ]] ; do
list+="$(cygpath -w "$1")"
shift
done
echo ${list[@]}
img2pdf -o img2pdf.pdf --pagesize A4 ${list[*]}
}
Note: cygpath -w
is responsible for converting a /cygdrive/ path to Windows path, so that Python understands the pathing.