How to use staplr to manipulate PDF files using R [with basic examples]

The staplr package is a multitool function outfitting your interface to handle pdf forms and images for fully optimized presentation and data filing. Running on Java 8, the package can be readily downloaded into your operating system for quick utilization. With the ability to manipulate a pdf file down to the individual pages, we’ll cover some of the more regularly used parts of the tool to get an idea of how staplr can be used for your work.


Capabilities of the staplr library center primarily on editing files and pdf formats for presentation and file keeping. Looking up some of its code lines shows how straightforward the commands can be:
get_fields – retrieves an item from pdf files
idenfity_form_fields – names files and presents in one sheet
rotate_pages – rotates pages in a file
remove_pages – removes selected items from a file
These functions’ purposes are fairly easy to discern but come with a fair amount of necessary input needed to execute.
Let’s look at the rotation command. Along with specifying which pages we want to effect, we also have to specify the angle of rotation in respect to the center. So we rotate to the east 90 degrees, to the west 180, and so on. Make note that we can’t just make a sweeping change to the degrees as the function won’t accept those parameters. Here’s what we see:
rotate_pages(
rotatepages,
page_rotation = c(0, 90, 180, 270),
input_filepath = NULL,
output_filepath = NULL,
overwrite = TRUE
So already we can see there’s a bit more work needed for the staplr package.
Next let’s look at setting fields in a list that we can reference for later use. Starting with the file path, we’ll have the function filter through the pdf and find any outlying fields that we can address, then overwrite if we so wish. After finding an item that meets all the criteria, the set_field function will flatten the page into a title text listed on its resulting page.
set_fields(
input_filepath = NULL,
output_filepath = NULL,
fields,
overwrite = TRUE,


convert_field_names = FALSE,
flatten = FALSE
When it completes its run of the library, you’ll have a neat and tidy call list of the fields on one directory for you to pick and choose. Optimal user interface is the key goal of staplr and once you’ll gotten around its needed arguments the library of simple functions can become a powerful toolset.
Let’s look at some of the more common uses for the staplr package where it takes selected pages from your file and split or joins them without needing external system software. We’ll start by pulling pages from our R library.
pdf_subset(‘https://cran.r-project.org/doc/manuals/r-release/R-intro.pdf’,
pages = 1:3, output = “subset.pdf”)
With the pdf function tool, we’ll make the files easier to organize. After our pdfs are made the combine command will
pdf(“test.pdf”)
plot(mtcars)
dev.off()
pdf_combine(c(“test.pdf”, “subset.pdf”), output = “joined.pdf”)
Compressing and filing images or pdf format is one of the staplr packages best uses.
The multifunctionality of the R code is also highly usable on a surprising number of platforms including Linux.
pdftk file1.pdf file2.pdf cat output newFile.pdf
pdftk user_guide.pdf burst
pdftk html_tidy.pdf attach_files command_ref.html to_page 24 output html_tidy_book.pdf
are all lines for splitting, joining and adding attachments that can be read by the Linux reader. Many coders have liked using the filing tool as they’ve updated the tools constantly with new renditions addressing users’ needs.

Scroll to top