<p>There is no problem in science quite as frustrating as other peoples' data. Whether it's malformed spreadsheets, disorganized documents, proprietary file formats, data without metadata, or any other data scenario created by someone else, <a href="https://twitter.com/hashtag/otherpeoplesdata?src=hash">scientists have taken to Twitter to complain about it</a>. As a political scientist who regularly encounters so-called &quot;open data&quot; in PDFs, this problem is particularly irritating. PDFs may have &quot;portable&quot; in their name, making them display consistently on various platforms, but that portability means any information contained in a PDF is irritatingly difficult to extract computationally. Encountering &quot;open data&quot; PDFs therefore makes me shout things like this repeatedly:</p>
<blockquote class="twitter-tweet"><p dir="ltr" lang="en">HEY US GOVERNMENT! Tables in PDF documents aren't &quot;Open Data.&quot; Please provide machine-readable formats or it doesn't count.</p>— Anthony A. Boyles (@AABoyles) <a href="https://twitter.com/AABoyles/status/776428077123506176">September 15, 2016</a></blockquote>
<p>What can we do about such data other than extract it by hand? One answer is rely on <a href="https://github.com/ropensci/tabulizer"><code>tabulizer</code></a> a package I submitted to rOpenSci that reduces some and often all of the hassle of extracting tabular data locked inside PDFs.</p>
<h1>What is <code>tabulizer</code>?</h1>
<p><code>tabulizer</code> provides R bindings to <a href="https://github.com/tabulapdf/tabula-java">the tabula-java library</a>, the open-source java library that powers <a href="http://tabula.technology/">Tabula</a> (source code on <a href="https://github.com/tabulapdf/tabula">GitHub</a>). What this means is that <code>tabulizer</code> relies directly on the underlying java classes that power Tabula without the need for system calls or the need to explicitly install Tabula on your system. (A potential downside is the need to handle intricacies of rJava - R's interface to Java, which I discuss in depth below.)</p>
<p>Tabula is an extremely powerful tool for extracting tabular data locked in PDFs. It's an incredibly valuable tool because the PDF file specification does not have a &quot;table&quot; representation. Instead, PDFs simply represent tables through the fixed positioning of text into rows and columns. Thus, unlike HTML, Word (.docx), or Open Document (.odt) file formats, there is no easy programmatic way to identify a table in a PDF. Tabula thus implements novel algorithms for identifying rows and columns of data and extracting them. <code>tabulizer</code> just provides a thin R layer on top of this power Java code.</p>
<p>Unfortunately, this means that <code>tabulizer</code> is not a universal solution to data trapped in PDFs. In particular, it can only identify and extract tables that are represented as text in a PDF:</p>
<blockquote class="twitter-tweet"><p dir="ltr" lang="en">Oh no no no no no! Just received <a href="https://twitter.com/hashtag/otherpeoplesdata?src=hash">#otherpeoplesdata</a> as a 276 page set of printed tables scanned in to a PDF</p>— Dr Elizabeth Sargent (@esargent184) <a href="https://twitter.com/esargent184/status/510056437033091074">September 11, 2014</a></blockquote>
<p>If a PDF is a scan of a document or the table is actually an image embedded in the PDF, tabula - and thus <code>tabulizer</code> - are useless. In those cases, users might want to check out the OCR functionality of <a href="https://github.com/ropensci/tesseract">tesseract</a>, which Jeroen Ooms developed for rOpenSci and <a href="https://ropensci.org/blog/blog/2016/11/16/tesseract">discussed previously on this blog</a>.</p>
<p>But it does mean that a substantial amount of difficult-to-parse tabular information in PDFs is now readily and quickly accessible via just one <code>tabulizer</code> function: <code>extract_tables()</code>.</p>
<h2>Installation</h2>
<p><code>tabulizer</code> is not yet on CRAN. (It's CRAN-ready but due to some underlying developments that are ongoing in the tabula-java library, I'm waiting for a formal release.) In the meantime, it's possible to install <code>tabulizer</code> directly from GitHub.</p>
<p>Before doing that, I would encourage users to make sure they have rJava installed (from CRAN) and that it works correctly on their platform. A lot of users report difficulties installing <code>tabulizer</code> that ultimately boil down to being Java and rJava issues that need to be resolved first. The <a href="https://github.com/ropensci/tabulizer#installation">package README</a> provides a number of details on installation, which requires a strictly ordered set of steps:</p>
<ol><li>Install the Java Development Kit, if you don't already have it on your system. (Note that the JDK is different from the Java Runtime Environment (JRE) that you almost certainly already have.) Details of how to do this vary a lot between platforms, so see the <a href="https://github.com/ropensci/tabulizer#installation">README</a> for details.</li>
<li>Install rJava using <code>install.packages(&quot;rJava&quot;)</code> and resolve any issues surrounding the <code>JAVA_HOME</code> environment variable that may need to be set before and/or after installing rJava. Again, see the <a href="https://github.com/ropensci/tabulizer#installation">README</a> or <a href="http://stackoverflow.com/search?q=%5Br%5D+rjava+install">various question/answer pairs on StackOverflow</a> for platform-specific instructions.</li>
<li>Install <code>tabulizer</code> and <code>tabulizerjars</code> (the package containing the tabula java library) using your favorite GitHub package installer:</li>
</ol><div><pre><code><span></span><span>library</span><span>(</span><span>&quot;ghit&quot;</span><span>)</span>
ghit<span>::</span>install_github<span>(</span><span>c</span><span>(</span><span>&quot;ropensci/tabulizerjars&quot;</span><span>,</span><span>&quot;ropensci/tabulizer&quot;</span><span>))</span>
</code></pre></div>
<p>This should work. If not, set <code>verbose = TRUE</code> in <code>ghit::install_github()</code> to identify the source of any issues. Some common problems are the dependency on the <strong>png</strong> package, which might need to be installed first. On Windows (depending on your version of R and how it was installed) may require setting <code>INSTALL_opts = &quot;--no-multiarch&quot;</code> in <code>ghit::install_github()</code>.</p>
<p>If none of these steps work, scroll through <a href="https://github.com/ropensci/tabulizer/issues?utf8=%E2%9C%93&q=is%3Aissue">the GitHub issues page</a> for anyone experiencing a similar problem and, if not resolved in any of those discussions, feel free to open an issue on GitHub describing your problem including the fully verbose output of <code>install_github()</code> and your <code>sessionInfo()</code>.</p>
<h1>Unlocking elections data with <code>tabulizer</code></h1>
<p>Elections data are the bread and butter of a lot of quantitative political science research. Many researchers in my field need to know how many citizens voted and for whom in order to make sense of campaigns, election integrity, partisanship, and so forth. Yet a substantial amount of election-related data is locked in government-produced PDFs. Worse, national, state, and local governments have little to no standardization in the formatting of elections data, meaning even if one could figure out a computational strategy for extracting one kind of data about elections in one year from one state, that computational strategy would likely be useless in the same state in another year or in any other state. Elections provide a fantastic and highly visible example of &quot;open&quot; government data that's not really open or usable at all.</p>
<p>As a simple example, <a href="http://elections.cdn.sos.ca.gov/sov/2016-general/sov/04-historical-voter-reg-participation.pdf">this PDF from the California Secretary of State's office</a> contains historical voter registration and turnout data in a well-formatted table. Why this is a PDF nobody knows. But extracting the tables using <code>tabulizer</code>'s <code>extract_tables()</code> function is a breeze with no need to even download the file:</p>
<div><pre><code><span></span><span>library</span><span>(</span><span>&quot;tabulizer&quot;</span><span>)</span>
sos_url <span>&lt;-</span> <span>&quot;http://elections.cdn.sos.ca.gov/sov/2016-general/sov/04-historical-voter-reg-participation.pdf&quot;</span>
tab1 <span>&lt;-</span> extract_tables<span>(</span>sos_url<span>)</span>
str<span>(</span>tab1<span>)</span>
</code></pre></div><div><pre><code><span></span>## List of 2
##  $ : chr [1:58, 1:9] &quot;&quot; &quot;Election Date&quot; &quot;Nov. 8, 1910&quot; &quot;Nov. 5, 1912 P&quot; ...
##  $ : chr [1:6, 1:9] &quot;&quot; &quot;Election Date&quot; &quot;Nov. 2, 2010&quot; &quot;Nov. 6, 2012 P&quot; ...
</code></pre></div>
<p>The (default) result is a list of two matrices, each containing the tables from pages 1 and 2 of the document, respectively. A couple of quick cleanups and this becomes a well-formatted data frame:</p>
<div><pre><code><span></span><span># save header</span>
h <span>&lt;-</span> tab1<span>[[</span><span>1</span><span>]][</span><span>2</span><span>,]</span>
<span># remove headers in first table</span>
tab1<span>[[</span><span>1</span><span>]]</span> <span>&lt;-</span> tab1<span>[[</span><span>1</span><span>]][</span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>),]</span>
<span># remove duplicated header in second table</span>
tab1<span>[[</span><span>2</span><span>]]</span> <span>&lt;-</span> tab1<span>[[</span><span>2</span><span>]][</span><span>-</span><span>c</span><span>(</span><span>1</span><span>,</span><span>2</span><span>),]</span>
<span># merge into one table</span>
tab1df <span>&lt;-</span> setNames<span>(</span><span>as.data.frame</span><span>(</span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span> tab1<span>),</span> stringsAsFactors <span>=</span> <span>FALSE</span><span>),</span> h<span>)</span>
str<span>(</span>tab1df<span>)</span>
</code></pre></div><div><pre><code><span></span>## 'data.frame':    60 obs. of  9 variables:
##  $ Election Date: chr  &quot;Nov. 8, 1910&quot; &quot;Nov. 5, 1912 P&quot; &quot;Nov. 3, 1914&quot; &quot;Nov. 7, 1916 P&quot; ...
##  $ Eligible     : chr  &quot;725,000&quot; &quot;1,569,000&quot; &quot;1,726,000&quot; &quot;1,806,000&quot; ...
##  $ Democratic   : chr  &quot;*&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##  $ Republican   : chr  &quot;*&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##  $ Other        : chr  &quot;*&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##  $ Total        : chr  &quot;*&quot; &quot;987,368&quot; &quot;1,219,345&quot; &quot;1,314,446&quot; ...
##  $ Total Votes  : chr  &quot;393,893&quot; &quot;707,776&quot; &quot;961,868&quot; &quot;1,045,858&quot; ...
##  $ Registered   : chr  &quot;*&quot; &quot;71.68%&quot; &quot;78.88%&quot; &quot;79.57%&quot; ...
##  $ Eligible     : chr  &quot;54.33%&quot; &quot;45.11%&quot; &quot;55.73%&quot; &quot;57.91%&quot; ...
</code></pre></div>
<p>Which is very easy to then quickly turn into a time-series visualization of registration rates:</p>
<div><pre><code><span></span><span>library</span><span>(</span><span>&quot;ggplot2&quot;</span><span>)</span>
years <span>&lt;-</span> <span>regexpr</span><span>(</span><span>&quot;[[:digit:]]{4}&quot;</span><span>,</span>tab1df<span>[[</span><span>&quot;Election Date&quot;</span><span>]])</span>
tab1df<span>$</span>Year <span>&lt;-</span> <span>as.numeric</span><span>(</span><span>regmatches</span><span>(</span>tab1df<span>[[</span><span>&quot;Election Date&quot;</span><span>]],</span> years<span>))</span>
tab1df<span>$</span>RegPerc <span>&lt;-</span> <span>as.numeric</span><span>(</span><span>gsub</span><span>(</span><span>&quot;%&quot;</span><span>,</span> <span>&quot;&quot;</span><span>,</span> tab1df<span>$</span>Registered<span>))</span>
</code></pre></div><div><pre><code><span></span>## Warning: NAs introduced by coercion
</code></pre></div><div><pre><code><span></span>ggplot<span>(</span>tab1df<span>,</span> aes<span>(</span>x <span>=</span> Year<span>,</span> y <span>=</span> RegPerc<span>))</span> <span>+</span>
  geom_line<span>()</span> <span>+</span> ylim<span>(</span><span>c</span><span>(</span><span>0</span><span>,</span><span>100</span><span>))</span> <span>+</span> ylab<span>(</span><span>&quot;% Registered&quot;</span><span>)</span> <span>+</span>
  ggtitle<span>(</span><span>&quot;California Voter Registration, by Year&quot;</span><span>)</span>
</code></pre></div><div><pre><code><span></span>## Warning: Removed 1 rows containing missing values (geom_path).
</code></pre></div>
<p></p>
<h2>Optional arguments</h2>
<p>The <code>extract_tables()</code> has several arguments that control extraction and the return value of the function. They performed reasonably well here, but it's worth seeing a few of the other options. The <code>method</code> argument controls the return value. For extremely well-formatted tables, setting this to &quot;data.frame&quot; can be convenient, though it doesn't work perfectly here:</p>
<div><pre><code><span></span>str<span>(</span>tab2 <span>&lt;-</span> extract_tables<span>(</span>sos_url<span>,</span> method <span>=</span> <span>&quot;data.frame&quot;</span><span>))</span>
</code></pre></div><div><pre><code><span></span>## List of 2
##  $ :'data.frame':    57 obs. of  9 variables:
##   ..$ X        : chr [1:57] &quot;Election Date&quot; &quot;Nov. 8, 1910&quot; &quot;Nov. 5, 1912 P&quot; &quot;Nov. 3, 1914&quot; ...
##   ..$ X.1      : chr [1:57] &quot;Eligible&quot; &quot;725,000&quot; &quot;1,569,000&quot; &quot;1,726,000&quot; ...
##   ..$ X.2      : chr [1:57] &quot;Democratic&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##   ..$ X.3      : chr [1:57] &quot;Republican&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##   ..$ X.4      : chr [1:57] &quot;Other&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##   ..$ X.5      : chr [1:57] &quot;Total&quot; &quot;*&quot; &quot;987,368&quot; &quot;1,219,345&quot; ...
##   ..$ X.6      : chr [1:57] &quot;Total Votes&quot; &quot;393,893&quot; &quot;707,776&quot; &quot;961,868&quot; ...
##   ..$ Turnout  : chr [1:57] &quot;Registered&quot; &quot;*&quot; &quot;71.68%&quot; &quot;78.88%&quot; ...
##   ..$ Turnout.1: chr [1:57] &quot;Eligible&quot; &quot;54.33%&quot; &quot;45.11%&quot; &quot;55.73%&quot; ...
##  $ :'data.frame':    5 obs. of  9 variables:
##   ..$ X        : chr [1:5] &quot;Election Date&quot; &quot;Nov. 2, 2010&quot; &quot;Nov. 6, 2012 P&quot; &quot;Nov. 4, 2014&quot; ...
##   ..$ X.1      : chr [1:5] &quot;Eligible&quot; &quot;23,551,699&quot; &quot;23,802,577&quot; &quot;24,288,145&quot; ...
##   ..$ X.2      : chr [1:5] &quot;Democratic&quot; &quot;7,620,240&quot; &quot;7,966,422&quot; &quot;7,708,683&quot; ...
##   ..$ X.3      : chr [1:5] &quot;Republican&quot; &quot;5,361,875&quot; &quot;5,356,608&quot; &quot;5,005,422&quot; ...
##   ..$ X.4      : chr [1:5] &quot;Other&quot; &quot;4,303,768&quot; &quot;4,922,940&quot; &quot;5,089,718&quot; ...
##   ..$ X.5      : chr [1:5] &quot;Total&quot; &quot;17,285,883&quot; &quot;18,245,970&quot; &quot;17,803,823&quot; ...
##   ..$ X.6      : chr [1:5] &quot;Total Votes&quot; &quot;10,300,392&quot; &quot;13,202,158&quot; &quot;7,513,972&quot; ...
##   ..$ Turnout  : chr [1:5] &quot;Registered&quot; &quot;59.59%&quot; &quot;72.36%&quot; &quot;42.20%&quot; ...
##   ..$ Turnout.1: chr [1:5] &quot;Eligible&quot; &quot;43.74%&quot; &quot;55.47%&quot; &quot;30.94%&quot; ...
</code></pre></div>
<p>Setting <code>method = &quot;character&quot;</code> returns a list of character vectors with white space reflecting the positioning of text within the PDF's tabular representation:</p>
<div><pre><code><span></span>str<span>(</span>tab3 <span>&lt;-</span> extract_tables<span>(</span>sos_url<span>,</span> method <span>=</span> <span>&quot;character&quot;</span><span>))</span>
</code></pre></div><div><pre><code><span></span>## List of 2
##  $ : chr &quot;\t\t\t\t\t\t\tTurnout\tTurnout\nElection Date\tEligible\tDemocratic\tRepublican\tOther\tTotal\tTotal Votes\tRegistered\tEligibl&quot;| __truncated__
##  $ : chr &quot;\t\t\t\t\t\t\tTurnout\tTurnout\nElection Date\tEligible\tDemocratic\tRepublican\tOther\tTotal\tTotal Votes\tRegistered\tEligibl&quot;| __truncated__
</code></pre></div>
<p>This argument can also be set to <code>&quot;csv&quot;</code>, <code>&quot;tsv&quot;</code>, or <code>&quot;json&quot;</code> to use a java-level utility to write the table to files in the working directory but this tends to be inconvenient. (For advanced users, <code>method = &quot;asis&quot;</code> returns an rJava object reference for those who want to manipulate the Java representation of the table directly.)</p>
<p>The other most important option to be aware of is <code>guess</code>, which indicates whether a column-finding algorithm should be used to identify column breaks. This should almost always be <code>TRUE</code>, setting it to <code>FALSE</code> will tend to return a less useful structure:</p>
<div><pre><code><span></span><span>head</span><span>(</span>extract_tables<span>(</span>sos_url<span>,</span> guess <span>=</span> <span>FALSE</span><span>)[[</span><span>1</span><span>]],</span> <span>10</span><span>)</span>
</code></pre></div><div><pre><code><span></span>##       [,1]
##  [1,] &quot;&quot;
##  [2,] &quot;&quot;
##  [3,] &quot;&quot;
##  [4,] &quot;&quot;
##  [5,] &quot;Election Date&quot;
##  [6,] &quot;Nov. 8, 1910&quot;
##  [7,] &quot;Nov. 5, 1912 P&quot;
##  [8,] &quot;Nov. 3, 1914&quot;
##  [9,] &quot;Nov. 7, 1916 P&quot;
## [10,] &quot;Nov. 5, 1918&quot;
##       [,2]
##  [1,] &quot;HISTORICAL VOTER REGISTRATION AND&quot;
##  [2,] &quot;PARTICIPATION IN STATEWIDE GENERAL ELECTIONS 1910-2016&quot;
##  [3,] &quot;Registration Votes Cast&quot;
##  [4,] &quot;Turnout&quot;
##  [5,] &quot;Eligible Democratic Republican Other Total Total Votes Registered&quot;
##  [6,] &quot;725,000 * * * *393,893*&quot;
##  [7,] &quot;1,569,000 * * * 987,368 707,776 71.68%&quot;
##  [8,] &quot;1,726,000 * * * 1,219,345 961,868 78.88%&quot;
##  [9,] &quot;1,806,000 * * * 1,314,446 1,045,858 79.57%&quot;
## [10,] &quot;1,918,000 * * * 1,203,898 714,525 59.35%&quot;
##       [,3]
##  [1,] &quot;&quot;
##  [2,] &quot;&quot;
##  [3,] &quot;&quot;
##  [4,] &quot;Turnout&quot;
##  [5,] &quot;Eligible&quot;
##  [6,] &quot;54.33%&quot;
##  [7,] &quot;45.11%&quot;
##  [8,] &quot;55.73%&quot;
##  [9,] &quot;57.91%&quot;
## [10,] &quot;37.25%&quot;
</code></pre></div>
<p>However, it can be useful if users want to specify the locations of tables manually. The <code>area</code> argument allows users to specifying a <code>c(top,left,bottom,right)</code> vector of coordinates for the location of tables on a page (which is useful if pages also contain other non-tabular content); setting <code>columns</code> with <code>guess = FALSE</code> indicates where the column breaks are within a table. With a little care in specifying column positions we can successfully separate the &quot;P&quot; flags specifying Presidential elections that were earlier concatenated with the election dates:</p>
<div><pre><code><span></span>cols <span>&lt;-</span> <span>list</span><span>(</span><span>c</span><span>(</span><span>76</span><span>,</span><span>123</span><span>,</span><span>126</span><span>,</span><span>203</span><span>,</span><span>249</span><span>,</span><span>297</span><span>,</span><span>342</span><span>,</span><span>392</span><span>,</span><span>453</span><span>,</span><span>498</span><span>,</span><span>548</span><span>))</span>
tab4 <span>&lt;-</span> extract_tables<span>(</span>sos_url<span>,</span> guess <span>=</span> <span>FALSE</span><span>,</span> columns <span>=</span> cols<span>)</span>

<span># save header</span>
h <span>&lt;-</span> tab4<span>[[</span><span>1</span><span>]][</span><span>5</span><span>,</span><span>-1</span><span>]</span>
<span># clean tables</span>
tab4<span>[[</span><span>1</span><span>]]</span> <span>&lt;-</span> tab4<span>[[</span><span>1</span><span>]][</span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>5</span><span>,</span><span>62</span><span>),</span><span>-1</span><span>]</span>
tab4<span>[[</span><span>2</span><span>]]</span> <span>&lt;-</span> tab4<span>[[</span><span>2</span><span>]][</span><span>-</span><span>c</span><span>(</span><span>1</span><span>:</span><span>5</span><span>,</span><span>10</span><span>:</span><span>17</span><span>),</span><span>-1</span><span>]</span>
<span># merge into one table</span>
tab4df <span>&lt;-</span> setNames<span>(</span><span>as.data.frame</span><span>(</span><span>do.call</span><span>(</span><span>&quot;rbind&quot;</span><span>,</span> tab4<span>),</span> stringsAsFactors <span>=</span> <span>FALSE</span><span>),</span> h<span>)</span>
str<span>(</span>tab4df<span>)</span>
</code></pre></div><div><pre><code><span></span>## 'data.frame':    60 obs. of  10 variables:
##  $ Election Date: chr  &quot;Nov. 8, 1910&quot; &quot;Nov. 5, 1912&quot; &quot;Nov. 3, 1914&quot; &quot;Nov. 7, 1916&quot; ...
##  $              : chr  &quot;&quot; &quot;P&quot; &quot;&quot; &quot;P&quot; ...
##  $ Eligible     : chr  &quot;725,000&quot; &quot;1,569,000&quot; &quot;1,726,000&quot; &quot;1,806,000&quot; ...
##  $ Democratic   : chr  &quot;*&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##  $ Republican   : chr  &quot;*&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##  $ Other        : chr  &quot;*&quot; &quot;*&quot; &quot;*&quot; &quot;*&quot; ...
##  $ Total        : chr  &quot;*&quot; &quot;987,368&quot; &quot;1,219,345&quot; &quot;1,314,446&quot; ...
##  $ Total Votes  : chr  &quot;393,893&quot; &quot;707,776&quot; &quot;961,868&quot; &quot;1,045,858&quot; ...
##  $ Registered   : chr  &quot;*&quot; &quot;71.68%&quot; &quot;78.88%&quot; &quot;79.57%&quot; ...
##  $ Eligible     : chr  &quot;54.33%&quot; &quot;45.11%&quot; &quot;55.73%&quot; &quot;57.91%&quot; ...
</code></pre></div>
<p>Figuring out columns positions and/or table areas is quite challenging to do by hand, so the <code>locate_areas()</code> provides an interactive interface for identifying areas. It returns lists of coordinates for specific table areas. A higher-level function, <code>extract_areas()</code>, connects that GUI directly to <code>extract_tables()</code> to return the tables within specified areas. Two other functions can be useful in this respect: <code>get_n_pages()</code> indicates the number of pages in a PDF and <code>get_page_dims()</code> indicates the dimensions of the pages.</p>
<h2>Some other functionality</h2>
<p>In addition to the core functionality around <code>extract_tables()</code>, <code>tabulizer</code> also provides some functions for working with PDFs that might be useful to those trapped in other peoples' data. We'll download the file first just to save some time:</p>
<div><pre><code><span></span>tmp <span>&lt;-</span> <span>tempfile</span><span>(</span>fileext <span>=</span> <span>&quot;.pdf&quot;</span><span>)</span>
download.file<span>(</span>sos_url<span>,</span> destfile <span>=</span> tmp<span>,</span> mode <span>=</span> <span>&quot;wb&quot;</span><span>,</span> quiet <span>=</span> <span>TRUE</span><span>)</span>
</code></pre></div>
<p>The <code>extract_text()</code> function extracts text content of the PDF, separately by page, as character strings:</p>
<div><pre><code><span></span>extract_text<span>(</span>tmp<span>)</span>
</code></pre></div><div><pre><code><span></span>## [1] &quot;4Election Date Eligible Democratic Republican Other         Total Total Votes\r\nTurnout \r\nRegistered\r\nTurnout \r\nEligible\r\nNov. 8, 1910 725,000 * * *             *    393,893              * 54.33%\r\nNov. 5, 1912 P 1,569,000 * * * 987,368 707,776 71.68% 45.11%\r\nNov. 3, 1914 1,726,000 * * * 1,219,345 961,868 78.88% 55.73%\r\nNov. 7, 1916 P 1,806,000 * * * 1,314,446 1,045,858 79.57% 57.91%\r\nNov. 5, 1918 1,918,000 * * * 1,203,898 714,525 59.35% 37.25%\r\nNov. 2, 1920 P 2,090,000 * * * 1,374,184 987,632 71.87% 47.26%\r\nNov. 7, 1922 2,420,000 319,107 968,429 244,848 1,532,384 1,000,997 65.32% 41.36%\r\nNov. 4, 1924 P 2,754,000 397,962 1,183,672 240,723 1,822,357 1,336,598 73.34% 48.53%\r\nNov. 2, 1926 2,989,000 410,290 1,298,062 204,510 1,912,862 1,212,452 63.38% 40.56%\r\nNov. 6, 1928 P 3,240,000 592,161 1,535,751 185,904 2,313,816 1,846,077 79.78% 56.98%\r\nNov. 4, 1930 3,463,000 456,096 1,638,575 150,557 2,245,228 1,444,872 64.35% 41.72%\r\nNov. 8, 1932 P 3,573,000 1,161,482 1,565,264 162,267 2,889,013 2,330,132 80.65% 65.22%\r\nNov. 6, 1934 3,674,000 1,555,705 1,430,198 154,211 3,140,114 2,360,916 75.19% 64.26%\r\nNov. 3, 1936 P 3,844,000 1,882,014 1,244,507 127,300 3,253,821 2,712,342 83.36% 70.56%\r\nNov. 8, 1938 4,035,000 2,144,360 1,293,929 173,127 3,611,416 2,695,904 74.65% 66.81%\r\nNov. 5, 1940 P 4,214,000 2,419,628 1,458,373 174,394 4,052,395 3,300,410 81.44% 78.32%\r\nNov. 3, 1942 4,693,000 2,300,206 1,370,069 150,491 3,820,776 2,264,288 59.26% 48.25%\r\nNov. 7, 1944 P 5,427,000 2,418,965 1,548,395 173,971 4,141,331 3,566,734 86.13% 65.72%\r\nNov. 5, 1946 5,800,000 2,541,720 1,637,246 204,997 4,383,963 2,759,641 62.95% 47.58%\r\nNov. 2, 1948 P 6,106,000 2,892,222 1,908,170 261,605 5,061,997 4,076,981 80.54% 66.77%\r\nNov. 7, 1950 6,458,000 3,062,205 1,944,812 237,820 5,244,837 3,845,757 73.32% 59.55%\r\nNov. 4, 1952 P 7,033,000 3,312,668 2,455,713 229,919 5,998,300 5,209,692 86.85% 74.07%\r\nNov. 2, 1954 7,565,000 3,266,831 2,415,249 203,157 5,885,237 4,101,692 69.69% 54.22%\r\nNov. 6, 1956 P 8,208,000 3,575,635 2,646,249 186,937 6,408,821 5,547,621 86.56% 67.59%\r\nNov. 4, 1958 8,909,000 3,875,630 2,676,565 200,226 6,752,421 5,366,053 79.47% 60.23%\r\nNov. 8, 1960 P 9,587,000 4,295,330 2,926,408 242,888 7,464,626 6,592,591 88.32% 68.77%\r\nNov. 6, 1962 10,305,000 4,289,997 3,002,038 239,176 7,531,211 5,929,602 78.73% 57.54%\r\nNov. 3, 1964 P 10,959,000 4,737,886 3,181,272 264,985 8,184,143 7,233,067 88.38% 66.00%\r\nNov. 8, 1966 11,448,000 4,720,597 3,350,990 269,281 8,340,868 6,605,866 79.20% 57.70%\r\nNov. 5, 1968 P 11,813,000 4,682,661 3,462,131 442,881 8,587,673 7,363,711 85.75% 62.34%\r\nNov. 3, 1970 12,182,000 4,781,282 3,469,046 456,019 8,706,347 6,633,400 76.19% 54.45%\r\nNov. 7, 1972 P 13,322,000 5,864,745 3,840,620 760,850 10,466,215 8,595,950 82.13% 64.52%\r\nNov. 6, 1973 S 13,512,000 5,049,959 3,422,291 617,569 9,089,819 4,329,017 47.62% 32.04%\r\nNov. 5, 1974 13,703,000 5,623,831 3,574,624 729,909 9,928,364 6,364,597 64.11% 46.45%\r\nNov. 2, 1976 P 14,196,000 5,725,718 3,468,439 786,331 9,980,488 8,137,202 81.53% 57.32%\r\nNov. 7, 1978 14,781,000 5,729,959 3,465,384 934,643 10,129,986 7,132,210 70.41% 48.25%\r\nNov. 6, 1979 S 15,083,000 5,594,018 3,406,854 1,006,085 10,006,957 3,740,800 37.38% 24.80%\r\nNov. 4, 1980 P 15,384,000 6,043,262 3,942,768 1,375,593 11,361,623 8,775,459 77.24% 57.04%\r\nNov. 2, 1982 15,984,000 6,150,716 4,029,684 1,378,699 11,559,099 8,064,314 69.78% 50.45%\r\nNov. 6, 1984 P 16,582,000 6,804,263 4,769,129 1,500,238 13,073,630 9,796,375 74.93% 59.08%\r\nNov. 4, 1986 17,561,000 6,524,496 4,912,581 1,396,843 12,833,920 7,617,142 59.35% 43.38%\r\nNov. 8, 1988 P 19,052,000 7,052,368 5,406,127 1,546,378 14,004,873 10,194,539 72.81% 53.51%\r\nNov. 6, 1990 19,245,000 6,671,747 5,290,202 1,516,078 13,478,027 7,899,131 58.61% 41.05%\r\nNov. 3, 1992 P 20,864,000 7,410,914 5,593,555 2,097,004 15,101,473 11,374,565 75.32% 54.52%\r\nNov. 2, 1993 S 20,797,000 7,110,142 5,389,313 2,043,168 14,524,623 5,282,443 36.37% 27.73%\r\nNov. 8, 1994 18,946,000 7,219,635 5,472,391 2,031,758 14,723,784 8,900,593 60.45% 46.98%\r\nNov. 5, 1996 P 19,526,991 7,387,504 5,704,536 2,570,035 15,662,075 10,263,490 65.53% 52.56%\r\nNov. 3, 1998 20,806,462 6,989,006 5,314,912 2,665,267 14,969,185 8,621,121 57.59% 41.43%\r\nNov. 7, 2000 P 21,461,275 7,134,601 5,485,492 3,087,214 15,707,307 11,142,843 70.94% 51.92%\r\nNov. 5, 2002 21,466,274 6,825,400 5,388,895 3,089,174 15,303,469 7,738,821 50.57% 36.05%\r\nOct.  7, 2003 S 21,833,141 6,718,111 5,429,256 3,236,059 15,383,526 9,413,494 61.20% 43.12%\r\nNov. 2, 2004 P 22,075,036 7,120,425 5,745,518 3,691,330 16,557,273 12,589,683 76.04% 57.03%\r\nNov. 8, 2005 S 22,487,768 6,785,188 5,524,609 3,581,685 15,891,482 7,968,757 50.14% 35.44%\r\nNov. 7, 2006 22,652,190 6,727,908 5,436,314 3,672,886 15,837,108 8,899,059 56.19% 39.29%\r\nNov. 4, 2008 P 23,208,710 7,683,495 5,428,052 4,192,544 17,304,091 13,743,177 79.42% 59.22%\r\nMay 19, 2009 S 23,385,819 7,642,108 5,325,558 4,185,346 17,153,012 4,871,945 28.40% 20.80%\r\nHISTORICAL VOTER REGISTRATION AND\r\nPARTICIPATION IN STATEWIDE GENERAL ELECTIONS 1910-2016\r\nVotes CastRegistration\r\n5Election Date Eligible Democratic Republican Other         Total Total Votes\r\nTurnout \r\nRegistered\r\nTurnout \r\nEligible\r\nNov. 2, 2010 23,551,699 7,620,240 5,361,875 4,303,768 17,285,883 10,300,392 59.59% 43.74%\r\nNov. 6, 2012 P 23,802,577 7,966,422 5,356,608 4,922,940 18,245,970 13,202,158 72.36% 55.47%\r\nNov. 4, 2014 24,288,145 7,708,683 5,005,422 5,089,718 17,803,823 7,513,972 42.20% 30.94%\r\nNov. 8, 2016 P 24,875,293 8,720,417 5,048,398 5,642,956 19,411,771 14,610,509 75.27% 58.74%\r\nNotes\r\n* Indicates information not available. \r\nIn 1911, women gained the right to vote in California.\r\nIn 1972, the voting age was lowered from 21 to 18.\r\nRegistration Votes Cast\r\nP indicates a presidential election year.\r\nThe first statewide record of party affiliations was reported in 1922.\r\nHISTORICAL VOTER REGISTRATION AND\r\nPARTICIPATION IN STATEWIDE GENERAL ELECTIONS 1910-2016 (continued)\r\nS indicates a statewide special election.\r\n&quot;
</code></pre></div>
<p>This can be useful for non-tabular content, getting a sense of the document's contents, or troubleshooting the main extraction function (e.g., sometimes there is non-visible text that confuses <code>extract_tables()</code>). <code>extract_metadata()</code> returns a list of the PDF's embedded document metadata:</p>
<div><pre><code><span></span>str<span>(</span>extract_metadata<span>(</span>tmp<span>))</span>
</code></pre></div><div><pre><code><span></span>## List of 10
##  $ pages   : int 2
##  $ title   : chr &quot;Statement of Vote - General Election, November 8, 2016&quot;
##  $ author  : NULL
##  $ subject : chr &quot;Statement of Vote - General Election, November 8, 2016&quot;
##  $ keywords: chr &quot;Statement of Vote - General Election, November 8, 2016&quot;
##  $ creator : chr &quot;Acrobat PDFMaker 11 for Excel&quot;
##  $ producer: chr &quot;Adobe PDF Library 11.0&quot;
##  $ created : chr &quot;Fri Dec 16 18:54:13 GMT 2016&quot;
##  $ modified: chr &quot;Fri Dec 16 18:54:44 GMT 2016&quot;
##  $ trapped : NULL
</code></pre></div>
<p>The <code>make_thumbnails()</code> function produces images (by default PNG) of pages, which can also be useful for debugging or just for the mundane purpose of image conversion:</p>
<div><pre><code><span></span>thumb <span>&lt;-</span> make_thumbnails<span>(</span>tmp<span>,</span> pages <span>=</span> <span>1</span><span>)</span>
<span>library</span><span>(</span><span>&quot;png&quot;</span><span>)</span>
thispng <span>&lt;-</span> readPNG<span>(</span>thumb<span>,</span> native <span>=</span> <span>TRUE</span><span>)</span>
d <span>&lt;-</span> get_page_dims<span>(</span>tmp<span>,</span> pages <span>=</span> <span>1</span><span>)[[</span><span>1</span><span>]]</span>
plot<span>(</span><span>c</span><span>(</span><span>0</span><span>,</span> d<span>[</span><span>1</span><span>]),</span> <span>c</span><span>(</span><span>0</span><span>,</span> d<span>[</span><span>2</span><span>]),</span> type <span>=</span> <span>&quot;n&quot;</span><span>,</span> xlab <span>=</span> <span>&quot;&quot;</span><span>,</span> ylab <span>=</span> <span>&quot;&quot;</span><span>,</span> asp <span>=</span> <span>1</span><span>)</span>
rasterImage<span>(</span>thispng<span>,</span> <span>0</span><span>,</span> <span>0</span><span>,</span> d<span>[</span><span>1</span><span>],</span> d<span>[</span><span>2</span><span>])</span>
</code></pre></div>
<p></p>
<p>And, lastly, the <code>split_pdf()</code> and <code>merge_pdf()</code> functions can extract specific pages from a PDF or merge multiple PDFs together. Those functions should find multiple uses cases beyond the challenges of working with other peoples' data.</p>
<h1>Conclusion</h1>
<p><code>tabulizer</code> can't solve all your PDF problems. More likely than not you'll at some point encounter a PDF that contains scanned tables or tables that tabula-java's algorithms can't identify well. But for a wide array of well-formatted PDF tables, <code>tabulizer</code> should provide a much simpler and much faster initial extraction of data than attempting to transcribe their contents manually.</p>
<h2>Contribute</h2>
<p>As always, the <a href="https://github.com/ropensci/tabulizer/issues">issue tracker</a> on Github is open for suggestions, bug reports, and package support. <a href="https://github.com/ropensci/tabulizer/pulls">Pull requests</a> are always welcome.</p>
<p>I've flagged some specific issues on GitHub which interested users might want to help out with. These range from some basic issues:</p>
<ul><li>Identifying and creating <a href="https://github.com/ropensci/tabulizer/issues/47">example use cases for the new <code>tabulizer</code> wiki</a> to showcase how the package works</li>
<li>Adding <a href="https://github.com/ropensci/tabulizer/issues/46">comprehensive, cross-platform installation instructions</a> to deal with the various intricacies of Java and rJava on various platforms</li>
</ul><p>To moderately difficult issues, like:</p>
<ul><li><a href="https://github.com/ropensci/tabulizer/issues/49">Improving the functionality and attractiveness of the Shiny-based <code>extract_areas()</code> graphical interface</a></li>
</ul><p>To more advanced topics that more experienced developers - especially those with Java experience - might be interested in working on:</p>
<ul><li>Improving handling of <a href="https://github.com/ropensci/tabulizer/issues/10">non-latin encodings</a> including adding tests thereof</li>
<li>Preparing <code>tabulizer</code> for <a href="https://github.com/ropensci/tabulizer/issues/48">the migration of the tabula-java library to PDFBox 2.0</a>, which will change some of the underlying classes (and methods thereof) that <code>tabulizer</code> calls from both tabula-java and PDFBox</li>
</ul><p>Help of any kind on these issues will be very useful for getting the package ready for CRAN release!</p>
<h2>Acknowledgments</h2>
<p>Many, many thanks to the Tabula team who have done considerable work to make the tabula-java library on which <code>tabulizer</code> depends. I also want to express considerable thanks to <a href="https://github.com/davidgohel">David Gohel</a> and <a href="https://github.com/lmullen">Lincoln Mullen</a> for their feedback during the <a href="https://github.com/ropensci/onboarding/issues/42">rOpenSci onboarding process</a>, which resulted in numerous improvements to the package and its usability, not least of which is the interactive shiny widget. Thanks, too, to <a href="https://github.com/sckott">Scott Chamberlain</a> for overseeing the review process and to the whole of rOpenSci for their support of the R community.</p>