Skip to main content
Inspiring
July 3, 2023
Answered

Save artboards to pdf with customizable number of artboards

  • July 3, 2023
  • 1 reply
  • 986 views

Hi community, hope you are doing awesome, can I get some help with some pdf export issues I am facing?
What I have is:

try {
var doc = app.activeDocument;
var totalArtboards = doc.artboards.length;

for (var i = 0; i < totalArtboards; i++) {
doc.artboards.setActiveArtboardIndex(i);
alert("seleccionando artboard " + i);
doc.selection = doc.artboards[i].artboardRect;

alert("exportar")
var exportFile = new File(doc.path + "/Artboard_" + (i + 1) + ".pdf");
var saveOpts = new PDFSaveOptions();
doc.saveAs(exportFile, saveOpts);
alert("exportado " + i);
}

doc.artboards.setActiveArtboardIndex(0);

alert("Each artboard exported as a separate PDF file.");
} catch (e) {
alert("Error: " + e);
}

 

and what I am looking for is to let the user specify how many artboards he wants to export, if I have a 17 artbaords file and I typed number "5" as the artbaords I need to be exported, the code will create 4 files, meaning: files 1,2 and 3 will have 5 of the artboards (using a loop with the artboard index) and the 4th file will have 2 artboards, total: 17 artboards. If i have an 8 artboards document and I typed 2 as the number of artboards the code will export 4 files with 2 arborads inside each file, do I explain?

I found this, but I have no idea how to modify it, right now, Illustrator crashes every time I try my code, can I have some help please? thanks


https://community.adobe.com/t5/illustrator-discussions/pdf-and-jpg-export-script/m-p/13638487#M359158

This topic has been closed for replies.
Correct answer jduncan

Slight modification should be all you need. Try it out and let me know if you have any questions. Cheers!

/*
ExportArtboardRangeToPDF.jsx for Adobe Illustrator
--------------------------------------------------

Export a specified range of artboards as a pdf.

Created in response to this question on the Adobe forum:
https://community.adobe.com/t5/illustrator-discussions/save-artboards-to-pdf-with-customizable-number-of-artboards/m-p/14734694#M412827
*/

(function () {
  // Define the target folder for the exported pdf
  if (app.documents.length > 0)
    var exportFolder = Folder.selectDialog("Select a folder for export");

  if (exportFolder != null) {
    // Get the current document
    var doc = app.activeDocument;
    // Get the current file name
    var fileName = doc.name.split(".")[0];

    // Prompt user for artboard range
    var defaultArtboards = "1,2";
    var userInput = prompt("Artboard export range (e.g. 1,3,4-5).", defaultArtboards);

    // No need to continue if user cancelled or didn't specify any artboards
    if (userInput === null || !userInput.length) {
      alert("No artboard(s) specified.");
      return;
    }

    // Set up PDF Save Options
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    pdfOptions.preserveEditability = false;
    pdfOptions.pDFPreset = "[Smallest File Size]"; // set to a preset on your system

    // Export the pdf with the specficed artboards
    var filePath;
    pdfOptions.artboardRange = userInput;
    filePath = new File(
      exportFolder + "/" + fileName + " - Artboards " + userInput + ".pdf" // choose your own file name
    );
    doc.saveAs(filePath, pdfOptions);
  }
})();

1 reply

jduncan
Community Expert
Community Expert
July 3, 2023

The original script I wrote only required a few small changes. As you'll see, I added a default 'setSize' value of 1 which will prepopulate the input field and export all artboard separately by default. You can change that to whatever number you want.

 

Let me know if this works for you? Cheers!

/*
ExportArtboardSetsToPDF.jsx for Adobe Illustrator
-------------------------------------------------

Export a specified set size of artboards as pdf(s).

Created in response to this question on the Adobe forum:
https://community.adobe.com/t5/illustrator-discussions/save-artboards-to-pdf-with-customizable-number-of-artboards/td-p/13910550
*/

// Define the target folder for the exported files
if (app.documents.length > 0)
  var exportFolder = Folder.selectDialog("Select a folder for export");

if (exportFolder != null) {
  // Get the current document
  var doc = app.activeDocument;
  // Get the current file name
  var fileName = doc.name.split(".")[0];

  // Prompt user for artboards set size (defaults to exporting every artboard separately)
  var defaultSetSize = 1;
  var setSize = prompt("Please enter the number of artboards per set:", defaultSetSize);
  setSize = setSize === null || isNaN(setSize) ? defaultSetSize : parseInt(setSize);

  // Set up PDF Save Options
  var pdfOptions = new PDFSaveOptions();
  pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
  pdfOptions.preserveEditability = false;
  pdfOptions.pDFPreset = "[Smallest File Size]";
  iterations = Math.ceil(doc.artboards.length / setSize);

  // Iterate over the total sets and export each pdf file
  var filePath;
  for (var i = 0; i < iterations; i++) {
    startAB = i * setSize + 1;
    endAB = i < Math.floor(doc.artboards.length / setSize) ? startAB + setSize - 1 : "";
    pdfOptions.artboardRange = startAB + "-" + endAB;
    filePath = new File(exportFolder + "/Set " + (i + 1) + ".pdf");
    doc.saveAs(filePath, pdfOptions);
  }
}

 

j.khakase
Inspiring
July 12, 2024

Good script, but it export all the artboards each time, how to export only targeted artboards as single file? will be great help

jduncan
Community Expert
jduncanCommunity ExpertCorrect answer
Community Expert
July 12, 2024

Slight modification should be all you need. Try it out and let me know if you have any questions. Cheers!

/*
ExportArtboardRangeToPDF.jsx for Adobe Illustrator
--------------------------------------------------

Export a specified range of artboards as a pdf.

Created in response to this question on the Adobe forum:
https://community.adobe.com/t5/illustrator-discussions/save-artboards-to-pdf-with-customizable-number-of-artboards/m-p/14734694#M412827
*/

(function () {
  // Define the target folder for the exported pdf
  if (app.documents.length > 0)
    var exportFolder = Folder.selectDialog("Select a folder for export");

  if (exportFolder != null) {
    // Get the current document
    var doc = app.activeDocument;
    // Get the current file name
    var fileName = doc.name.split(".")[0];

    // Prompt user for artboard range
    var defaultArtboards = "1,2";
    var userInput = prompt("Artboard export range (e.g. 1,3,4-5).", defaultArtboards);

    // No need to continue if user cancelled or didn't specify any artboards
    if (userInput === null || !userInput.length) {
      alert("No artboard(s) specified.");
      return;
    }

    // Set up PDF Save Options
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    pdfOptions.preserveEditability = false;
    pdfOptions.pDFPreset = "[Smallest File Size]"; // set to a preset on your system

    // Export the pdf with the specficed artboards
    var filePath;
    pdfOptions.artboardRange = userInput;
    filePath = new File(
      exportFolder + "/" + fileName + " - Artboards " + userInput + ".pdf" // choose your own file name
    );
    doc.saveAs(filePath, pdfOptions);
  }
})();