Skip to main content
_Philip_
Inspiring
March 3, 2021
Answered

[Script] Exporting PDF and JPG with custom name from Data merge

  • March 3, 2021
  • 2 replies
  • 6530 views

Hi,

First of all - sorry for a long thread. Secondly, I'm not (at all) well versed in coding, so I tip my hat to those who have put together the scripts (linked/mentioned below) that were the starting point of this. I've been trying to combine/modify these scripts to perform the tasks I require but I can't seem to get it to work.

 

What I'm trying to do: I have a multi page document that consist of about 20 pages that range from a bunch of products; double sided business card, a couple of different posters/signs and material for online marketing. These different products contain a persons name and contact details (phone number and email) along with different types of photos of the person depending on the product.

 

Some pages need to be exported as JPG and some as PDF using different presets. 

 

My current solution: I'm batching/using a bunch of scripts in a folder. These are each dedicated to a number of pages. For example (PDF), if  page 1-2 would be a business card then I would have a minimalistic script/file called "page 1 business card.jsx":

 

with(app.pdfExportPreferences){
    pageRange = "1-2"
    }
var myPDFExportPreset = app.pdfExportPresets.item("Business card preset")
app.activeDocument.exportFile(ExportFormat.pdfType, File("/c/temp/XXX business card.pdf"),
false, myPDFExportPreset);

 

For example (JPG), if page 15 needs to be exported as a high quality JPG I would save this in the batch/export script folder as "page 15 HQ jpg.jsx": 

 

var doc = app.activeDocument;

app.jpegExportPreferences.properties = {
   antiAlias: true,
   embedColorProfile: true,
   exportResolution: 300,
   // exportingSpread: true,
   jpegColorSpace: JpegColorSpaceEnum.rgb,
   jpegExportRange: ExportRangeOrAllPages.exportRange,
   jpegQuality: JPEGOptionsQuality.maximum,
   jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
   useDocumentBleeds: false,
   simulateOverprint: false,
   pageString: "15"
}

var tempFile = File("/c/temp/XXX product.jpg");
doc.exportFile(ExportFormat.jpg, tempFile);

 

Then I would run all the scripts using this lovely script by Kasyan.

http://kasyan.ho.ua/indesign/batch_process_scripts/batch_process_scripts.html 

 

After all the scripts have been run I would have a number of files, like this: 

  • XXX business card.pdf
  • XXX Instagram 1.jpg
  • XXX Instagram 2.jpg
  • XXX Sign.pdf
  • XXX Facebook.jpg
  • XXX Poster.pdf

Then I would have to rename the XXX part manually to First name + Last name, ie "John Doe business card.pdf"

 

What I would like to do: I would like to use a script that allows me to specify the name using a paragraph style. Then I could make a non printing layer and, in this layer, create a text box in which I'd use a specific paragraph style applied to the two data merge entries "<first_name> <last name>". Then during the export, a script would find that specific paragraph style and replace the XXX.

 

My starting point is this script from: https://indesignsecrets.com/topic/export-pages-to-jpg-with-custom-filenames 

 

if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}

function myPS() {
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}

function MakeJPEGfile() {

app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high;
app.jpegExportPreferences.exportResolution = 150;
app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;

app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();

var f = myDoc.findGrep();

for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents;
app.jpegExportPreferences.pageString = curPage.name;
var myFilePath = myDoc.filePath + "/" + objName + ".jpg"; //export to a folder of the current document
var myFile = new File(myFilePath);
myDoc.exportFile(ExportFormat.jpg, myFile, false);
} catch(e) {
//pasteboard?
}

 

 

I made a bastardization of this script by excluding the last part of the JPG export and inserting/modifying the simple PDF script above. And this actually works with PDF: 

 

if (app.documents.length != 0){
var myDoc = app.activeDocument;
MakeJPEGfile();
} else {
alert("Please open a document and try again.");
}

function myPS() {   
try {
return myDoc.selection[0].appliedParagraphStyle;
} catch (e) {
alert("Place cursor to text with paragraph style for filenames");
exit();
}
}


function MakeJPEGfile() {
    
   app.jpegExportPreferences.properties = {
   antiAlias: true,
   embedColorProfile: true,
   exportResolution: 150,
   // exportingSpread: true, // Uncomment if spreads
   jpegColorSpace: JpegColorSpaceEnum.rgb,
   jpegExportRange: ExportRangeOrAllPages.exportRange,
   jpegQuality: JPEGOptionsQuality.high,
   jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
   useDocumentBleeds: false,
   simulateOverprint: false,
   pageString: "1-2"
}


app.findGrepPreferences = null;
app.findGrepPreferences.appliedParagraphStyle = myPS();

var f = myDoc.findGrep();

for (var myCounter = 0; myCounter < f.length; myCounter++) {
try {
var curPage = f[myCounter].parentTextFrames[0].parentPage;
if (curPage.appliedSection.name != "") {
curPage.appliedSection.name = "";
}
var objName = f[myCounter].contents;
app.jpegExportPreferences.pageString = curPage.name;
var myFilePath = myDoc.filePath + "/" + objName + ".jpg";
var myFile = new File(myFilePath);
//myDoc.exportFile(ExportFormat.jpg, myFile, false);

with(app.pdfExportPreferences){
    pageRange = ("1-2")
    }
var myPDFExportPreset = app.pdfExportPresets.item("business card preset");
app.activeDocument.exportFile(ExportFormat.pdfType, File(myDoc.filePath + "/" + objName + " business card.pdf"),false, myPDFExportPreset);

} catch(e) {
//pasteboard?
}
}
}

 

 

Problem: This doesn't work with JPG as the script only exports the pages where the specified "file name" paragraph style is present, in my case the first page. So, hereby kindly requesting that someone could help out in solving the matter of exporting a specified page as JPG. 

 

I guess I could modify this part (below) in each case to get it to link to a paragraph style that is specific to the page/product ie. Page "Facebook 1" gets a paragraph style called "Facebook 1". 

 

app.findGrepPreferences.appliedParagraphStyle = myPS();

 

 

Ideas? Solutions? Plz halp! Thanks in advance! 🙂

This topic has been closed for replies.
Correct answer Mike Bro

Wish I could edit the last post. 

Dunno what happened but it started working even with the part I thought was causing it not to work. 

 

Last question still stands though 🙂


Give this a try....

 

var doc = app.documents[0];

var myFilePath = File("/c/temp/"); 


for(var p = 0; p < app.documents[0].pages.length; p++) {  
         var frames = app.documents[0].pages[p].textFrames;
         var jpg_name = null; 
         
      
         for(var i = 0; i < frames.length; i++) { 
          try { 
              if(frames[i].paragraphs[0].appliedParagraphStyle.name == 'Facebook') {  
                jpg_name = frames[i].paragraphs[0].contents;  
                   break;  
              }
            }catch (e){
          }  
        }
            
        if(jpg_name != null) {  
          
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.LOW; // low medium high maximum

        app.jpegExportPreferences.exportResolution = 72;

        app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
        app.jpegExportPreferences.pageString = app.documents[0].pages[p].name;

         
         doc.exportFile(ExportFormat.jpg, File(myFilePath +"/" + jpg_name + ".jpg"), false);
        }
        if (jpg_name == null) { 
        }
      }
         alert("Done Exporting jpg's!");

    

Regards.

Mike

2 replies

Colin Flashman
Community Expert
March 11, 2021

I have a paid script that might be what you're after - Data Merge to Single Records PRO:

This script effectively merges each file one at a time to PDF; or ID and then exports to another format... but if exporting to anything other than directly to PDF, there is the ability to run a script prior to export. If you use this script, merge to ID files but use a custom script that you've written to do specific page exports, would that be of use?

If the answer wasn't in my post, perhaps it might be on my blog at colecandoo!
_Philip_
_Philip_Author
Inspiring
March 11, 2021

Hey Colin, 

 

Thanks, I already own your (awesome) script and gave you praise for it (we just spoke via e-mail in regards to the GREP script vs. bullet lists via Data merge) 🙂 

 

If you sneak a peek at the image I posted in my reply to Mike: I enjoy using your script for the different parts that the document consists. Say I need to produce business cards for all 190+ posts I would just use the 20+ page template, erase all the pages that weren't pertinent to the business card and then save it as a separate document/run your script to produce 190+ business cards that would end up getting proper file names like: John Smith Business card.pdf

I first started using your script when I had to produce christmas cards for all employees. Much obliged 🙂 

 

However, when a new co-worker starts, they need the full setup - so now I can set up my paragraph styles/ text boxes and just run that script to get:

/John Smith/business card/John Smith Business card.pdf

/John Smith/Social/John Smith Instagram_1080x1080px.jpg

/John Smith/Social/John Smith Facebook_1920x1080px.jpg

/John Smith/Ads/John Smith Letterbox ad.pdf

/John Smith/Ads/John Smith Poster.pdf

 

New Participant
August 11, 2021

hi guys,

 

I am a bit lost, but I guess this is a similar case. Hopefully someone can solve my problem.

 

I have a document with several pages. I want to export every single page as a JPG. So far no problems.

But I want the filename to be individual for every JPG. I use one graphic on every page on a separat layer and I would love to have the name of the linked graphic be part of the filename.

 

For example:

graphic motive_01.jpg on Page 1

graphic motive_02.jpg on Page 2

graphic motive_01.jpg on Page 3

graphic motive_04.jpg on Page 4

...

 

the exported files should look like "INDD file name + motive_01_PageNumber.jpg"

 

Is that possible?

Thanks

_Philip_
_Philip_Author
Inspiring
March 3, 2021

Quick follow up: After some more time on Google I found a post leading back to a older/dead part of this forum, but Waybackmachine solved it: 

https://web.archive.org/web/20141202192138/https://forums.adobe.com/thread/1339758 

 

I thought I could use this to create a script that pointed to a specific paragraph style and then only exported the page on which this style was used. I did a test run and used a style called "Facebook" (applied to <first name> <last name>) only on page 3 (of 3) and tried exporting. 

I ended up with: 

  • null1 Facebook.jpg
  • null2 Facebook.jpg
  • John Doe3 Facebook.jpg

 

Not quite what I had in mind...

if (app.documents.length != 0) {  
     var myDoc = app.activeDocument;  
    MakeJPEGfile();  
}  
else{    
     alert("Please open a document and try again.");    
}   
  
  
function MakeJPEGfile() {   
  

  
     for(var myCounter = 0; myCounter < myDoc.pages.length; myCounter++) {  
  
          if (myDoc.pages.item(myCounter).appliedSection.name != "") {  
               myDoc.pages.item(myCounter).appliedSection.name = "";  
          }  
              
            var myPageName = myDoc.pages.item(myCounter).name;  
          var myJpegPrefix = "";  
          var isStyleExist = true;  
  
          //Checking the existing of the paragraph style (filename)  
          try {  
              myDoc.paragraphStyles.item("Facebook");  
          }  
          catch (myError) {  
              isStyleExist = false;  
          }  
  
          if (isStyleExist)  
  
          myJpegPrefix = getParagraphContent(myDoc.paragraphStyles.item("Facebook"), myDoc.pages.item(myCounter)) + myPageName;  
          app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.high; // low medium high maximum  
          app.jpegExportPreferences.exportResolution = 72;  
          app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.exportRange;  
          app.jpegExportPreferences.pageString = myPageName;  
  
  
          var myFilePath = File("/c/temp/" + myJpegPrefix + " Facebook.jpg");  
          var myFile = new File(myFilePath);  
          myDoc.exportFile(ExportFormat.jpg, myFile, false);  
     }  
}  
  
  
// The new function, but needs to add checking the file name rules.    
  
function getParagraphContent (paragraphStyle, currentPage)  
{  
    var paragraphContent = null;  
    for (var c = 0; c < currentPage.textFrames.length; c++)  
    {  
        for (var i = 0; i < currentPage.textFrames.item(c).paragraphs.length; i++)  
        {  
            if (currentPage.textFrames.item(c).paragraphs.item(i).appliedParagraphStyle == paragraphStyle)  
            {  
                paragraphContent = currentPage.textFrames.item(c).paragraphs.item(i).contents;  
   // Remove spaces and returns at the end:  
                paragraphContent = paragraphContent.replace(/\s+$/, '');  
   // Replace remaining spaces with hyphen:  
               // paragraphContent = paragraphContent.replace(/\s+/g, '-');  
   // Make lowercase:  
              //  paragraphContent = paragraphContent.toLowerCase();  
                return paragraphContent;  
            }  
        }  
    }  
    return paragraphContent;  
}  

 

Brainiac
March 3, 2021

Hello Philip!

 

 

Here's my take on what I think you're looking to do.......

I just made a couple of modifications to an existing script I wrote.

 

var doc = app.documents[0];

var mySelectedFolder = Folder.selectDialog ("Select a Folder");
 if(mySelectedFolder != null){

}else{
exit();
}

for(var p = 0; p < app.documents[0].pages.length; p++) {  
         var frames = app.documents[0].pages[p].textFrames;
         var myPageName = app.documents[0].pages[p].name;  
         var jpg_name = null; 
         
      
         for(var i = 0; i < frames.length; i++) { 
          try { 
              if(frames[i].paragraphs[0].appliedParagraphStyle.name == 'Facebook') {  
                jpg_name = frames[i].paragraphs[0].contents;  
                   break;  
              }
            }catch (e){
          }  
        }
            
        if(jpg_name != null) {  
          
        app.jpegExportPreferences.jpegQuality = JPEGOptionsQuality.LOW; // low medium high maximum

        app.jpegExportPreferences.exportResolution = 72;

        app.jpegExportPreferences.jpegExportRange = ExportRangeOrAllPages.EXPORT_RANGE;
        app.jpegExportPreferences.pageString = app.documents[0].pages[p].name;

         
         doc.exportFile(ExportFormat.jpg, File(mySelectedFolder +"/"+ myPageName +'_' + jpg_name + ".jpg"), false);
        }
        if (jpg_name == null) { 
        }
      }
         alert("Done Exporting jpg's!");

    

 

 

Regards,

Mike

_Philip_
_Philip_Author
Inspiring
March 3, 2021

Hi Mike, 

 

Thanks for your reply! 

 

I tried your script but it doesn't export anything on my end. At first I thought the problem was that Facebook was in single quotation marks: 'Facebook' - so I changed it to "Facebook" but that didn't work either. The script runs without fault but doesn't seem to output. 

 

Is it possible to modify the script to skip the step where one chooses export folder? I just need to point the file to a fixed location for instance "/c/temp/".