#!/usr/bin/env bash # HEADER #======================================================================= # # commission -- Extract some Academic Records from a PDF file. # # SYNOPSIS # commission output # # DESCRIPTION # Shell script to extract one or more pages of a PDF file from an # integer or a list of integers corresponding to academic records. # # POSITIONAL ARGUMENT # PDF infile name # # OPTIONS # -h|--help display this help and exit # -n|num an integer or a csv file of integers # -o|output PDF outfile name # # EXAMPLES # $ commission infile.pdf -n 1234 -v # $ commission infile.pdf -n 1234 -o case_1234.pdf # $ commission infile.pdf -n list.csv -o comm_1.pdf # # DEPENDENCIES # - pdfgrep # - pdftk # - GNU coreutils # - PDF viewer, like evince # # Copyright (C) 2019,2020 Nicolas Mesnier # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License version 3 or # above as published by the Free Software Foundation. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # #======================================================================= # END_OF_HEADER #----------------------------------------------------------------------- Help(){ # Read header to display help and version BOH=$(head -200 ${0} | grep -n "^# HEADER" | cut -f1 -d:) EOH=$(head -200 ${0} | grep -n "^# END_OF_HEADER" | cut -f1 -d:) head -$(($EOH-1)) ${0} | tail -$(($EOH-$BOH-1)) \ | grep -e "^#$" -e "^# " | sed -e "s/^#=*//g" } #----------------------------------------------------------------------- __getPages__(){ # Get pages numbers of case number ocurence in a PDF file # Input: # $1: PDF file name (str) # $2: case number (int) # Output: # str: page range as "n1-n2" echo "$(pdfgrep --cache -n n°${2} ${1} | cut -d: -f1 \ | awk '{if(min==""){min=max=$1}; if($1>max) {max=$1}; \ if($1&2 exit 1 fi fi shift ;; -o|output) if [ -f ${2} ]; then read -p " File \"${2}\" allready exists. Replace? y/[n] " rep case ${rep} in [Yy]* ) outfile=${2} ;; * ) exit 1;; esac else outfile=${2} fi shift ;; -v|view) noOutPut=true ;; *) if [ -f ${1} ];then infile=${1} else echo "File \"${1}\" does not exists." >&2 exit 1 fi ;; esac if ! shift; then echo 'Missing parameter argument.' >&2 exit 1 fi done #----------------------------------------------------------------------- # *** run #----------------------------------------------------------------------- if [ -z "${num}" -o -z "${num_file}" ];then if [ ! -z ${num_file} ];then pages="" for num in $(cat ${num_file} | xargs);do pages+=" $(__getPages__ ${infile} ${num})" done else pages=$(__getPages__ ${infile} ${num}) fi if [ ${noOutPut} = true ];then tmpfile=${infile%%.pdf}"_"${pages}".pdf" pdftk ${infile} cat ${pages} output ${tmpfile} \ && ${viewer} ${tmpfile} && rm ${tmpfile} else pdftk ${infile} cat ${pages} output ${outfile} fi fi #====================================================================eof # vim: set tw=72 ts=4 sw=4 nu: