diff --git a/OCRmyPDF.sh b/OCRmyPDF.sh index 71a6d3e8..dc331b8e 100644 --- a/OCRmyPDF.sh +++ b/OCRmyPDF.sh @@ -6,6 +6,8 @@ TOOLNAME="OCRmyPDF" VERSION="v1.0-rc2" +START=`date +%s` + usage() { cat << EOF -------------------------------------------------------------------------------------- @@ -150,11 +152,12 @@ cd "`dirname $0`" today=$(date +"%Y%m%d_%H%M") fld=$(basename "$FILE_INPUT_PDF" | sed 's/[.][^.]*//') TMP_FLD="./tmp/$today.filename.$fld" +FILE_TMP="$TMP_FLD/tmp.txt" # temporary file with a very short life used for several things) FILE_SIZE_PAGES="$TMP_FLD/page-sizes.txt" # size in pt of the respective page of the input PDF file FILES_OCRed_PDFS="${TMP_FLD}/*-ocred.pdf" # string matching all 1 page PDF files that need to be merged FILE_OUTPUT_PDF_CAT="${TMP_FLD}/ocred.pdf" # concatenated OCRed PDF files FILE_OUTPUT_PDFA_WO_META="${TMP_FLD}/ocred-pdfa-wo-metadata.pdf" # PDFA file before appending metadata -FILE_VALIDATION_LOG="${TMP_FLD}/pdf_validation.log" # log file containing the results of the validation of the PDF/A file +FILE_VALIDATION_LOG="${TMP_FLD}/pdf_validation.log" # log file containing the results of the validation of the PDF/A file # Create tmp folder [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Creating temporary folder: \"$TMP_FLD\"" @@ -168,22 +171,21 @@ mkdir -p "${TMP_FLD}" [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Input file: Extracting size of each page (in pt)" ! identify -format "%w %h\n" "$FILE_INPUT_PDF" > "$FILE_SIZE_PAGES" \ && echo "Could not get size of PDF pages. Exiting..." >&2 && exit $EXIT_BAD_INPUT_FILE -sed -I "" '/^$/d' "$FILE_SIZE_PAGES" # removing empty lines (last one should be) +sed -i "" '/^$/d' "$FILE_SIZE_PAGES" # removing empty lines (last one should be) numpages=`cat "$FILE_SIZE_PAGES" | wc -l | sed 's/^ *//g'` -[ $VERBOSITY -ge $LOG_INFO ] && echo "Input file: The file has $numpages pages" +numpages=`printf "%04d" $numpages` # add leading zeros to the page number # Itterate the pages of the input pdf file cpt="1" while read pageSize ; do - # add leading zeros to the page number - page=`printf "%04d" $cpt` - [ $VERBOSITY -ge $LOG_INFO ] && echo "Processing page $page" + page=`printf "%04d" $cpt` # add leading zeros to the page number + [ $VERBOSITY -ge $LOG_INFO ] && echo "Processing page $page / $numpages" # create the name of the required file curOrigImg="$TMP_FLD/${page}_Image" # original image available in the current PDF page - # (the image file may have a different orientation than in the pdf file) - curHocr="$TMP_FLD/$page.hocr" # hocr file to be generated by the OCR SW for the current page + # (the image file may have a different orientation than in the pdf file) + curHocr="$TMP_FLD/$page.hocr" # hocr file to be generated by the OCR SW for the current page curOCRedPDF="$TMP_FLD/${page}-ocred.pdf" # PDF file containing the image + the OCRed text for the current page curOCRedPDFDebug="$TMP_FLD/${page}-debug-ocred.pdf" # PDF file containing data required to find out if OCR worked correctly @@ -193,7 +195,7 @@ while read pageSize ; do [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Page $page: size ${heightPDF}x${widthPDF} (h*w in pt)" # extract raw image from pdf file to compute resolution # unfortunatelly this image can have another orientation than in the pdf... - # so we will have to extract it again later + # so we will have to extract it again later using pdftoppm pdfimages -f $page -l $page -j "$FILE_INPUT_PDF" "$curOrigImg" 1>&2 # count number of extracted images nbImg=`ls -1 "$curOrigImg"* | wc -l` @@ -215,10 +217,17 @@ while read pageSize ; do fi [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Page $page: size ${heightCurImg}x${widthCurImg} (h*w pixel)" # compute the resolution of the image - dpi_x=$(($widthCurImg*72/$widthPDF)) - dpi_y=$(($heightCurImg*72/$heightPDF)) - [ "$dpi_x" -ne "$dpi_y" ] && echo "X/Y resolutions not equal ($dpi_x/$dpi_y). Exiting..." >&2 && exit $EXIT_BAD_INPUT_FILE - dpi="$dpi_x" + dpi_x=`echo "scale=5;$widthCurImg*72/$widthPDF" | bc` + dpi_y=`echo "scale=5;$heightCurImg*72/$heightPDF" | bc` + # compute the maximum allowed resolution difference that can be cause by: + # - the truncated PDF with/height in pt + # - the precision of dpi value + epsilon=`echo "scale=5;($widthCurImg*72/$widthPDF^2)+($heightCurImg*72/$heightPDF^2)+0.00002" | bc` # max inaccuracy due to truncation of PDF size in pt + diff1=`echo "($dpi_x - $dpi_y) < $epsilon " | bc` + diff2=`echo "($dpi_y - $dpi_x) < $epsilon " | bc` + [ $diff1 -eq 0 -o $diff2 -eq 0 ] && echo "Resolutions difference ($dpi_x/$dpi_y) higher than expected ($epsilon). Exiting..." >&2 && exit $EXIT_BAD_INPUT_FILE + dpi=`echo "scale=5;($dpi_x+$dpi_y)/2+0.5" | bc` # adding 0.5 is required for rounding + dpi=`echo "scale=0;$dpi/1" | bc` # round to the nearest integer # Identify if page image should be saved as ppm (color) or pgm (gray) ext="ppm" @@ -333,7 +342,7 @@ EOF [ $VERBOSITY -ge $LOG_DEBUG ] && echo "Output file: Checking compliance to PDF/A standard" java -jar "$JHOVE" -c "$JHOVE_CFG" -m PDF-hul "$FILE_OUTPUT_PDFA" > "$FILE_VALIDATION_LOG" grep -i "Status|Message" "$FILE_VALIDATION_LOG" # summary of the validation -[ $VERBOSITY -ge $LOG_DEBUG ] && cat "$FILE_VALIDATION_LOG" +[ $VERBOSITY -ge $LOG_DEBUG ] && echo "The full validation log is available here: \"$FILE_VALIDATION_LOG\"" # check the validation results pdf_valid=1 grep -i 'ErrorMessage' "$FILE_VALIDATION_LOG" >&2 && pdf_valid=0 @@ -353,5 +362,8 @@ if [ $KEEP_TMP -eq 0 ]; then fi +END=`date +%s` +[ $VERBOSITY -ge $LOG_DEBUG ] && echo "Script took $(($END-$START)) seconds" + [ $pdf_valid -ne 1 ] && exit $EXIT_INVALID_OUPUT_PDFA || exit 0