Explorar el Código

pdfmerge-smart main code

Guzmán Castanedo Villalba hace 6 años
padre
commit
e1b6be4ea0
Se han modificado 1 ficheros con 142 adiciones y 0 borrados
  1. 142 0
      pdfmerge-smart

+ 142 - 0
pdfmerge-smart

@@ -0,0 +1,142 @@
+#!/bin/bash
+#/##################################################################\
+#| Script to merge multiple pdf's in one file adding white pages    |
+#| if the the number of each one is odd.                            |
+#| Guzmán Castanedo (guzman@castanedo.es)                           |
+#| February 2017                                                    |
+#| Licence: GPL 3.0 -> https://www.gnu.org/licenses/gpl-3.0.en.html |
+#\##################################################################/
+
+function usage {
+	printf "USAGE:\t"$(basename "$0")" [PDFin.pdf] <PDFout.pdf>\n"
+	printf "INFO:\n"
+	printf "\tMerge PDFin.pdf in one PDFout.pdf adding white pages if PDFin.pdf\n"
+	printf "\thas an odd number of pages.\n"
+	printf "OPTIONS:\n"
+	printf "\t-h, --help help\n"
+	printf "\t-v, --verbose verbose mode\n"
+	printf "\t-y, --yes enable overwrite mode\n"
+}
+
+#Check if pdfunite, pdfinfo and convert are installed
+echo
+if [ ! -x /usr/bin/pdfinfo ]; then
+	printf "ERROR: pdfinfo is not installed\n"
+	exit 1
+fi
+if [ ! -x /usr/bin/pdfunite ]; then
+	printf "ERROR: pdfunite is not installed\n"
+	exit 1
+fi
+if [ ! -x /usr/bin/convert ]; then
+	printf "ERROR: convert is not installed\n"
+	exit 1
+fi
+
+#Parse args
+verbose=false
+overwrite=false
+#With getopts
+TEMP="$(getopt -q -o hvy --long help,verbose,yes -n "$(basename "$0")" -- "$@")"
+eval set -- "$TEMP"
+unset TEMP
+while true; do
+	case "$1" in
+		-h|--help)
+			#Help
+			usage
+			exit
+			;;
+		-v|--verbose)
+			#Verbose
+			verbose=true
+			shift
+			;;
+		-y|--yes)
+			#Overwrite
+			overwrite=true
+			shift
+			;;
+		--)
+			#Last one
+			shift
+			break
+			;;
+		*)
+			#Unspected
+			usage
+			echo "ERROR: Invalid option $1"
+			exit 1
+			;;
+	esac
+done
+#Create white pdf (random name)
+whitepdf=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
+whitepdf="$whitepdf.pdf"
+while [ -e $whitepdf ]; do
+	#Extremly rare, but possible->Choose another one
+	whitepdf=$(cat /dev/urandom | tr -dc 'a-z0-9' | fold -w 32 | head -n 1)
+	whitepdf="$whitepdf.pdf"
+done
+#Make whitepdf with convert
+convert xc:none -page A4 $whitepdf
+if [ $verbose = true ];then
+	printf "White PDF:\t%s\n" $whitepdf
+fi
+
+#Parse arguments
+unite="pdfunite"
+cont=0
+for i in $@; do
+	cont=$((cont+1))
+	unite+=" $i"
+	if [ $cont -lt $# ]; then
+		temp=$(pdfinfo $i 2>/dev/null)
+		if [ ! $? -eq 0 ]; then
+			printf "ERROR: %s aren't a valid PDF file.\n" $i
+			usage
+			exit 1
+		fi
+		pages=$(pdfinfo $i|grep Pages:|awk '{print $2}')
+		if [ $verbose = true ]; then
+			printf "PDFin:\t%s (%s Pages)\n" $i $pages
+		fi
+		if [ ! $((pages%2)) -eq 0 ]; then
+			#Odd number
+			unite+=" $whitepdf"
+		fi
+	else
+		#Last one
+		if [ $overwrite = false ]; then
+			if [ -e $i ]; then
+				printf "Warning: Output file $i exists. Overwrite? [y/n]: "
+				read confirm
+				case ${confirm:0:1} in
+					y|Y)
+						;;
+					*)
+						rm $whitepdf
+						exit 1
+						;;
+				esac
+			fi
+		fi
+		#Execute pdfunite
+		$($unite)
+		if [ ! $? -eq 0 ];then
+			printf "Warning: something was wrong\n"
+		else
+			printf "Creation Successful\n"
+			if [ $verbose = true ]; then
+				pages=$(pdfinfo $i|grep Pages:|awk '{print $2}')
+				printf "PDFOut:\t$s (%s Pages)\n" $i $pages
+			fi
+		fi
+	fi
+done
+
+#Remove whitepdf
+if [ $verbose = true ]; then
+	printf "Deletting temporal files\n"
+fi
+rm $whitepdf