Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

script to export a PDF from a data merge

Community Beginner ,
Feb 19, 2025 Feb 19, 2025

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

TOPICS
Import and export , Scripting
444
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 19, 2025 Feb 19, 2025

Can you show your script?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 19, 2025 Feb 19, 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.");

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2025 Feb 19, 2025

@samir_4316 

 

What error do you get?

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 19, 2025 Feb 19, 2025

Pretty sure this line: 

            var mergedDoc = dataMerge.mergeRecords()

 

Needs to be: 

dataMerge.dataMerge.mergeRecords()

 

Chatgpt ain't too bright sometimes

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 20, 2025 Feb 20, 2025

I believe DataMergeExportFormat is not a valid object in InDesign scripting. I've tried multiple approaches, including AI, but none have worked. It seems that InDesign scripting doesn't support exporting Data Merge directly in the way I expected.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 20, 2025 Feb 20, 2025
quote

I believe DataMergeExportFormat is not a valid object in InDesign scripting. I've tried multiple approaches, including AI, but none have worked. It seems that InDesign scripting doesn't support exporting Data Merge directly in the way I expected.


By @samir_4316

 

What way do you expect? 

 

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#DataMerge.html#d1e418772__d1e418948 

 

"AI" lacks experience... 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 20, 2025 Feb 20, 2025
LATEST

Did you try my solution?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 19, 2025 Feb 19, 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...

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines