Skip to content

Instantly share code, notes, and snippets.

@jastkand
Last active December 10, 2023 00:46
Show Gist options
  • Save jastkand/5132098 to your computer and use it in GitHub Desktop.
Save jastkand/5132098 to your computer and use it in GitHub Desktop.
An Adobe Illustrator Script that helps to generate a list of PDF documents from template. Replaces %name% and %lastname% with items from data array then saves the result to PDF file.
var data = [
["John", "Doe"],
["James", "Brown"]
];
function main() {
var firstName, lastName;
for (var key in data) {
firstName = data[key][0];
lastName = data[key][1];
replaceData(firstName, lastName)
var dest = "~/pdf/" + firstName + "_" + lastName + ".pdf";
saveFileToPDF(dest);
resetTemplate(firstName, lastName);
}
}
function saveFileToPDF (dest) {
var doc = app.activeDocument;
if ( app.documents.length > 0 ) {
var saveName = new File ( dest );
saveOpts = new PDFSaveOptions();
saveOpts.compatibility = PDFCompatibility.ACROBAT5;
saveOpts.generateThumbnails = true;
saveOpts.preserveEditability = true;
doc.saveAs( saveName, saveOpts );
}
}
function replaceData(firstName, lastName) {
var active_doc = app.activeDocument;
var firstNamePattern = /%name%/g;
var lastNamePattern = /%lastname%/g;
var textFrames = active_doc.textFrames;
if (textFrames.length > 0) {
for (var i = 0 ; i < textFrames.length; i++) {
var thisTextFrame = textFrames[i];
var newString = thisTextFrame.contents.replace(firstNamePattern, firstName);
if (newString != thisTextFrame.contents) {
thisTextFrame.contents = newString;
}
newString = thisTextFrame.contents.replace(lastNamePattern, lastName);
if (newString != thisTextFrame.contents) {
thisTextFrame.contents = newString;
}
}
}
}
function resetTemplate(firstName, lastName) {
var active_doc = app.activeDocument;
var firstNamePattern = "%name%";
var lastNamePattern = "%lastname%";
var textFrames = active_doc.textFrames;
if (textFrames.length > 0) {
for (var i = 0 ; i < textFrames.length; i++) {
var thisTextFrame = textFrames[i];
var newString = thisTextFrame.contents.replace(firstName, firstNamePattern);
if (newString != thisTextFrame.contents) {
thisTextFrame.contents = newString;
}
newString = thisTextFrame.contents.replace(lastName, lastNamePattern);
if (newString != thisTextFrame.contents) {
thisTextFrame.contents = newString;
}
}
}
}
var originalInteractionLevel = userInteractionLevel;
userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;
main();
userInteractionLevel = originalInteractionLevel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment