#!/bin/bash #B. Vandeportaele 2018 OUTPUT_DIR="./pdf/" OUTPUT_FILENAME=cours_comm #check that soffice is not already run SOFFICE_RUNNING=$(pstree | grep soffice) if [ ! -z "$SOFFICE_RUNNING" ]; then echo soffice have to be closed before lauching this script! echo save your data and try pkill soffice exit -1 fi #create output directory mkdir -p ${OUTPUT_DIR} #generate list and convert files to pdf if required FILE_LIST="" while read line #for each line of list do echo processing ${line} if [ ! -z "${line}" ]; #avoid processing empty lines then OFFICE_FILE=${line} PDF_FILE="${OFFICE_FILE/".odp"/".pdf"}" PDF_FILE_WITH_FOLDER="${OUTPUT_DIR}${PDF_FILE}" # test if the office file is newer than the pdf, it works also if the pdf file is not existing if [ ${OFFICE_FILE} -nt ${PDF_FILE_WITH_FOLDER} ] then echo "this file has changed, converting it to pdf: ${OFFICE_FILE} -> ${PDF_FILE_WITH_FOLDER}" rm ${PDF_FILE_WITH_FOLDER} #convert each individual file to pdf echo "soffice --convert-to pdf --outdir ${OUTPUT_DIR} ${OFFICE_FILE}" soffice --convert-to pdf --outdir ${OUTPUT_DIR} ${OFFICE_FILE} else echo "this file has not changed, keeping the previous pdf: ${OFFICE_FILE} -> ${PDF_FILE_WITH_FOLDER}" fi #add to the list of file in variable and subsitute odp with pdf FILE_LIST="${FILE_LIST} ${OUTPUT_DIR}${line/".odp"/".pdf"}" fi done < listoffilestoconcatenate.txt #I'm obliged to use while .... in that order so the scope of ${FILE_LIST} is not only local inside the loop echo list of files: ${FILE_LIST} #concatenate the files to a single one gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=${OUTPUT_DIR}${OUTPUT_FILENAME}.pdf -dBATCH ${FILE_LIST} #count the number of pages in the concatened file # https://stackoverflow.com/questions/1672580/get-number-of-pages-in-a-pdf-using-a-cmd-batch-file NUMBEROFPAGES=$(pdfinfo "${OUTPUT_DIR}${OUTPUT_FILENAME}.pdf" | grep Pages | sed 's/[^0-9]*//') echo $NUMBEROFPAGES pages found #set the right number of pages to generate in numbers.tex from the generic numbers.in.tex file sed "s/NUMBEROFPAGESTOGENERATE/$NUMBEROFPAGES/g" ${OUTPUT_DIR}numbers.tex cat ${OUTPUT_DIR}numbers.tex #compute the pdf for pages numbers pdflatex -output-directory ${OUTPUT_DIR} ${OUTPUT_DIR}numbers.tex #merge the pdf containing the content and the pdf containing the page numbers # http://cazencott.info/index.php/post/2015/04/30/Numbering-PDF-Pages # sudo apt install pdftk if false then #split files to individual pages pdftk ${OUTPUT_DIR}numbers.pdf burst output ${OUTPUT_DIR}number_%03d.pdf pdftk ${OUTPUT_DIR}${OUTPUT_FILENAME}.pdf burst output ${OUTPUT_DIR}page_%03d.pdf for i in $(seq -f %03g 1 $NUMBEROFPAGES) ; do pdftk ${OUTPUT_DIR}page_$i.pdf stamp ${OUTPUT_DIR}number_$i.pdf ${OUTPUT_DIR}output new_$i.pdf ; done pdftk ${OUTPUT_DIR}new_???.pdf output ${OUTPUT_DIR}${OUTPUT_FILENAME}_numbered.pdf else #direct method dealing with the 2 files without merging individual pages pdftk ${OUTPUT_DIR}${OUTPUT_FILENAME}.pdf multistamp ${OUTPUT_DIR}numbers.pdf output ${OUTPUT_DIR}${OUTPUT_FILENAME}_numbered.pdf fi #generate the 6 slides per page for printing pdfnup --nup 2x3 --no-landscape -o ${OUTPUT_DIR}${OUTPUT_FILENAME}_numbered_six.pdf ${OUTPUT_DIR}${OUTPUT_FILENAME}_numbered.pdf #add annexes if required....