25

I am using OSX and would like to be able to convert pdf files to text.

I would like a free application to do this, as I am sure there must be some.

2
  • 3
    Are you looking to extract text from PDFs which already contain text? (i.e., you could copy and paste pieces out of them) Or are you looking to recognize text that is in image content? Nov 7, 2014 at 20:31
  • Does free-ocr.com help?
    – Tim
    Jan 8, 2015 at 15:53

8 Answers 8

23

Here are the steps I used to install and use xpdf via Homebrew.

  1. Install Homebrew's dependencies:
xcode-select --install
  1. Install Homebrew from their website:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  1. Do what it tells you to complete the Homebrew installation.

  2. Verify that Homebrew is happy and functioning to spec.

brew doctor
  1. Next install xpdf and its dependencies:
brew install Caskroom/cask/xquartz
brew install xpdf
  1. Finally, use pdftotext, a package that comes with the xpdf suite:
pdftotext Some_Document.pdf Some_Document.txt

The first file name is an existing PDF; the second is the destination. The results were much better than with an (admittedly old) version of Adobe Acrobat. Edit: New (2019) versions of Adobe Acrobat have shown similarly poor results.

3
  • 7
    I think the xquartz is not needed for the console tools in xpdf. Furthermore, there is the poppler fork of xpdf which seems far more actively maintained: github.com/scraperwiki/scraperwiki-python/issues/… Sep 18, 2018 at 12:46
  • 1
    Worked great. Note that installing xpdf installed a bunch of dependencies on my system.
    – rinogo
    May 29, 2021 at 18:28
  • Tried and got a .txt with gibberish and U+000c is not a basic ASCIII character warning on one line when I ran pdftotext text.pdf text.txt . But perhaps pdf quality is just not good enough for conversion.
    – rhand
    Sep 16, 2023 at 0:17
10

Multiple methods.

  1. Use Google documents (you will need a Google account)

  2. Use Automator (some work required)

You can use Automator to create a workflow that can extract text from PDFs and save it as a text or RTF document.

extract text from pdf

5

xpdf which I installed with ports:

port install xpdf

contains:

xpdf-pdftotext

It does what you want for any PDF file which is coming from a text file (and not from an image):

xpdf-pdftotext PDF_file text_file
2
  • 1
    The command, at least as installed by HomeBrew, is just “pdftotext”. Feb 23, 2016 at 4:12
  • @FlashSheridan Can I encourage you to post an answer covering "pdftotext" and how to install it via homebrew? Comments may get deleted anytime (and also don't show up in searches).
    – nohillside
    Feb 24, 2016 at 13:39
4

Current version of Adobe Reader (11.0.09) has a "Save as Other" item in its File Menu.

One of the options is Text.

The App is free, and does a decent job outputting text files. All images in the new document will be lost with the .txt format.

4

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". Then you can use it as a Quick Action or DropApp.

#!/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()

NB: The above script will 'only' work on any Mac running Leopard to Monterey 12.2...! Python 2 was removed from macOS in Monterey 12.3. If you install python 3 and the pyobjc library, then the following python 3 script will work:

#!/usr/bin/env python3
    
import os, sys
from Quartz import PDFDocument
from CoreFoundation import (NSURL, NSString)
NSUTF8StringEncoding = 4
        
def pdf2txt():
  for filename in sys.argv[1:]: 
    shortName = os.path.splitext(filename)[0]
    outputfile = shortName+" text.txt"
    pdfURL = NSURL.fileURLWithPath_(filename)
    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()
7
  • This works out of the box (ie - didnt have to install any python libraries or anything!) - Is just a raw dump of text, but sometimes that's what you need - note that the file saves in the current directory (if running from the command line) and doesnt come out to stdout
    – Brad Parks
    Feb 7, 2020 at 0:16
  • @BradParks It should save to the same folder as the input file: it takes the filepath from the command arguments. How are you supplying it?
    – benwiggy
    Feb 7, 2020 at 12:14
  • ahh... i was in the same folder, so I never noticed that - my only real intent was to say that it doesnt come to stdout, and that it works! thanks!
    – Brad Parks
    Feb 7, 2020 at 14:11
  • 1
    Replace the two lines after the if pdfDoc statement with print(pdfDoc.string()).
    – benwiggy
    Feb 7, 2020 at 14:16
  • 1
    @Jérémie The built-in python (using the pyObjC library for accessing all the MacOS APIs) has been very useful since OS X Tiger, at least! Here's my complete suite of PDF utilities: github.com/benwiggy/PDFsuite
    – benwiggy
    Feb 15, 2020 at 11:31
3

Using built-in Shortcuts app

  1. Add "Get Text from PDF" action
  2. Add "Append Text to <LOCATION>" action

Shortcuts - Extract text from PDF

2

Extract selectable text (not OCR) in PDF on MacOS

brew install poppler

pdftotext x.pdf x.txt

Related: linux - Is the pdftotext command line tool for mac? - Super User

1

I would think you should be able to copy and paste the text into another document. To select all the text

Open the PDF in "Preview", and

  • choose "Edit | Select All"
  • choose "Edit | Copy"

Go to another app, say "Text Edit"

  • choose "Edit | Paste"

Note that if you try and do this, and there's no text pasted, just a bunch of blank lines, try printing your PDF to a new PDF first, e.g.

  • In Preview, choose "File | Print"
  • In the bottom right, choose "PDF | Save as PDF"
  • This exports a new PDF.

Now try the above process with this new PDF. Worked for me!

PS: If you have Microsoft Word, you may be able to open your PDF in word

4
  • 1
    While this should work when the PDF document actually contains text content, it does not maintain formatting and in some cases produces garbage text along with what would be considered wanted text. Note that this may also be the same with other methods, but I felt it important to point out. Oct 24, 2017 at 16:53
  • without a doubt - not perfect for sure, and I totally agree with everything you've said. I only add it here as an option I've noticed before, that worked for me without installing anything at all ;-)
    – Brad Parks
    Oct 24, 2017 at 17:25
  • I also agree that it works for most cases, but page numbers, footer details that aren't related to the document and worse yet: INFINITE SCROLLING on large documents make this an unsatisfactory solution. If it was less than 5 pages I'd consider it, but given most tables I need are from PDFs off research sites that refuse to spit out excel or SQL, this option isn't even functional.
    – Tmanok
    Feb 27, 2019 at 2:56
  • Agreed! not a viable option for your use case
    – Brad Parks
    Feb 27, 2019 at 11:18

You must log in to answer this question.

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