9

Is there an easy way to extract plain text from a pdf file?

On *nix systems I used to have a command ps2ascii that would do the job, but it seems that this command is not installed by default on my Mac.

What would be the easiest way to extract text from a pdf file or, alternatively, how to get ps2ascii on my system?

5

Adobe Reader has a "Save as Text…" option under the File menu. Easiest way.

3
  • Thanks! This is by far the most simple solution, it was able to cope with my huge file without any problems and produced a reasonably clean and usable text output. Sep 27 '10 at 11:14
  • 1
    Yes, it is the easiest, but not always the cleanest output. v7 output the best, but things go haywire before and after that. Part of my job is to supply plain text versions of college textbooks to disabled college students. I have a lot of experience with this, and that's why I recommend Ghostscript if looking for the free option. Sep 27 '10 at 13:43
  • 1
    @ghoppe thanks to this easy option but it is not available in mac version of adobe,how to make it in adobe via Adobe reader?
    – user11707
    Oct 7 '11 at 5:35
7

ps2ascii is a part of Ghostscript, which can be installed on Mac OS X (and it might already be by default from the factory).

4
  • 1
    Ghostscript is not included with OSX. Tho any of the methods listed below would happily install it. Sep 22 '10 at 14:02
  • I don't know if I'm pushing it too much but could you give me details on exactly what do I have to install and how? (Full disclaimer: I'm a fairly experienced user and I've already managed to install it, but on the beta stage of this site I would like to see how much detail and information are we to expect from people giving answers.) Sep 23 '10 at 16:17
  • I think a question like that would step out of the scope of SE since there are already very detailed docs available on their site. A more suitable question in this case, as far as I see it, would be to post a question related to installation after you've tried to install it, installation fails after repeated attempts, and searching the web yields little or no answers. Sep 23 '10 at 16:49
  • brew install ghostscript should allow you to install ps2ascii. Running that program printed all of the text from a PDF I wanted to convert. Thank you! Jan 8 at 21:00
2

If you don't mind using a GUI, you can select text from a PDF opened with Preview.app

2
  • Thanks, this seems to work for simple cases. But I've got a very large document (over 1000 pages) and it almost crashes the system just by trying to select-all! Sep 23 '10 at 16:09
  • An offbeat solution related to the above answer is that Acrobat 7 actually made surprisingly clean text extractions (but you are better off using a proper utility like ghostscript for something that large). Sep 23 '10 at 16:53
2

The following python script will output the text from a PDF document to a .txt file. (Note: There is no guarantee that the text is necessarily in 'logical' human readable order, due to the way that data is held in the PDF format.)

The script will create text files for any PDF files supplied as arguments to it on the command line (e.g. pdf2txt.py myPDF.pdf), or you can use in Automator's "Run Shell Script" action, setting the shell type to python and Pass input to "As arguments".

#!/usr/bin/python
# coding: utf-8

import os, sys
from Quartz import PDFDocument
from CoreFoundation import (NSURL, NSString)
NSUTF8StringEncoding = 4

def pdf2txt():
    for filename in sys.argv[1:]:   
        inputfile =filename.decode('utf-8')
        shortName = os.path.splitext(filename)[0]
        outputfile = shortName+" text.txt"
        pdfURL = NSURL.fileURLWithPath_(inputfile)
        pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL)
        if pdfDoc :
            pdfString = NSString.stringWithString_(pdfDoc.string())
            pdfString.writeToFile_atomically_encoding_error_(outputfile, True, NSUTF8StringEncoding, None)

if __name__ == "__main__":
   pdf2txt()
5
  • As a reminder: this needs pip install pyobjc.
    – Itachi
    Jun 25 '20 at 9:47
  • @Itachi No it doesn't: it will run out of the box on MacOS from Snow Leopard to Catalina.
    – benwiggy
    Jun 25 '20 at 10:07
  • I'm confused a little, so where does Quartz and CoreFoundation come from? Is it builtin package python for macOS?
    – Itachi
    Jun 25 '20 at 14:56
  • MacOS ships with python 2.7 and includes pyObjC v.2.5.1. You can update to a newer version of pyObjC, if you want increased functions and bug fixes -- or even install it in python3. But the factory OS has shipped with the same version for about 10 years or more.
    – benwiggy
    Jun 25 '20 at 15:27
  • Lesson learnt. Thanks!
    – Itachi
    Jun 25 '20 at 16:33
2

I’m not aware of any OS X native utility that does that, however, you can install most of the unix/linux commands with any of these three methods:

Homebew: Homebrew is the easiest and most flexible way to install the UNIX tools Apple didn't include with OS X.

Fink: The Fink project wants to bring the full world of Unix Open Source software to Darwin and Mac OS X.

Macports: The MacPorts Project is an open-source community initiative to design an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open-source software on the Mac OS X operating system.

Homebrew is the “new kid on the block” and promises to solve the “problems and limitations” that the other two have (whatever those problems may be). I suggest you take a look at all of them and use what you consider most flexible/simple for your needs.

There is, however, an app (Payware) that used to do that (I don’t know if it still does it). I’m talking about DEVONthink, and you can try a demo for a few days.

update: According to this post, you could install DevonThink (trial version) and extract the ‘pdftotext’ binary "which is free, of course" [sic] out of the bundle.

3
  • Thanks for the pointers, but which of these would actually contain ps2ascii? And which one should I prefer? Sep 23 '10 at 16:10
  • MacPorts handles Ghostscript 9 (the latest version). Sep 23 '10 at 16:51
  • OS X native utility is the Adobe Reader application, which has Save as Text.
    – ghoppe
    Sep 23 '10 at 20:21
1

Use Online document converters like Saaspose.PDF that can convert your PDF file to a TXT-based document. And because its a cloud API, there is no need to download or install anything.

1
  • But of course you have to upload your document to them. What will they do with it after?
    – benwiggy
    Jul 10 at 8:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .