staplr
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.
sudo apt update -y
sudo apt install -y openjdk-8-jdk openjdk-8-jre
sudo R CMD javareconf
Also restart your R session after javareconf
pdftk-java
is built as a faithful representation of the originalpdftk
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.