Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* This function exports the Addendums as PDFs by merging the data from the .txt file in this directory
* with the fields in the PDF.
*
* It's wrapped in a function so it can return a NULL if there's an error.
*
* @author Ethan Beyer
* @version 1.0
*/
!(app.trustedFunction(
function(){
app.beginPriv();
// Init all Vars
var projBaseDir = "/Users/abc/Desktop/";
var templatePDF = projBaseDir + "Addendum.pdf";
var dataFile = projBaseDir + "Addendum Data.txt";
var outputSaveDir = projBaseDir + "_OUTPUT/";
var flattenedDocsDir = projBaseDir + "_FLATTENED/";
// Create the Date
var today = new Date();
var dd = today.getDate();
var mm = today.getMonth() + 1; // January is 0!
var yyyy = today.getFullYear();
if (dd < 10) { dd = '0' + dd; }
if (mm < 10) { mm = '0' + mm; }
today = String(yyyy) + String(mm) + String(dd);
// Loop through all rows in selected Text file and save each as their own file.
var i = 0;
do {
// Open the document
var $this = app.openDoc(templatePDF);
var nRtn = $this.importTextData(dataFile, i);
var id = $this.getField("Contract-Sub ID").value;
var comp = $this.getField("Company Name").value;
var outputDocName = id+" - "+comp+" "+today;
// Just in case the name has a slash in it, this prevents the script from breaking.
comp = comp.replace("/", "-");
if(nRtn !== 0) {
console.println("\n");
console.println("---------------------------skipped");
console.println("Index: "+i);
console.println("Return Code: "+nRtn);
console.println("ID: "+id);
console.println("Company Name: "+comp);
} else {
// Save the file
$this.saveAs({
cPath: outputSaveDir + outputDocName + ".pdf",
bPromptToOverwrite: true
});
// Open the file, flatten it, save it, close it.
var flattenedDoc = app.openDoc({
cPath: outputSaveDir + outputDocName + ".pdf",
bHidden: true,
});
flattenedDoc.flattenPages();
flattenedDoc.saveAs({
cPath: flattenedDocsDir + flattenedDoc.documentFileName + " [FLATTENED].pdf",
bPromptToOverwrite: true
});
// Close all open docs every iteration
var d = app.activeDocs;
for(var openDocCounter in d) {
d[openDocCounter].closeDoc(true);
}
}
i++;
} while(nRtn <= 0);
app.endPriv();
return;
}
))();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.