miyapiのひとりごと

blogこと始め。コンピュータと映画、そしてお酒の好きなmiyapiが気ままに書きつづる日記。。。

Linux: PDFをテキストに変換

2010-10-27 00:09:23 | Weblog
PDFのマニュアルの本文をテキストファイルに抽出したいことが、
たまにあるが、以下のコマンドでそれができる。

ps2ascii infile.pdf outfile.txt

ps2ascii は Ghostscript tools に入っている。

最初はいろいろ調べてたけど、いまいち良いツールが見当たらないなぁ、
と悩んでいたところに、たまたま man を見て発見。

意外に身近にあるものだなぁ。。というより、たまには man も
見てみるものだなぁ。


$man ps2ascii
---------------------------
PS2ASCII(1) Ghostscript Tools PS2ASCII(1)

NAME
ps2ascii - Ghostscript translator from PostScript or PDF to ASCII

SYNOPSIS
ps2ascii [ input.ps [ output.txt ] ]
ps2ascii input.pdf [ output.txt ]

DESCRIPTION
ps2ascii uses gs(1) to extract ASCII text from PostScript(tm) or Adobe Portable Document Format (PDF) files. If no files are specified on the
command line, gs reads from standard input; but PDF input must come
from an explicitly-named file, not standard input. If no output file
is specified, the ASCII text is written to standard output.

ps2ascii doesn’t look at font encoding, and isn’t very good at dealing
with kerning, so for PostScript (but not currently PDF), you might con-
sider pstotext (see below).