Skip to content

Instantly share code, notes, and snippets.

@mskyttner
Last active June 10, 2019 10:04
Show Gist options
  • Save mskyttner/72ef8993094f4f30875e28e8642c597c to your computer and use it in GitHub Desktop.
Save mskyttner/72ef8993094f4f30875e28e8642c597c to your computer and use it in GitHub Desktop.

Filling out PDF files using staplr

Markus Skyttner 2019-06-10

# to render this into GitHub markdown:
# rmarkdown::render("name_of_this_file.R")

library(staplr)

# this is the PDF form/template to be filled
pdf <- system.file('testForm.pdf', package = 'staplr')

# view the PDF file
#file.show(pdf) 

# find the fields in the PDF
fields <- get_fields(pdf)

# change values
fields$TextField1$value <- 
  "Blaha - this is the input I want to provide"

# output the results to a new file
set_fields(pdf, "output.pdf", fields)

# inspect the result
#file.show("output.pdf")
@mskyttner
Copy link
Author

mskyttner commented Jun 10, 2019

The gist above was generated from the R script below, saved as staplr.R and then rendered into markdown with rmarkdown::render('staplr.R'), note the YAML front matter which converts the R script to a GitHub flavored markdown file:

#' ---
#' title: "Filling out PDF files using staplr"
#' author: Markus Skyttner
#' output: 
#'  github_document
#' ---

# to render this into GitHub markdown:
# rmarkdown::render("name_of_this_file.R")

library(staplr)

# this is the PDF form/template to be filled
pdf <- system.file('testForm.pdf', package = 'staplr')

# view the PDF file
#file.show(pdf) 

# find the fields in the PDF
fields <- get_fields(pdf)

# change values
fields$TextField1$value <- 
  "Blaha - this is the input I want to provide"

# output the results to a new file
set_fields(pdf, "output.pdf", fields)

# inspect the result
#file.show("output.pdf")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment