using bash to read pdf content

bash

Solution

Poppler Library provides a set of command line tools to extract text and metadata from PDF files.

To extract metadata you could use `pdfinfo`

For example

:~> pdfinfo ProAdminGuide.pdf  2>/dev/null | \
  grep Title: | sed 's/Title:[ ]*//'

Outputs

Professional Administrator’s Guide

Sometimes the PDF file does not contain complete metadata. In this case you might try your luck to extract the title from the text of the title page. To extract the text of the title page you could use `pdftotext`

:~> pdftotext ProAdminGuide.pdf - | head -3

Outputs

A division of


Professional Administrator’s Guide, published by

In any case it is worth first checking that you can extract the titles from the pdf file before renaming them automatically

for book in *.pdf ; do 
   title=$(pdfinfo "$book" 2>/dev/null | grep Title: | sed 's/Title:[ ]*//')
   [[ "$title" ]] || continue
   mv "$book" "${title}.pdf"  
done

Edit: added a nice idiom suggested by Charles Duffy in the comments as a precaution

Problem

I have several ebooks which are not always named after the title of the book. Would it be possible to use bash commands to read the pdf's first page (and do a trivial grep etc) and rename the file accordingly ? thanks -a

Original source