PDF Barcode Document Management CUPS Printer

Aus Primatenwiki
Zur Navigation springen Zur Suche springen

Add a postprocessing script for tagging the printed pdf documents with a barcode for using with a document management software

Install cups-pdf

 $ sudo apt install cups-pdf

Configure cups-pdf

Configure the output path in the cups-pdf configuration file /etc/cups/cups-pdf.conf

 $ sudo vi /etc/cups/cups-pdf.conf

Change

"Out ${HOME}/PDF" to "Out ${HOME}/Desktop" for saving the pdf file on the desktop.
Add the to be created post processing script in the postprocessing section:

PostProcessing /usr/local/bin/cups-pdf-barcode-tag

Create the PostProcessing script

 $ sudo vi /usr/local/bin/cups-pdf-barcode-tag

 #!/bin/bash

 FILENAME=$(basename $1)
 DIRNAME=$(dirname $1)
 firstpagefilepdf=/tmp/first.pdf
 firstpagefilepdfstamped=/tmp/firststamped.pdf
 restpagefilepdf=/tmp/first.pdf
 barcodefileps=/tmp/barcode.ps
 barcodefilepdf=/tmp/barcode.pdf
 barcodefilepdfrot=/tmp/barcoderot.pdf

 numberofpages=$(pdfinfo "$1" | grep Pages | awk '{print $2}')
 barcodetext="DMSD"$(date +'%Y%m%d%H%M%S')
 newfilename=$DIRNAME"/"${barcodetext}" "${FILENAME}

 #delete old tmp files
 rm ${firstpagefilepdf} ${restpagefilepdf} ${barcodefileps} ${barcodefilepdf} ${barcodefilepdfrot} ${firstpagefilepdfstamped} 2>/dev/null

 
 #generate barcode ps
 barcode -b "${barcodetext}" -o ${barcodefileps} -e 128 -g 180x20+300+795 -n

 #convert ps to pdf
 ps2pdf ${barcodefileps} ${barcodefilepdf}

 #rotate barcode pdf -90 degrese
 pdftk ${barcodefilepdf} cat 1-endwest output ${barcodefilepdfrot}

 #extract first page
 pdftk $1 cat 1 output ${firstpagefilepdf}

 #stamp first page with barcode
 pdftk ${firstpagefilepdf} stamp ${barcodefilepdfrot} output ${firstpagefilepdfstamped}

 #only if pdf contains more than 1 pages
 if $numberofpages -gt 1
 then
        #extract rest pages (2-x)
        pdftk $1 cat 2-end output ${restpagefilepdf}
        #merge first and rest pages to new output file
        pdftk ${firstpagefilepdfstamped} ${restpagefilepdf} cat output "${newfilename}"

 else
        pdftk ${firstpagefilepdfstamped} cat output "${newfilename}"
 fi

 #delete old tmp files
 rm ${firstpagefilepdf} ${restpagefilepdf} ${barcodefileps} ${barcodefilepdf} ${barcodefilepdfrot} ${firstpagefilepdfstamped} 2>/dev/null

 #delete original file
 rm $1 2>/dev/null
== Configure apparmor for allowing the postproccessing script to execute ==

 $ sudo vi /etc/apparmor.d/usr.sbin.cupsd

Add before the last closing curly bracket } the following line:

/usr/local/bin/cups-pdf-barcode-tag uxr,


Restart apparmor and cups

 $ sudo service apparmor restart

 $ sudo service cups restart