Extract Text from PostScript and PDF with ps2ascii
ps2ascii pulls the plain text out of a PostScript or PDF file when you need the words, not the layout. It writes to a new file or straight to standard output, either way leaning on Ghostscript underneath. Allow about ten minutes for a first conversion, plus longer 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:
$ ps2ascii /path/to/document.ps /path/to/document.txt
$ wc -c /path/to/document.txt
$ sed -n '1,12p' /path/to/document.txt
- No progress message means it worked. A successful command normally writes nothing.
- wc reports a non-zero size for a document containing visible text.
- sed shows readable lines. Blank lines and unusual spacing are normal: the tool reconstructs text from positioned glyphs, not an authoring source file.
- The PostScript input is left untouched. Only the output file is written.
Warning
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:
$ 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
The final mv changes the directory entry, so check the temporary result before running it. If conversion fails or the text is poor, stop before mv and leave the existing destination in place.
Recovery
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
- Do not pipe a PDF in. Writing
cat document.pdf | ps2asciiviolates the documented input rule and can produce an error rather than text. - Use a named path for audit trails. That is also easier to check 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'
Warning
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 suits a controlled PostScript pipeline, not feeding arbitrary PDF bytes.
Tip
For repeatable jobs, prefer named input and output files. It makes 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.
- Wrong-order letters or missing spaces? Some PostScript files use font encodings or kerning that
ps2asciihandles poorly. That is a limitation of this utility, not proof the source has no text. - For PostScript specifically, the manual suggests considering
pstotext. That suggestion does not extend to PDF in the installed manual. - Scanned document? It may contain only page images and need OCR;
ps2asciiis not an OCR tool. - Non-zero exit code? Retain the original and read the error message. Test with a copy when you are investigating a document supplied by somebody else.
Compare the extracted text with the rendered document before deciding the conversion failed.
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
- Tool identified:
ps2asciiand Ghostscript 10.02.1 are identified on the machine. - Text extracted: a named PostScript or PDF input produced inspectable text, or standard output was used deliberately.
- PDF passed as a path: never through standard input.
- Original untouched: the source document was not overwritten, and a temporary output was checked before replacement.
- Limits understood: font encoding, kerning, scans and image-only pages can limit extraction quality.
- Failures diagnosed: you can tell an input or permission failure apart from text that is merely inaccurate.