Search code examples
rformspdffdf

Filling PDF forms in R?


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!


Solution

  • 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')