The workflow this script is set up for is the Data Merge >> Export PDF, which as you noted spits out a single, massive PDF.
The script runs in Acrobat. It takes the CSV file as an input (said CSV must have a column named "filename" containing the individual name for the extracted PDF. It counts the total number of pages in the PDF and divides by the total number of records in the CSV in order to calculate the number of pages per individual PDF, then iterates through the document, extracting pages as it goes.
There is user input for text to prefix and/or suffix the output file names.
I've not tested this, but it looks straightforward (other than the parsing of the CSV, which came from someone on stackoverflow and I didn't look over in detail).
For the record, here is the script:
var CSV = function (data, delimiter) {
var _data = CSVToArray(data, delimiter);
var _head = _data.shift();
return {
length: function () {return _data.length;},
adjustedLength: function () {return _data.length - 1;},
getRow: function (row) {return _data[row];},
getRowAndColumn: function (row, col) {
if (typeof col !== "string") {
return _data[row][col];
} else {
col = col.toLowerCase();
for (var i in _head) {
if (_head[i].toLowerCase() === col) {
return _data[row][i];
}
}
}
}
};
};
function CSVToArray( strData, strDelimiter ){
strDelimiter = (strDelimiter || ",");
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
var arrData = [[]];
var arrMatches = null;
while (arrMatches = objPattern.exec( strData )){
var strMatchedDelimiter = arrMatches[ 1 ];
if (
strMatchedDelimiter.length &&
(strMatchedDelimiter != strDelimiter)
){
arrData.push( [] );
}
if (arrMatches[ 2 ]){
var strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
var strMatchedValue = arrMatches[ 3 ];
}
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
return( arrData );
}
function isInt(n) {
return typeof n === "number" && n % 1 == 0;
}
var prepend = app.response("Enter any text to go at the START of each filename:");
var append = app.response("Enter any text to go at the END of each filename:");
var pathStr = app.response("If the PDFs should be saved in a sub folder, enter the relative path here:", "", "pdf/");
this.importDataObject("CSV Data");
var dataObject = this.getDataObjectContents("CSV Data");
var csvData = new CSV(util.stringFromStream(dataObject, 'utf-8'), ',');
var pagesPerRecord = this.numPages / csvData.length();
if (isInt(pagesPerRecord)) {
for (var i = 0; i < this.numPages; i ++) {
var pageStart = i*pagesPerRecord;
var pageEnd = (i+1)*pagesPerRecord - 1;
var recordIndex = (i + pagesPerRecord) / pagesPerRecord;
var filename = csvData.getRowAndColumn(i, "filename");
if (!filename) {
app.alert('No filenames found - using "file-XX.pdf". Press Escape after continuing to cancel.');
filename = "file-" + i;
}
var settings = {nStart: pageStart, nEnd: pageEnd, cPath: pathStr+prepend+filename+append+'.pdf'};
this.extractPages(settings);
}
} else {
var message = "The number of pages per row is not an integer (" + pagesPerRecord;
message += ", " + this.numPages + " pages, " + csvData.length() + " rows).";
}