Extract text from PostScript and PDF with ps2ascii
You will turn a PostScript or PDF document into plain ASCII text, either on standard output or in a new text file. Allow about ten minutes for a first conversion, plus time to inspect the result if the document uses unusual fonts or layout. The examples use the installed Ghostscript 10.02.1 package and do not need sudo.
The route
Jump straight to the step you need, or tick off Done means at the end.
1. Check the installed tool
ps2ascii is a small Ghostscript wrapper. This guide describes the behaviour of the local Ghostscript 10.02.1 installation, packaged as ghostscript 10.02.1~dfsg1-0ubuntu7.9. Check your own executable before putting the command into a script:
$ command -v ps2ascii
/usr/bin/ps2ascii
$ gs --version
10.02.1
Your path can differ. The version matters because this utility delegates the conversion to Ghostscript, and diagnostics or text extraction details can change between releases.
Checkpoint
You have a readable input file and command -v ps2ascii finds the executable.
2. Extract PostScript to a new file
Pass the input first and the output second. The output file is plain text, while the PostScript input is left untouched:
$ ps2ascii /path/to/document.ps /path/to/document.txt
$ wc -c /path/to/document.txt
$ sed -n '1,12p' /path/to/document.txt
A successful command normally writes no progress message. wc should report a non-zero size for a document containing visible text, and sed should show readable lines. Blank lines and unusual spacing are normal because the tool is reconstructing text from positioned glyphs, not reading an authoring source file.
Do not use a destination that contains the only copy of useful text. If the destination already exists, the wrapper asks Ghostscript to write it, so protect it first or choose a new name.
3. Use standard output for a pipeline
Omit the output argument when another command should receive the extracted text:
$ ps2ascii /path/to/document.ps | sed -n '1,40p'
$ ps2ascii /path/to/document.ps | grep -i 'invoice' || true
The second command keeps the shell from treating a missing match as an unexpected script failure. Remove || true when a missing match should make your own script fail.
For a file you can replace only after conversion succeeds, use a temporary file in the same directory. The final mv changes the directory entry, so check the temporary result first:
$ ps2ascii /path/to/document.ps /path/to/document.txt.new
$ test -s /path/to/document.txt.new
$ sed -n '1,12p' /path/to/document.txt.new
$ mv /path/to/document.txt.new /path/to/document.txt
If conversion fails or the text is poor, stop before mv and leave the existing destination in place. To recover an accidental replacement, restore your backup or regenerate the text from the original document. The command does not provide a version history.
4. Extract text from a PDF
PDF input must be named explicitly. It cannot come from standard input, even though PostScript can:
$ ps2ascii /path/to/document.pdf /path/to/document.txt
$ test -s /path/to/document.txt && echo 'text file was created'
text file was created
For a PDF, do not write cat document.pdf | ps2ascii. That violates the documented input rule and can produce an error rather than text. A named path is also easier to audit when a script handles files from an upload or archive.
The same output choice applies to PDF:
$ ps2ascii /path/to/document.pdf | sed -n '1,20p'
Reading an untrusted document invokes Ghostscript's interpreter. The installed wrapper includes Ghostscript's -dSAFER option, but keep normal file permissions and isolation practices: do not make the input writable by unrelated users, and do not run this conversion as root just to read a document.
5. Use standard input only for PostScript
With no arguments, ps2ascii reads PostScript from standard input and writes the extracted text to standard output:
$ printf '%s\n' '%!PS' '/Helvetica findfont 12 scalefont setfont' '72 720 moveto' '(stdin test) show' 'showpage' | ps2ascii
stdin test
The exact indentation and line ending can vary. The useful check is that stdin test appears. This mode is suitable for a controlled PostScript pipeline, not for feeding arbitrary PDF bytes.
For repeatable jobs, prefer named input and output files. It makes it clear which file was read, avoids accidentally consuming a shell pipeline, and gives you a place to inspect the result before another command uses it.
6. Diagnose an empty or inaccurate result
First establish whether the input is readable and whether it is the type you think it is:
$ test -r /path/to/document.ps && echo readable
readable
$ file /path/to/document.ps
/path/to/document.ps: PostScript document text
For a PDF, file should identify a PDF document. A missing file, a permission error or a damaged document is a problem to fix before changing Ghostscript options. Ordinary extraction does not require elevated privileges.
Some PostScript files use font encodings or kerning that ps2ascii handles poorly. You may see letters in the wrong order, missing spaces, or output that is nearly empty even though a viewer displays the page. This is a limitation of this utility, not proof that the source has no text. For PostScript specifically, the manual suggests considering pstotext. That suggestion does not extend to PDF in the installed manual.
Compare the extracted text with the rendered document. If the document is a scan, it may contain only page images and need OCR; ps2ascii is not an OCR tool. If the command exits non-zero, retain the original and read the error message. Test with a copy when you are investigating a document supplied by somebody else.
7. Automate only after checking the output
A small shell check can distinguish a failed conversion from an empty result, but a non-empty file is not a guarantee that every word is correct:
$ output=/path/to/document.txt.new
$ ps2ascii /path/to/document.pdf "$output" && test -s "$output"
$ status=$?
$ printf 'conversion status: %s\n' "$status"
conversion status: 0
$ [ "$status" -eq 0 ] && mv -- "$output" /path/to/document.txt
Keep the temporary path separate from the source and quote paths that come from variables. If the status is not zero, inspect the error and do not promote the temporary file. A failed conversion may leave a partial output, so remove that temporary file only after you have confirmed it is not needed for diagnosis.
Done means
ps2asciiand Ghostscript 10.02.1 are identified on the machine.- A named PostScript or PDF input produced inspectable text, or standard output was used deliberately.
- PDF input was passed as a file path rather than through standard input.
- The original document was not overwritten, and a temporary output was checked before replacement.
- You know that font encoding, kerning, scans and image-only pages can limit extraction quality.
- You can distinguish an input or permission failure from text that is merely inaccurate.