Skip to content

Getting Started with PDF Oxide ®

PDF Oxide ships idiomatic R bindings for fast PDF text, Markdown, and HTML extraction — 0.8ms mean text extraction, 100% pass rate on 3,830 PDFs — backed by the same Rust core as every other binding. The R package wraps the pdf_oxide C ABI through R’s .Call interface; document handles are R external pointers freed by the garbage collector, and page indices are 0-based to match the underlying engine.

Installation

The R package links the default-feature cdylib. Build the native library, then install the package pointing it at the header and the cdylib:

# 1. build the native library (shipped binding feature set)
cargo build --release --lib \
  --features ocr,rendering,signatures,barcodes,tsa-client,system-fonts

# 2. install the R package
PDF_OXIDE_INCLUDE_DIR="$PWD/include" PDF_OXIDE_LIB_DIR="$PWD/target/release" \
  R CMD INSTALL r/

At runtime, make the cdylib discoverable to the linker:

LD_LIBRARY_PATH="$PWD/target/release" Rscript your_script.R

Opening a PDF

Open a file with pdf_open(), then inspect its metadata. pdf_version() returns a named list with major and minor.

library(pdfoxide)

doc <- pdf_open("research-paper.pdf")

pdf_page_count(doc)               # number of pages
v <- pdf_version(doc)
cat("PDF version:", paste(v$major, v$minor, sep = "."), "\n")
pdf_is_encrypted(doc)             # logical

Text Extraction

Extract reading-order text for a single 0-based page with pdf_extract_text().

library(pdfoxide)

doc <- pdf_open("report.pdf")
text <- pdf_extract_text(doc, 0)  # 0-based page index
cat(text)

Loop over every page using pdf_page_count():

doc <- pdf_open("book.pdf")
for (page in seq_len(pdf_page_count(doc)) - 1L) {   # 0-based indices
  cat("--- Page", page + 1L, "---\n")
  cat(pdf_extract_text(doc, page), "\n")
}

Markdown and HTML

Convert a single page to Markdown or HTML, or convert the whole document at once.

library(pdfoxide)

doc <- pdf_open("paper.pdf")

md  <- pdf_to_markdown(doc, 0)    # one page as Markdown
html <- pdf_to_html(doc, 0)       # one page as HTML

all_md   <- pdf_to_markdown_all(doc)    # whole document
all_text <- pdf_to_plain_text_all(doc)  # whole document, plain text

cat(all_md)

Words, Characters, and Lines

Element extraction returns lists of records with positional bounding boxes. Each bbox is a named list with x, y, width, and height.

library(pdfoxide)

doc <- pdf_open("paper.pdf")

# Positioned words — each has $text, $bbox, $font_name, $font_size, $bold
words <- pdf_extract_words(doc, 0)
for (w in head(words, 10)) {
  cat(sprintf("'%s' at (%.1f, %.1f) font=%s bold=%s\n",
              w$text, w$bbox$x, w$bbox$y, w$font_name, w$bold))
}

# Reading-order lines — each has $text, $bbox, $word_count
lines <- pdf_extract_text_lines(doc, 0)
for (ln in head(lines, 5)) {
  cat(sprintf("[%d words] %s\n", ln$word_count, ln$text))
}

# Positioned characters — $character is the Unicode codepoint (integer)
chars <- pdf_extract_chars(doc, 0)
for (ch in head(chars, 10)) {
  cat(sprintf("'%s' at (%.1f, %.1f) size=%.1f\n",
              intToUtf8(ch$character), ch$bbox$x, ch$bbox$y, ch$font_size))
}

Tables

pdf_extract_tables() returns detected tables. Each table record carries row_count, col_count, has_header, and a cells character matrix indexed 1-based as tbl$cells[row, col].

library(pdfoxide)

doc <- pdf_open("statement.pdf")
tables <- pdf_extract_tables(doc, 0)

for (tbl in tables) {
  cat(sprintf("Table: %d rows x %d cols (header=%s)\n",
              tbl$row_count, tbl$col_count, tbl$has_header))
  for (r in seq_len(tbl$row_count)) {
    cat(paste(tbl$cells[r, ], collapse = " | "), "\n")
  }
}

Search a single page with pdf_search(), or the whole document with pdf_search_all(). Both take an optional case_sensitive flag (default FALSE) and return records with text, page, and bbox.

library(pdfoxide)

doc <- pdf_open("manual.pdf")

# Whole document
hits <- pdf_search_all(doc, "configuration")
for (h in hits) {
  cat(sprintf("Page %d: '%s' at (%.0f, %.0f)\n",
              h$page, h$text, h$bbox$x, h$bbox$y))
}

# Single page, case-sensitive
page_hits <- pdf_search(doc, 0, "Configuration", case_sensitive = TRUE)

Opening from Bytes

Open a PDF held in memory — handy when reading from S3, HTTP, or a database — with pdf_open_from_bytes(), which takes a raw vector.

library(pdfoxide)

bytes <- readBin("report.pdf", "raw", file.info("report.pdf")$size)
doc <- pdf_open_from_bytes(bytes)
cat(pdf_extract_text(doc, 0))

Password-Protected PDFs

Open an encrypted document with pdf_open_with_password(), or call pdf_authenticate() after opening (it returns TRUE on success, FALSE for a wrong password).

library(pdfoxide)

doc <- pdf_open_with_password("confidential.pdf", "secret")
cat(pdf_extract_text(doc, 0))

Creating PDFs

The builder functions create a pdfoxide_pdf from Markdown, HTML, or plain text. Save it to a path with pdf_save(), or serialize to a raw vector with pdf_to_bytes() (which can be re-opened with pdf_open_from_bytes()).

library(pdfoxide)

pdf <- pdf_from_markdown("# Hello World\n\nThis is a PDF.\n")
pdf_save(pdf, "output.pdf")

pdf_from_html("<h1>Invoice</h1><p>Amount due: $42.00</p>") |>
  pdf_save("invoice.pdf")

pdf_from_text("Plain text document.\n\nSecond paragraph.") |>
  pdf_save("notes.pdf")

# Round-trip through bytes
bytes <- pdf_to_bytes(pdf_from_markdown("# In memory\n\nbody\n"))
doc <- pdf_open_from_bytes(bytes)
cat(pdf_extract_text(doc, 0))

Next Steps