less 如何显示 PDF?

blu*_*ast 54 pdf less

我尝试了几个程序:pdftotext、pdf2txt.py、...所有这些程序都可以从 PDF 中提取文本,但是做得更好的一个是好的 ol' less:PDF 中的文本具有正确的布局。少怎么做这个?它使用任何库,还是内置的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
Run Code Online (Sandbox Code Playgroud)

Dan*_*l B 66

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

该脚本存在多种变体。我在看Gentoo版本。在其中,您将找到以下行:

*.ps|*.pdf) ps2ascii "$1" || pstotext "$1" || pdftotext "$1" ;;
Run Code Online (Sandbox Code Playgroud)

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

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

pdftohtml -stdout "$t" | parsehtml -
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,事实证明它正在使用`pdftotext -layout $1 -` (17认同)