Filling PDF forms in R?
Asked Answered
L

1

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!

Lorenalorene answered 24/1, 2018 at 15:21 Comment(2)
Relevant post? #15397255 Creating multiple PDFs in a loop.Noted
@Noted 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?Lorenalorene
P
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')
Percent answered 9/4, 2018 at 20:55 Comment(3)
Could you point me in the right direction to installing the pdftk server on Windows and hooking it up to R?Plagio
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.Percent
Hey @Percent 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: #69921905Boer

© 2022 - 2024 — McMap. All rights reserved.