Skip to content

Instantly share code, notes, and snippets.

@netizen-ais
Last active May 12, 2020 16:58
Show Gist options
  • Save netizen-ais/67c74f0c21925e582568deefd66266aa to your computer and use it in GitHub Desktop.
Save netizen-ais/67c74f0c21925e582568deefd66266aa to your computer and use it in GitHub Desktop.
#!/bin/bash
# sxi.mkpubf v2.0.27
i () { ${debug:-logger -t "${dname:-${0##*sxi.}}[$$]"} $(< /var/run/cpd.temp)º ${0##*sxi.} "${*}" ;}
e () { eid=${1}; shift; i "ERROR ${eid}: ${*}"; exit ${eid} ;}
while [ "${1:0:2}" == "--" ]; do declare _${1:2}="${1:2}"; [[ ${1} =~ = ]] && eval _${1:2}; shift; done #getopt
[[ ( ${_help} ) || $# -ne 2 ]] && grep -e "^# " $0 | cut -b3- && exit 0
[[ ! ( ${_noqueue} ) ]] && {
echo $$ >>${qlock:=/var/run/${0##*/}.PID}; trap "sed -ni '/$$/!p' ${qlock}" EXIT while read nextpid < ${qlock}; [ ${nextpid} != $$ ]; do sleep 1; done; }
set -- sxi2 ${*}
for argv in dname base name; do declare ${argv}=${1,,}; shift; done
if test -d ${base}; then
base=${base%/}/pdfs/; name=${name%.pdf}
cd $(mktemp --directory /tmp/${name}.XXX); shopt -s nullglob; touch ${name}
for pdf in ${base}[0-9]*pdf; do
filename=${pdf##*/}; cpage=$(( 10#${filename%%-*} )); echo -ne "\r ${cpage}"
if [[ ( ${_d2ss} ) && ( ${filename:0:3} != "${filename:4:3}" ) ]]; then #d2ss
if test $( pdfinfo ${pdf} | grep Pages | cut -b17- ) -eq 1; then
convert \
-density ${_d2ss:-288} \
${pdf} \
-crop 50%x100% \
-alpha off \
-page ${_page:-808x1106} \
x.png
for xfile in x*png; do
convert \
-density ${_d2ss:-288} ${xfile} \
-density 72 -page ${_page:-808x1106} ${cpage}.pdf
let cpage++
done
else
pdftk ${pdf} burst output x-%02d.pdf
for xfile in x-*pdf; do
if [[ ( ${_zgs} ) ]]; then
zpdf --page=${_page:-808x1106} ${_gsx} ${xfile} ${cpage}.pdf >/dev/null
else
mv ${xfile} ${cpage}.pdf
fi
let cpage++
done
fi
elif [[ ( ${_zgs} ) ]]; then
zpdf --page=${_page:-808x1106} --keep ${_gsx} ${pdf} ${cpage}.pdf >/dev/null
else
cp ${pdf} ${cpage}.pdf
fi
done; echo
[[ ( ${_lo2f} ) ]] && { : {,[1-9]{,[0-9]}}[0-9].pdf; last=${_%%.*}; [[ $(( ${last} % 2 )) -eq 1 ]] && mv {${last},1}.pdf; }
files=( {,[1-9]{,[0-9]}}[0-9].pdf )
# [[ ${_errok} == ${_errok/odd/} ]] \
# && [[ $(( ${#files[@]} % 2 )) -eq 1 ]] \
# && e 1 "Odd number of pages: ${#files[@]}"
if pdftk ${files[@]} cat output ${name}.pdf; then rm ${files[@]}; else exit; fi
else
cp ${base} ${name}; name=${name%.pdf}; OLDPWD=
fi
#[[ ${_errok} == ${_errok/odd/} ]] && [[ $(( $( pdfinfo ${name}.pdf | grep Pages | cut -b17- ) % 2 )) -ne 0 ]] && e 2 "Odd number of pages"
[[ ( ${_stamp} ) ]] && extra1="multistamp ${_stamp} ${extra1}"
[[ ( ${_lock} ) ]] && extra2="allow ${_allow} owner_pw ${_lock} ${extra2}"
[[ $(( ${#extra1} + ${#extra2} )) -ne 0 ]] \
&& pdftk ${name}.pdf ${extra1} output x.pdf ${extra2} \
&& mv x.pdf ${name}.pdf
[[ ( ${_gsz} ) ]] && {
zpdf --page=${_page:-808x1106} ${_gsx} ${name}.pdf
mv ${name}{z,}.pdf; }
#[[ ${#_forcegray} -ne 0 ]] TODO gs -sOutputFile=42bn.pdf -sDEVICE=pdfwrite -sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray -dCompibilityLevel=1.4 -dFixedMedia -dPDFFitPage -dDEVICEWIDTHPOINTS=808 -dDEVICEHEIGHTPOINTS=1106 -dNOPAUSE -dBATCH /media/net/region/srv/produccion/publicaciones/laregion/20120627/pdfs/042-042.pdf
[[ ( ${OLDPWD} ) ]] && { mv ${name}.pdf ~-; cd ~-; rm ~-/*; rmdir ~-; }
# sxi.mkpubf [--options] input_path output_name[.pdf]
# --options are:
# --noqueue Bypass queue control system, process in parallel
# --page=WIDTHxHEIGHT
# --d2ss[=DPI] Unmerge dual pages into proper ones. Use DPI density or default to 288
# --lo2f Bring back last page to first when it's (double and) odd
# --forcegray Remove color from BW pages" # TODO
# --stamp=PDF Insert PDF(file) as watermark
# --lock=PASSWD Protect using PASSWD
# --allow=PERMS Concede PERMS (Printing/DegradedPrinting/ModifyContents/CopyContents/AllFeatures
# --errok=TEST Don't stop because of TEST
# --zgs Preprocess with Ghostscript-GPL
# --gsz Postprocess with Ghostscript-GPL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment