Back (Current repo: dotfiles)

my dotfiles to make it easy to transfer my settings
To clone this repository:
git clone https://git.viktor1993.net/dotfiles.git
Log | Download | Files | Refs

pdflabel (2667B)


#!/bin/bash

# When presenting documents or images as evidence at some US Courts, the individual pages must form
# a collection called "EXHIBIT <single letter>. Each page must be labeled with this, as well as
# a page number.

OUTPUT_DIR=/tmp/out-$(date '+%Y%m%d%H%M%S')
TEMPLATE_DIR=/tmp/exhibit-template
TITLE_TEMPLATE=$HOME/.config/custom/exhibit-template.tex
STAMP_TEMPLATE=$HOME/.config/custom/exhibit-stamp
CWD=$(pwd)

function add_exhibit_label() {

    echo "${FUNCNAME[0]}: Adding file $1"
    INPUT=$1
    LETTER=$2
    PAGE_NUMBER=$3

    if [ ! -f $INPUT ]; then
        echo "${FUNCNAME[0]}: Invalid input, file is missing."
        return 1
    fi

    OUTPUT_NAME=$(echo $INPUT | sed -E 's/\.[a-z]+$/-output.pdf/' | sed -E 's/(.*)\/([^/]+$)/\2/')

    pdftk ${OUTPUT_DIR}/${PAGE_NUMBER}-pagenumber.pdf background $INPUT output ${OUTPUT_DIR}/00${PAGE_NUMBER}-page.pdf
    rm ${OUTPUT_DIR}/${PAGE_NUMBER}-pagenumber.pdf
}

function create_exhibit_title_page() {

    LETTER=$1
    mkdir -p $TEMPLATE_DIR
    cat $TITLE_TEMPLATE > ${TEMPLATE_DIR}/000-exhibit.tex
    sed -i -E "s/@REPLACE@/$LETTER/" ${TEMPLATE_DIR}/000-exhibit.tex
    echo "${FUNCNAME[0]}: creating title page"
    pdflatex -output-directory=${CWD} ${TEMPLATE_DIR}/000-exhibit.tex >/dev/null 

    if [ -f ${CWD}/000-exhibit.pdf ]; then
        mv ${CWD}/000-exhibit.pdf ${OUTPUT_DIR}/000-exhibit.pdf
    fi
}

function merge_exhibit_files() {

    cd $OUTPUT_DIR
    echo "${FUNCNAME[0]}: merging files"
    gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=merged.pdf $(ls | grep -E "[0-9-]+[a-z]+.pdf$" | sort -V)
    cd $CWD

}


function main() {

    if [[ ! $1  =~ ^[a-zA-Z] ]]; then
        echo "${FUNCNAME[0]}: Please add a letter of exhibit"
        exit 1
    fi

    mkdir -p $OUTPUT_DIR

    EXHIBIT_LETTER=$(echo $1 | awk '{print toupper($0)}')

    NUMBER_OF_PAGES_NEEDED=$(ls $CWD | grep -Ec "\.pdf$")
    cat $STAMP_TEMPLATE > ${TEMPLATE_DIR}/000-template
    sed -i -E "s/%REPLACE_NUM%/$NUMBER_OF_PAGES_NEEDED/" ${TEMPLATE_DIR}/000-template
    sed -i -E "s/%REPLACE_EXHIBIT%/$EXHIBIT_LETTER/" ${TEMPLATE_DIR}/000-template

    cd $TEMPLATE_DIR
    bash ${TEMPLATE_DIR}/000-template
    mv ${TEMPLATE_DIR}/pagenumbers.pdf ${OUTPUT_DIR}/pagenumbers.pdf
    cd $OUTPUT_DIR
    pdfseparate pagenumbers.pdf %d-pagenumber.pdf

    cd $CWD
    c=1
    for FILE in $(find . -regextype posix-extended -regex '.*pdf$' -print0 | xargs -0 realpath | sort -V); do
        add_exhibit_label $FILE $EXHIBIT_LETTER $c
        let c++
    done

    create_exhibit_title_page $EXHIBIT_LETTER
    merge_exhibit_files $EXHIBIT_LETTER

    echo "Results in $OUTPUT_DIR"
}

main "$@"