Skip to main content
Participant
August 6, 2024
Answered

Export independently PDF with script

  • August 6, 2024
  • 3 replies
  • 355 views

Hi, I'm trying to export my pages base on a text box of my file: its a document of multiple cerifications so every page has the name of each person that participated. I'm trying to automaticize the process of exportation to have a pdf of every page with the name of the person based on the text box that has it written. I tagged each box with "nombreDestinatario" but it doesn't work. Also, I don't know if the ExportPreset it's ok. 

I appreciate if you can help me 🙂

(I don't want to keep writting on each file the corresponded name hahah there are 40 pages)

var myDocument = app.activeDocument;
var myPages = myDocument.pages;
var myPage, myTextFrame, myFileName;

for (var i = 0; i < myPages.length; i++) {
    myPage = myPages[i];
    myTextFrame = null;
    

    for (var j = 0; j < myPage.textFrames.length; j++) {
        if (myPage.textFrames[j].label == "nombreDestinatario") {
            myTextFrame = myPage.textFrames[j];
            break;
        }
    }
    
    if (myTextFrame != null) {
        myFileName = myTextFrame.contents + ".pdf";
        var myFile = new File("/Users/andrea/Desktop/alumnos/" + myFileName); 
        var myPDFExportPreset = app.pdfExportPresets.item("[PDF/X-3:2002]"); 
        myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset, myPage);
    } else {
        alert("No se encontró la caja de texto 'nombreDestinatario' en la página " + (i+1));
    }
}

 

This topic has been closed for replies.
Correct answer Peter Kahrel

In addition to what my colleagues mentioned, three additional points:

1. It's more efficient first to check whether there are any pages with missing frames, and if there are, show them all and quit.

2. No need to bind the preset to a variable at every iteration of the loop: do it once (and check whether it exists).

3. You can access a labelled text frame directly, no need for a loop -- if you labelled them on the Layers panel.

4. To obtain the contents of a frame, address its parent story to avoid problems with overset text.

 

That's rather a lot, so I recast your script:

 

var myDocument = app.activeDocument;
// Use an array, much faster than a collection
var myPages = myDocument.pages.everyItem().getElements();
var myPage, myFile;
var missing = [];

//Check whether the preset is present

var myPDFExportPreset = app.pdfExportPresets.item("[PDF/X-3:2002]");
if (!myPDFExportPreset.isValid) {
  alert ("Preset not found");
  exit();
}

// Then check whether there are any pages without the frame

for (var i = 0; i < myPages.length; i++) {
  if (!myPages[i].textFrames.item("nombreDestinatario").isValid) {
    missing.push (myPages[i].name);
  }
}

// Any missing frames? Show them and quit

if (missing.length > 0) {
  alert("No se encontró la caja de texto 'nombreDestinatario' en las páginas " + missing.join(', '));
  exit();
}


for (var i = 0; i < myPages.length; i++) {
  myPage = myPages[i];
  // Use the parentStory to avoid problems with overset
  myFileName = myPage.textFrames.item("nombreDestinatario").parentStory.contents;
  // ~/Desktop/ is a shortcut for the current user's desktop
  myFile = File("~/Desktop/alumnos/" + myFileName + ".pdf");
  app.pdfExportPreferences.pageRange = myPage.name;
  myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
}

 

3 replies

Peter Kahrel
Community Expert
Peter KahrelCommunity ExpertCorrect answer
Community Expert
August 7, 2024

In addition to what my colleagues mentioned, three additional points:

1. It's more efficient first to check whether there are any pages with missing frames, and if there are, show them all and quit.

2. No need to bind the preset to a variable at every iteration of the loop: do it once (and check whether it exists).

3. You can access a labelled text frame directly, no need for a loop -- if you labelled them on the Layers panel.

4. To obtain the contents of a frame, address its parent story to avoid problems with overset text.

 

That's rather a lot, so I recast your script:

 

var myDocument = app.activeDocument;
// Use an array, much faster than a collection
var myPages = myDocument.pages.everyItem().getElements();
var myPage, myFile;
var missing = [];

//Check whether the preset is present

var myPDFExportPreset = app.pdfExportPresets.item("[PDF/X-3:2002]");
if (!myPDFExportPreset.isValid) {
  alert ("Preset not found");
  exit();
}

// Then check whether there are any pages without the frame

for (var i = 0; i < myPages.length; i++) {
  if (!myPages[i].textFrames.item("nombreDestinatario").isValid) {
    missing.push (myPages[i].name);
  }
}

// Any missing frames? Show them and quit

if (missing.length > 0) {
  alert("No se encontró la caja de texto 'nombreDestinatario' en las páginas " + missing.join(', '));
  exit();
}


for (var i = 0; i < myPages.length; i++) {
  myPage = myPages[i];
  // Use the parentStory to avoid problems with overset
  myFileName = myPage.textFrames.item("nombreDestinatario").parentStory.contents;
  // ~/Desktop/ is a shortcut for the current user's desktop
  myFile = File("~/Desktop/alumnos/" + myFileName + ".pdf");
  app.pdfExportPreferences.pageRange = myPage.name;
  myDocument.exportFile(ExportFormat.pdfType, myFile, false, myPDFExportPreset);
}

 

brian_p_dts
Community Expert
Community Expert
August 7, 2024

You need to set the page export settings and range: 

app.pdfExportPreferences.pageRange = myPage.name;
Robert at ID-Tasker
Legend
August 6, 2024

What is the error? 

 

The label for the TextFrame - in the Script Label panel - should be without enter at the end - just one line.

 

So you need to select TextFrame, type "nombreDestinatario" - or paste - then unselect TextFrame - click somewhere else. Then click the same TextFrame to check the label.