I have a bunch of pdf files/forms that are all in a standardized format. They contain information such version history, author, who signed the document, and what documents it references. I need to extract that information to feed into a database containing information about the documents. I am pretty sure I could do this using a regular expression(s),but I am not entirely sure how to go about it. I would like to automate the process also, so that when new documents are created, they are added to the system, and also the system looks at all the documents on a web page and extracts the information.
1 Answer
It's possible to convert .pdf
document to a text file. After doing that it should be possible to extract a piece of text depending on how regular will it be using regular expressions. You did not specify what OS you are using but on *nix
it's possible to automate this using pdftotext
command line tool and a combination of grep/sed/cut/awk/perl
.
To download/upload documents from/to a web server you can use scp
if SSH
access is enabled or lftp
, extract metadata locally and push it to a web server. You will need to have a list of documents you already processed. Alternatively, you don't even need to download .pdf
from the web server because you can process it directly on the web server or mount a remote directory with sshfs
. You can trigger the operation of extracting metadata every time a new .pdf
is added using inotify
mechanism or some kind of web server callback or execute it regularly using a crontab
job.
I am still not sure whether you prefer *nix
or Windows
. If you prefer the latter, I can't help you because I don't use it.
exiftool
to extract.pdf
metadata and parse its output using regular expressions. For example, to findCreator
of a given.pdf
:exiftool doc.pdf | grep "^Creator" | perl -pe 's,^Creator.+: (.*)$,\1,'
. To find an author:exiftool doc.pdf | grep "^Author" | perl -pe 's,^Author.+: (.*)$,\1,'
.pdf
document to a text file. After doing that it should be possible to extract a piece of text depending on how regular will it be using regular expcessions. You did not specify what OS you are using but on*nix
it's possible to automate this usingpdftotext
command line tool and a combination ofgrep/sed/cut/awk/perl
man pdftotext
:pdftotext [options] [PDF-file [text-file]]
. It also says thatPdftotext reads the PDF file, PDF-file, and writes a text file, text-file. If text-file is not specified, pdftotext converts file.pdf to file.txt. If text-file is ´-', the text is sent to stdout.
. So, for example:pdftotext <doc>.pdf
will produce<doc>.txt
in the same directory.