Scientific articles are typically locked away in PDF format, a format designed primarily for printing but not so great for searching or indexing. The new pdftools package allows for extracting text and metadata from pdf files in R. From the extracted plain-text one could find articles discussing a particular drug or species name, without having to rely on publishers providing metadata, or pay-walled search engines.
The pdftools slightly overlaps with the Rpoppler package by Kurt Hornik. The main motivation behind developing pdftools was that Rpoppler depends on glib, which does not work well on Mac and Windows. The pdftools package uses the poppler c++ interface together with Rcpp, which results in a lighter and more portable implementation.
On Windows and Mac the binary packages can be installed directly from CRAN:
Installation on Linux requires the poppler development library. For Ubuntu 18.04 (Bionic) and Ubuntu 20.04 (Focal) we provide backports of poppler version 22.02 to support the latest functionality:
sudo add-apt-repository -y ppa:cran/poppler sudo apt-get update sudo apt-get install -y libpoppler-cpp-dev
On other versions of Debian or Ubuntu simply use::
sudo apt-get install libpoppler-cpp-dev
If you want to install the package from source on MacOS you need brew:
brew install poppler
sudo yum install poppler-cpp-devel
?pdftools manual page shows a brief overview of the main utilities. The most important function is
pdf_text which returns a character vector of length equal to the number of pages in the pdf. Each string in the vector contains a plain text version of the text on that page.
In addition, the package has some utilities to extract other data from the PDF file. The
pdf_toc function shows the table of contents, i.e. the section headers which pdf readers usually display in a menu on the left. It looks pretty in JSON:
Other functions provide information about fonts, attachments and metadata such as the author, creation date or tags.
Another feature of pdftools is rendering of PDF files to bitmap arrays (images). The poppler library provides all functionality to implement a complete PDF reader, including graphical display of the content. In R we can use
pdf_render_page to render a page of the PDF into a bitmap, which can be stored as e.g. png or jpeg.
Data scientists are often interested in data from tables. Unfortunately the pdf format is pretty dumb and does not have notion of a table (unlike for example HTML). Tabular data in a pdf file is nothing more than strategically positioned lines and text, which makes it difficult to extract the raw data with
tabulizer package is dedicated to extracting tables from PDF, and includes interactive tools for selecting tables. However,
tabulizer depends on
rJava and therefore requires additional setup steps or may be impossible to use on systems where Java cannot be installed.
It is possible to use
pdftools with some creativity to parse tables from PDF documents, which does not require Java to be installed.
If you want to extract text from scanned text present in a pdf, you’ll need to use OCR (optical character recognition). Please refer to the rOpenSci
tesseract package that provides bindings to the Tesseract OCR engine. In particular read the section of its vignette about reading from PDF files using