用于检查PDF的BASH脚本是ocr'd

时间:2011-11-03 15:20:32

标签: linux bash pdf xpdf

不知道从哪里开始

我有一台超过8000个PDf的Linux服务器,需要知道哪些PDF已经过了,哪些没有。

正在考虑某种脚本调用XPDF检查pdf,但老实说不确定这是否可能

提前感谢您提供任何帮助

2 个答案:

答案 0 :(得分:4)

确保安装了命令行工具pdffonts。 (这有两个版本:一个作为xpdf-utils的一部分发布,另一个作为poppler-utils的一部分。)

所有由扫描页面组成的PDF都不会使用任何字体(既不是嵌入式字体也不是非嵌入式字体)。

命令行

pdffonts /path/to/scanned.pdf

然后显示该文件的任何字体信息。

这可能已足以让您将文件分成两组。

如果您的PDF具有混合扫描页面和“普通”页面(或已修整页面),那么您将不得不扩展和完善上述简单方法。有关详细信息,请参阅man pdffontspdffonts --help

答案 1 :(得分:1)

pdffonts的问题在于,有时它不会返回任何内容,例如:

name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------

有时它会返回:

name                                 type              emb sub uni object ID
------------------------------------ ----------------- --- --- --- ---------
[none]                               Type 3            yes no  no     266  0
[none]                               Type 3            yes no  no       9  0
[none]                               Type 3            yes no  no     297  0
[none]                               Type 3            yes no  no     341  0
[none]                               Type 3            yes no  no     381  0
[none]                               Type 3            yes no  no     394  0
[none]                               Type 3            yes no  no     428  0
[none]                               Type 3            yes no  no     441  0
[none]                               Type 3            yes no  no     451  0
[none]                               Type 3            yes no  no     480  0
[none]                               Type 3            yes no  no     492  0
[none]                               Type 3            yes no  no     510  0
[none]                               Type 3            yes no  no     524  0
[none]                               Type 3            yes no  no     560  0
[none]                               Type 3            yes no  no     573  0
[none]                               Type 3            yes no  no     584  0
[none]                               Type 3            yes no  no     593  0
[none]                               Type 3            yes no  no     601  0
[none]                               Type 3            yes no  no     644  0

考虑到这一点,让我们编写一个小文本工具来获取pdf中的所有字体:

pdffonts my-doc.pdf | tail -n +3 | cut -d' ' -f1 | sort | uniq

如果您的pdf未进行OCR,则不会输出任何内容或[none]

如果您希望它运行得更快,请使用-l标记仅分析前5页:

pdffonts -l 5 my-doc.pdf | tail -n +3 | cut -d' ' -f1 | sort | uniq

现在将其包装在bash脚本中,例如is-pdf-ocred.sh

#!/bin/bash
MYFONTS=$(pdffonts -l 5 "$1" | tail -n +3 | cut -d' ' -f1 | sort | uniq)
if [ "$MYFONTS" = '' ] || [ "$MYFONTS" = '[none]' ]; then
    echo "NOT OCR'ed: $1"
else 
    echo "$1 is OCR'ed."
fi 

最后,我们希望能够搜索pdf。 find命令不知道.bashrc中的别名或函数,因此我们需要为它提供脚本的路径。 在您选择的目录中运行它,如下所示:

find . -type f -name "*.pdf" -exec /path/to/my/script/is-pdf-ocred.sh '{}' \;

我假设pdf文件以.pdf结尾,尽管这并不总是您可以做出的假设。 您可能希望将其管道为更少或将其输出到文本文件中:

find . -type f -name "*.pdf" -exec /path/to/my/script/is-pdf-ocred.sh '{}' \; | less
find . -type f -name "*.pdf" -exec /path/to/my/script/is-pdf-ocred.sh '{}' \; > pdfs.txt

我使用-l 5标志在10秒多一点内完成了大约200个pdf。