CRAN_Status_Badge


Last-changedate

staplr

This package provides functions to manipulate PDF files:

  • fill out PDF forms: get_fields() and set_fields()
  • merge multiple PDF files into one: staple_pdf()
  • remove selected pages from a file: remove_pages()
  • rename multiple files in a directory: rename_files()
  • rotate entire pdf document: rotate_pdf()
  • rotate selected pages of a pdf file: rotate_pages()
  • Select pages from a file: select_pages()
  • splits single input PDF document into individual pages: split_pdf()
  • splits single input PDF document into parts from given points: split_from()

This package is still under development and this repository contains a development version of the R package staplr.

Installation

staplr requires a Java installation on your system. You can get the latest version of java from here. OpenJDK also works.

You can install the stable version from CRAN.

install.packages('staplr', dependencies = TRUE)

You can install staplr from github with:

# install.packages("devtools")
devtools::install_github("pridiltal/staplr")

Example

library(staplr)
# Merge multiple PDF files into one
staple_pdf()

# This command prompts the user to select the file interactively. 
# Remove page 2 and 3 from the selected file.
remove_pages(rmpages = c(2,3))

# This function selects pages from a file;
select_pages(selpages = c(1,3))

# This function splits a single input PDF document into individual pages
split_pdf()

# This function writes renamed files back to directory
#if the directory contains 3 PDF files
rename_files(new_names = paste("file",1:3))

# These functions are to fill out pdf forms
get_fields() 
set_fields()
# This includes 2 external functions `get_fields` and `set_fields` 
# and files to use as examples.
# This is what the example file looks like

# If you get path to this file by
pdfFile = system.file('testForm.pdf',package = 'staplr')

# And do
fields = get_fields(pdfFile)
# You'll get a list of fields that the pdf contains 
# along with some additional information about the fields.

# You make modifications in any of the fields by
fields$TextField1$value = 'this is text'
set_fields(pdfFile, 'newFile.pdf', fields)

# This will create a filled pdf file

Troubleshooting and 2.11.0 changes

  • As of version 2.11.0, the package uses pdftk-java instead of using the original pdftk. pdftk-java is included with the package so if you have a working java installation, you shouldn’t have any problems.

  • While default java options should be enough for most use cases, if you need to, you can change java options that is used to run pdftk by doing

options('staplr_java_options' = '-Xmx512m') 

This option is not affected by rJava settings.

  • If you don’t have a working java installation, your installation will fail since you can’t install rJava. Make sure you follow the proper instructions for java installation. For openJDK on linux make sure you get both jdk and jre and run javareconf.

Also restart your R session after javareconf

  • pdftk-java is built as a faithful representation of the original pdftk so there shouldn’t be any major differences between the outputs. However, for any reason you’d prefer to run a local installation of pdftk rather than using the version that is shipped with the package, do
# set staplr_custom_pdftk to the path to local installation
# just setting to pdftk will do if it's already in your path
 options('staplr_custom_pdftk' = 'pdftk') 

If you want to do this, you can get the original version of pdftk from here. Note that MacOS users with a version higher than “High Sierra” should use this version instead.

Make sure to set the option back to NULL if you want to use the built in pdftk later.