7

I am seeking a way to automate PDF form filling in R. I cannot find a package written to do this. Is there an option out there?

Alternative solutions I can think of:

  1. Using R to overlay a PDF containing text onto an blank PDF template.
  2. Using R to generate an FDF file that can be read by some other software or code in a different language.

All of these things seem doable in Python. However, my organization leans strongly towards R, and in the past has relied upon software devs to write C# to fill out the forms. I'm hoping to use R to skip over this step.

Thanks!

ADF
  • 522
  • 6
  • 14
  • 1
    Relevant post? https://stackoverflow.com/questions/15396755/using-loops-with-knitr-to-produce-multiple-pdf-reports-need-a-little-help-to Creating multiple PDFs in a loop. – zx8754 Jan 24 '18 at 15:44
  • @zx8754 That thread seems relevant to a different problem. In my case, I don't need to loop over multiple PDF generations. Instead, I need one PDF to be generated automatically upon an API call, and that PDF has precise parameters designated by the forms built into the PDF. Does that make sense? – ADF Jan 24 '18 at 19:18

1 Answers1

4

staplr package now supports this with get_fields and set_fields functions. Note that for this to work pdftk server must be installed and in your path

get_fields returns a list of fields and their types from a pdf that you can modify

set_fields allows you to fill form according to your modifications. See below code for an example

pdfFile = system.file('testForm.pdf',package = 'staplr')

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'

# and apply the changes you have made in a new file
set_fields(pdfFile, 'newFile.pdf', fields)

Note: Currently github version of staplr has fixes that are yet to make into CRAN that affect staplr's ability to write in non-english alphabets. For best experience you may want to install it by doing

devtools::install_github('pridiltal/staplr')
OganM
  • 2,543
  • 16
  • 33
  • Could you point me in the right direction to installing the pdftk server on Windows and hooking it up to R? – Pablo Boswell Oct 18 '18 at 19:49
  • 3
    You just need to download it and place it somewhere in your PATH, or add it to your PATH. You can change your path from Advanced system settings -> Environment Variables -> System Variables -> path. This is for windows 8/10. – OganM Oct 19 '18 at 20:35
  • Hey @OganM I tried to use the package that you suggested, staplr, but I am getting some errors. I posted a question in Stackoverflow. Would you please give your suggestions? Here is the link: https://stackoverflow.com/questions/69921405/find-keys-and-values-in-a-pdf-with-r – Beginner Nov 11 '21 at 00:32