Skip to main content
Participating Frequently
June 12, 2012
Question

Data Merge in newest version of InDesign

  • June 12, 2012
  • 6 replies
  • 21442 views

Hi all,

Was wondering if anyone can point me to the improvments to Data Merge in the newest version of InDesign (assuming there are some). We are specifically looking for a way to output single PDFs (500 page Data Merged InDesign Document saves as 500 PDF files for example) and each PDF being named using a column from the attached Excel file. I realise that it will be possible to have the javascripted, but was kind of hoping to avoid this with the newest InDesgn.

Thanks,

Graham

    This topic has been closed for replies.

    6 replies

    Nick Goodenough
    Participant
    February 11, 2019

    Years later… but for anyone using the revised script I have a fix if you're getting the error:

    RaiseError: The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

    Doc.extractPages:83:Console undefined:Exec

    ===> The file may be read-only, or another user may have it open. Please save the document with a different name or in a different folder.

    The third prompt of the script asks for the relative location of a folder to save the pages in, it defaults to 'pdf/' and if you don't have said 'pdf' folder already created the scripts not smart enough to create it itself, and instead throws the error above.

    The fix is to make sure you have a folder created that matches your answer to the third prompt -OR- leave the third prompt blank and all the files will be saved in place and not in a separate folder.

    Known Participant
    July 23, 2014

    Ahhhhh well done!! I shall try this and let you know how it works out. Thanks for all your hard work. I completely agree, this really could be implemented directly within inDesign but till will attempt this and thank you for your efforts.

    All my client names are already in excel too so similar structure I think. Here's hoping.

    Known Participant
    July 22, 2014

    Hi Graham / everyone else

    Did you ever figure out how to do this? I need to do a similar (or the same) task. I have a 12pp document and 100 records, I want to output 100 x 12pp PDF files, named from a column in the attached CSV.

    Any ideas?

    Participating Frequently
    July 22, 2014

    Hello Ruth!

    I attempted it again today with a clear mind hoping that it was just something that I was missing or doing wrong. Unfortunately, I still was not able to successfully execute the process. I'm not sure if it's the JavaScript or the CSV file. If I had to guess, I would say that based on the complexity of the JavaScript, it's not counting the lines in the database properly. I've done everything possible to - and with the CSV file and end up with the same "The number of pages per row is not an integer…" error. I have a 597 page PDF that I need to split into 199, 3-page PDFs and rename. I really would hate to do this manually, but I have spent so much time trying to get the script to work that I could have . I cannot afford to purchase any software that might be available to accomplish this same thing. Should you manage to find a usable solution, please do share. I know that I would greatly appreciate it!

    Best of luck!

    Known Participant
    July 22, 2014

    I can't even get my head around the script

    In Acrobat Pro you can automatically split the document into even sized "parts" - but you still need to manually rename which I'm hoping to avoid.

    Pages > Split Document

    If that helps.

    Participating Frequently
    July 21, 2014

    I am running into the same situation as well. I have tried a myriad of solutions, but nothing seems to work

    I don't think that I am missing something that's fundamentally simple. I've gone through the process a couple of dozen times without success.

    I may have to split my PDF and rename manually - bummer.

    Participating Frequently
    July 21, 2014

    I'm going to purchase this Advanced Software for Splitting and Merging PDF Documents because I couldn't get the script to work.

    Participating Frequently
    August 9, 2012

    If you're getting 500 separate PDF files, you're doing something wrong. You should be able to save your Excel file (with graphic file references) in tab-delimited TXT format, attach it to your InDesign template through Merge function, complete your layout by dragging fields, then generate your catalog. Check the Indesign Help function. Columns should be your Field names.

    Peter Spier
    Community Expert
    Community Expert
    August 9, 2012

    Cosmo,

    Did you read the first post? The OP isn't getting separate PDFs, he WANTS separate PDFs and was hoping something had changed to allow it.

    Inspiring
    July 30, 2013

    Finally on cs6 and have retested GrahamHe's script. Still has the same error in InDesign, but that is because this is NOT and indesign script - it's an ACROBAT script! The script is applied using the action wizard.

    So this doesn't behave the way I thought, such as running the script directly from indesign. The file is still merged to a single multi-page PDF file, and then the script is run via the action wizard.

    Had trouble getting the script to work initially via acrobat but I did amend the second line to read


    var _data = data.split('\r');

    and it worked a treat!

    One warning: the names in the column selected to become the filenames need to be unique (such as a primary key) otherwise there is a risk of files overwriting each other.

    colly


    Here's an improved version of that script:

    • It works for multiple pages per record rather than just one page per record (and it figures out how many, e.g. 500 pages and 100 records = 5 pages each). The only limitation is, each record must have the same number of pages.
    • It works for UTF-8 csv files, so it doesn't just skip or screw up accented characters or other non-English characters. If you have problems, make sure you're saving UTF-8 csvs - these are the default for most things except Excel, but quite tricky in Excel, see some suggestions here http://stackoverflow.com/questions/4221176/excel-to-csv-with-utf8-encoding
    • It allows you to specify text to go before and after each name, and a sub folder to put the PDFs in
    • It can cope with csvs that have commas and quotes in the cells, using CSV parsing code from here http://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data (this should work fine but CSVs are quite variable - if you have problems, try exporting the CSV from a different program, e.g. if it's coming from Excel, try exporting it from Google Docs or OpenOffice)

    If you have trouble running it in Acrobat's Actions panel, try running it in the Javascript console instead. Hit ctrl-J or cmd-J, then enable the console. copy and paste the code in, select the code you just copied and pasted, and hit ENTER (not return! on Mac) to run the code.

    In my testing, (700kb PDFs, 600 or so records, 6gb RAM Mac Pro) it's pretty darn fast. Everything else I'd tried takes hours and hours or dies mid way, this takes less than a minute. Reading the CSV is probably the slowest part, then it spits out PDFs at a rate of several a second. It seems to take maybe 5% as long as the data merge takes.

    Here's the improved code:

    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.toLowerCase() === col) {
                            return _data[row];
                        }
                    }
                }
            }
        };
    };
    
    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).";
    }
    
    
    
    Steve Werner
    Community Expert
    Community Expert
    June 12, 2012

    There are no changes in Data Merge in InDesign CS6.

    GrahamHeAuthor
    Participating Frequently
    June 12, 2012

    Thanks Steve.