Sean Larkin
Sean Larkin

Reputation: 35

Script for InDesign That Can Export Each Spread of a Document As 2-Page PDF's, AND Name The PDF's Based on Specific Table Cell

Before I explain what I need to happen, I think it'd be helpful to explain the document I'll be working on:

With InDesign, using the data merge functionality, I will create a document where each spread will be the front and back page of a product spec sheet (letter size, 2-sided).

What I'm hoping a script can do, is to export every spread as it's own PDF, and name the PDFs based on the model number of the product shown on each spread.

So for example, let's say the data merge was 25 records. That'd be 25 spreads (50 pages), starting on page 2. The export would create a PDF of pages 2-3, a PDF of 4-5, of 6-7, and so on. The export process would also extract the model number which would be consistently located in a table on left page, and use the model number to name the PDF it's creating.

It seemed more doable when it was just in my head. Now that I've typed it out I'm not as optimistic. But I'm also a scripting simpleton so hopefully any skilled folks can chime in on the viability of the idea.

Thanks!

*edit to say that I used 25 records just as an example. The project will be for hundreds of spec sheets hence the interest to data merge and automate what I can.

So far I've just worked on verifying I could use InDesign's data merge to create spreads and not just single pages.

I did look at a script I got help on here that while was a very different process, it did include a step that found cell contents based on the text in the title cell. That's what made me think it'd be possible to extract the model number and use it to name each file.

Screenshot of spread showing the data merge tags

Screenshot of spread showing the data merge tags

Screenshot showing simple table idea

Screenshot showing simple table idea

Screenshot showing table idea as referenced in my last comment

Screenshot showing table idea as referenced in my last comment

Upvotes: 1

Views: 264

Answers (2)

Yuri Khristich
Yuri Khristich

Reputation: 14537

Here is the variant of the script which takes the names of the PDF files from first cell of second row of first table on the hidden layer with name 'Hidden Text Test':

var doc = app.activeDocument;
var pdf_preset = app.pdfExportPresets[0]; // High Quality Print
var folder = doc.filePath // save into the same folder as the active document
var hidden_layer_name = 'Hidden Text Test';

var table, layer_name, pdf_file_name, page_num;

var tables = doc.stories.everyItem().tables.everyItem().getElements();

for (var i=0; i<tables.length; i++) {
    table = tables[i];
    layer_name = table.parent.itemLayer.name;
    if (layer_name != hidden_layer_name) continue;

    page_num = parseInt(table.parent.parentPage.name);
    pdf_file_name = table.rows[1].cells[0].contents;
    export_spread(page_num, folder, pdf_file_name);
}

function export_spread(page, folder, name) {
    var pdf_file = File(folder + '/' + name + '.pdf');
    app.pdfExportPreferences.pageRange = page + '-' + (page+1);
    doc.exportFile(ExportFormat.pdfType, pdf_file, false, pdf_preset);
}

Upvotes: 1

Yuri Khristich
Yuri Khristich

Reputation: 14537

A simplest solution is here:

if (app.selection.length == 0) {
    alert('Select a text frame with a list of names');
    exit();
}

var frame = app.selection[0];

if (!(frame instanceof TextFrame || frame instanceof InsertionPoint)) {
    alert('Select a text frame with a list of names');
    exit();
}

if (frame instanceof InsertionPoint) frame = frame.parent.textContainers[0];

var doc = app.activeDocument;
var names = frame.contents.split('\r');
var pdf_preset = app.pdfExportPresets[0]; // High Quality Print

var start_page = 2; // start from a second page
var pdf_folder = doc.filePath // save to the same folder as the active document

for (var i=0; i<names.length; i++) {
    var pdf_file = File(pdf_folder + '/' + names[i] + '.pdf');
    var page = start_page + i*2;
    app.pdfExportPreferences.pageRange = page + '-' + (page+1);
    doc.exportFile(ExportFormat.pdfType, pdf_file, false, pdf_preset);
}

Script takes the list of names from selected frame (you can copy/paste manually it from your csv/xlsx date file) and exports the spreads into PDFs with names from the list.

InDesign layout with several pages and one text frame with a list of the names

The result:

Folder with icons of several saved PDF files and one InDesign file

One of the PDF files opened in Adobe Acrobat

If you need to change a PDF-export profile or something else, let me know.

Upvotes: 1

Related Questions