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 -

相關內容