Skip to content

Instantly share code, notes, and snippets.

@zacharysyoung
Last active July 25, 2024 19:58
Show Gist options
  • Save zacharysyoung/a710c35fb760986a7233a8b5e795e80c to your computer and use it in GitHub Desktop.
Save zacharysyoung/a710c35fb760986a7233a8b5e795e80c to your computer and use it in GitHub Desktop.
Import rows of data into individual PDFs

Import rows of data into individual PDFs

How to get data like this...

Name Age Street Address City State Zip
Tami 23 123 Main St Anytown Anystate 11111
John 54 456 Second Ave Anytown Anystate 22222
Troy 39 789 Last Cir Anytown Anystate 99999

into a set of PDFs like this...

Filled-in PDF Forms

Using this Gist

All the files necessary to test this process are included below, except the empty form PDF which I don't think I can upload/include, even in a comment, so I'll at least show a picture of the PDF in Prepare Form mode:

Prepare Form: Empty, Awesome Form

Process

Using JavaScript

You'll need some familiarity with running JavaScript in Acrobat. Search around for "Run Javascript in Acrobat debugger/console". This is a pretty good starting place, even for Mac, The Acrobat JavaScript Console (Your best friend for developing Acrobat JavaScript). It's pretty easy to open Debugger, copy-paste your code into ViewConsole, then Cmd/Ctrl-A to select-all, and finally Cmd/Ctrl-Enter to "run selected".

If you're running directly in the debugger, you'll be in a Privileged mode and that'll be that, but if you want to run from a button in a PDF, you'll need to read up on security, functions, automation, etc...:

The documentation for Javascript in Acrobat:

From that Doc methods page, search for importTextData which is the backbone of this script/automation.

Making the data

The easiest way to make sure you have the correct fields in your data TSV/TXT file is just open your form in Prepare Form mode and export the form. This will give you an empty TXT file with the columns/header your input data file will need:

Export your form's fields/headers

Name Age Street Address City State Zip
Tami 23 123 Main St Anytown Anystate 11111
John 54 456 Second Ave Anytown Anystate 22222
Troy 39 789 Last Cir Anytown Anystate 99999
/* globals app */
// Define root path: where you've put input TXT data, the input form, and where the output forms will be created
var rootPath = "/Users/zyoung/Downloads/Awesome_Form/";
var emptyForm = "Page-1_SECURED.pdf";
// Tab-separated value (TSV) "txt" file. The data must have a header row so
// Acrobat can match up your data/cell values to the fields in your form
var data = "Page_1_data_filled.txt";
// We don't know ahead of time how many rows there will be, so keep incrementing
// `dataRowNumber` till it passes the last row, then we'll break out of the loop (below)
// After reading the header row, Acrobat starts counting data rows from 0
var dataRowNumber = 0;
do {
// Open the empty form
var doc = app.openDoc(rootPath + emptyForm);
// Try to import a row
var returnCode = doc.importTextData(rootPath + data, dataRowNumber);
// Normal: there are no more rows, close form and break out of do-loop
if (returnCode === -3) {
doc.closeDoc();
break;
}
// One of the errors we know about
if (returnCode === 3) {
app.alert("Error: Invalid Row - some number of column headers in " + data + " do not exactly match the field names in your form");
break;
}
// Any other error we don't know about
if (returnCode !== 0) {
app.alert("Error: was not able to import data for row " + (dataRowNumber + 1) + "; return code is " + returnCode);
break;
}
// Finally, save the filled-in form
var saveName = emptyForm.replace(".pdf", "");
var savePath = rootPath + saveName + "_" + (dataRowNumber + 1) + ".pdf";
try {
doc.saveAs(savePath);
doc.closeDoc();
} catch (e) {
app.alert("Encountered expection while trying to save then close " + savePath + "...");
app.alert(e);
break;
}
// Increment row number by 1 to try and look up the next row (if there is one)
dataRowNumber += 1;
} while (true); // keeps the do-loop running until *we decide* to break out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment