---
title: "Introduction to tabulizer"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Introduction to tabulizer}
  %\VignetteEngine{knitr::rmarkdown}
  \usepackage[utf8]{inputenc}
---

**tabulizer** provides R bindings to the [Tabula java library](https://github.com/tabulapdf/tabula-java/), which can be used to computationally extract tables from PDF documents. The main function `extract_tables()` mimics the command-line behavior of the Tabula, by extracting all tables from a PDF file and, by default, returns those tables as a list of character matrices in R.

```{r}
library("tabulizer")
f <- system.file("examples", "data.pdf", package = "tabulizer")

# extract table from first page of example PDF
tab <- extract_tables(f, pages = 1)
head(tab[[1]])
```

The `pages` argument allows you to select which pages to attempt to extract tables from. By default, Tabula (and thus tabulizer) checks every page for tables using a detection algorithm and returns all of them. `pages` can be an integer vector of any length; pages are indexed from 1.

It is possible to specify a remote file, which will be copied to R's temporary directory before processing:

```{r}
f2 <- "https://github.com/leeper/tabulizer/raw/master/inst/examples/data.pdf"
extract_tables(f2, pages = 2)
```

## Changing the Method of Extraction

The default method used by `extract_tables()` mimics the behaviour of Tabula.
For each page the algorithm decides whether it contains one consistent table
and then extracts it by using spreadsheet-tailored algorithm
`method = "lattice"`. The correct recognition of a table depends on whether the
page contains a table grid. If it doesn't and the table is a matrix of cells
with values without borders, it might not be able to recognise it. This also
happens when multiple tables with different number of columns are present on the
same page. In those cases another, more general, algorithm `method = "stream"`
is used, which relies on the distances between text characters on the page.

```{r}
# Extract tables by deciding for each page individually
extract_tables(f2, method = "decide")
```

It is possible to specify the preferred algorithm which might be a better option
for more difficult cases.

```{r}
# Extract tables by using "lattice" method
extract_tables(f2, pages = 2, method = "lattice")
```

```{r}
# Extract tables by using "stream" method
extract_tables(f2, pages = 2, method = "stream")
```

## Modifying the Return Value ##

By default, `extract_tables()` returns a list of character matrices. This is because many tables might be malformed or irregular and thus not be easily coerced to an R data.frame. This can easily be changed by specifying the `output` argument:

```{r}
# attempt to coerce tables to data.frames
extract_tables(f, pages = 2, output = "data.frame")
```

Tabula itself implements three "writer" methods that write extracted tables to disk as CSV, TSV, or JSON files. These can be specified by `output = "csv"`, `output = "tsv"`, and `output = "json"`, respectively. For CSV and TSV, one file is written to disk for each table and R session's temporary directory `tempdir()` is used by default (alternatively, the directory can be specified through `output` argument). For JSON, one file is written containing information about all tables. For these methods, `extract_tables()` returns a path to the directory containing the output files.

```{r}
# extract tables to CSVs
extract_tables(f, output = "csv")
```

If none of the standard methods works well, you can specify `output = "asis"` to return an rJava "jobjRef" object, which is a pointer to a Java ArrayList of Tabula Table objects. Working with that object might be quite awkward as it requires knowledge of Java and Tabula's internals, but might be useful to advanced users for debugging purposes.

## Extracting Areas ##

By default, tabulizer uses Tabula's table detection algorithm to automatically identify tables within each page of a PDF. This automatic detection can be toggled off by setting `guess = FALSE` and specifying an "area" within each PDF page to extract the table from. Here is a comparison of the default settings, versus extracting from two alternative areas within a page:

```{r}
str(extract_tables(f, pages = 2, guess = TRUE, output = "data.frame"))
str(extract_tables(f, pages = 2, area = list(c(126, 149, 212, 462)), guess = FALSE, output = "data.frame"))
str(extract_tables(f, pages = 2, area = list(c(126, 284, 174, 417)), guess = FALSE, output = "data.frame"))
```

The `area` argument should be a list either of length 1 (to use the same area for each specified page) or equal to the number of pages specified. This also means that you can extract multiple areas from one page, but specifying the page twice and indicating the two areas separately:

```{r}
a2 <- list(c(126, 149, 212, 462), c(126, 284, 174, 417))
str(extract_tables(f, pages = c(2,2), area = a2, guess = FALSE, output = "data.frame"))
```

## Interactive Table Extraction ##

In addition to the programmatic extraction offered by `extract_tables()`, it is also possible to work interactively with PDFs via the `extract_areas()` function. This function triggers a process by which each (specified) page of a PDF is converted to a PNG image file and then loaded as an R graphic. From there, you can use your mouse to specify upper-left and lower-right bounds of an area on each page. Pages are cycled through automatically and, after selecting areas for each page, those areas are extracted auto-magically (and the return value is the same as for `extract_tables()`). Here's a shot of it in action:

[![extract_areas()](http://i.imgur.com/USTyQl7.gif)](http://i.imgur.com/USTyQl7.gif)

`locate_areas()` handles the area identification process without performing the extraction, which may be useful as a debugger, or simply to define areas to be used in a programmatic extraction.

## Miscellaneous Functionality ##

Tabula is built on top of the [Java PDFBox library](https://pdfbox.apache.org/)), which provides low-level functionality for working with PDFs. A few of these tools are exposed through tabulizer, as they might be useful for debugging or generally for working with PDFs. These functions include:


 - `extract_text()` converts the text of an entire file or specified pages into an R character vector.
 - `split_pdf()` and `merge_pdfs()` split and merge PDF documents, respectively.
 - `extract_metadata()` extracts PDF metadata as a list.
 - `get_n_pages()` determines the number of pages in a document.
 - `get_page_dims()` determines the width and height of each page in pt (the unit used by `area` and `columns` arguments).
 - `make_thumbnails()` converts specified pages of a PDF file to image files.

