less 如何显示 PDF?

less 如何显示 PDF?

我尝试了几个程序:pdftotext、pdf2txt.py,……它们都可以从 PDF 中提取文本,但效果更好的是 good ol' less:PDF 中的文本具有正确的布局。less 是如何做到这一点的?它是使用任何库,还是 PDF 处理是内置的?

我之所以问这个问题,是因为我想以编程方式使用此功能,而不必以外部程序的形式运行(我正在使用 python)。

我的系统是:

» less --version
less 458 (GNU regular expressions)
Copyright (C) 1984-2012 Mark Nudelman

less comes with NO WARRANTY, to the extent permitted by law.
For information about the terms of redistribution,
see the file named README in the less distribution.
Homepage: http://www.greenwoodsoftware.com/less

» uname -a
Linux polyphemus 3.13.0-53-generic #89-Ubuntu SMP Wed May 20 10:34:39 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

答案1

您的发行版可能正在使用流行的lesspipe.sh脚本。检查LESSOPEN环境变量。

此脚本有多种变体。我正在查看 Gentoo 版本。在其中,您将找到以下行:

*.ps|*.pdf) ps2ascii "$1" || pstotext "$1" || pdftotext "$1" ;;

这意味着它将按照显示的顺序尝试这些命令。$1是文件名。

另一个版本使用以下命令:

pdftohtml -stdout "$t" | parsehtml -

相关内容