Skip to main content
Participant
February 19, 2025
Question

script to export a PDF from a data merge

  • February 19, 2025
  • 2 replies
  • 367 views

"I am attempting to create a script to export a PDF from a data merge, but nothing is working.

2 replies

Robert at ID-Tasker
Legend
February 20, 2025
quote

"I am attempting to create a script to export a PDF from a data merge, but nothing is working.


By @samir_4316

 

Not sure how we can help you - without seeing your code...

 

brian_p_dts
Community Expert
Community Expert
February 20, 2025

Can you show your script?

Participant
February 20, 2025

// InDesign Script to Merge Data and Export as PDF

var savePath = "C:/Users/Samir/Desktop/Sam/Merged_Output.pdf";

 

// Check if a document is open

if (app.documents.length > 0) {

    var doc = app.activeDocument;

 

    // Check if Data Merge is set up

    if (doc.dataMergeProperties && doc.dataMergeProperties.dataMergeFields.length > 0) {

        var dataMerge = doc.dataMergeProperties;

 

        try {

            // Set Data Merge to merge ALL records into a new document

            dataMerge.dataMergePreferences.recordSelection = RecordSelection.ALL_RECORDS;

 

            // Merge all records into a new document

            var mergedDoc = dataMerge.mergeRecords();

 

            // Check if the merge was successful

            if (mergedDoc) {

                // Activate the merged document before exporting

                app.activeDocument = mergedDoc;

 

                // Try to get the PDF export preset

                var pdfPresetName = "[High Quality Print]"; // Change if needed

                var pdfPreset = null;

 

                if (app.pdfExportPresets.itemByName(pdfPresetName).isValid) {

                    pdfPreset = app.pdfExportPresets.itemByName(pdfPresetName);

                } else {

                    alert("Warning: PDF preset '" + pdfPresetName + "' not found. Using default settings.");

                }

 

                // Export the merged document as PDF

                mergedDoc.exportFile(ExportFormat.PDF_TYPE, new File(savePath), false, pdfPreset);

 

                // Close the merged document without saving

                mergedDoc.close(SaveOptions.NO);

 

                alert("PDF successfully exported to:\n" + savePath);

            } else {

                alert("Error: Merged document was not created.");

            }

        } catch (e) {

            alert("Error during Data Merge or PDF Export:\n" + e.message);

        }

    } else {

        alert("No Data Merge fields found. Please ensure a Data Merge file is linked.");

    }

} else {

    alert("No document open. Please open a document and try again.");

}

Robert at ID-Tasker
Legend
February 20, 2025

@samir_4316 

 

What error do you get?