0

I created a script that batch exports PDF's into JPEG images. The images must be 900px x 900px and the artwork on the PDF must be either 800px wide or tall, depending on its proportions. Sometimes the artwork has a white background, so the script creates a black frame arund the placed artwork so that its proportions are visible.

So far I've been able to succesfully create a script for this purpose, no matter the size of the artwork it places and resizes it into the set limits and exports it into a JPG file with the desired dimensions. It also creates the black frame perfectly.

Figs A to D show what this current script is capable to do:

enter image description here

Now to the tricky part...

Half the time I need to export PDF's that have more than one page, usually they're multiple copies of the same artwork, and it's meant to be showed in groups. I need to find a way of automatically place every page of the PDF into the bounding box of the JPG (figs E-H).

enter image description here

Several times also I receive files that contain numerous pages but in different sizes (figs I-J):

enter image description here

I know there's a way of looping through the PDF pages just like the script loops through the Multiple files, but I have no idea of how to create a bounding box and make the script calculate the distances and scale factors based on each placed page. If, for example, the amount of pages to place is to great for them to be legible once they're in the bounding box, it would be ideal to create another JPG file with the remaining pages.

It would be a huge help if someone could help me figuring out a way of accomplishing this. The code I provide already works for the first use case, so It's great if someone finds it useful.

Thanks a lot

This is the code as it currently is:

#target illustrator

// Define the target dimensions
var targetSize = 900;

// Select the PDF files
var inputFiles = File.openDialog("Select PDF files to process", "*.pdf", true);

// Loop through the selected files
for (var i = 0; i < inputFiles.length; i++) {
  var inputFile = inputFiles[i];
  
  // Open the PDF file
  var doc = app.open(inputFile);
  
  // Create a new document with the target size
  var newDoc = app.documents.add(DocumentColorSpace.RGB, targetSize, targetSize);
  
  // Get the first artboard of the new document
  var artboard = newDoc.artboards[0];
  
  // Get the active layer of the new document
  var layer = newDoc.activeLayer;
  
  // Place the artwork from the PDF file into the new document
  var placedItem = layer.placedItems.add();
  placedItem.file = inputFile;
  
  // Get the dimensions of the artwork
  var placedItemBounds = placedItem.visibleBounds;
  var placedItemWidth = placedItemBounds[2] - placedItemBounds[0];
  var placedItemHeight = placedItemBounds[1] - placedItemBounds[3];
  
  // Calculate the scale factor based on the proportions
  var scaleFactor = (placedItemWidth > placedItemHeight) ? targetSize / placedItemWidth : targetSize / placedItemHeight;
  
  // Resize the artwork
  placedItem.resize(scaleFactor * 90, scaleFactor * 90);
  
  // Calculate the position offset to center the artwork
  var xOffset = (targetSize - placedItemWidth*scaleFactor*0.9) / 2;
  var yOffset = ((targetSize - placedItemHeight*scaleFactor*0.9) / 2)+placedItemHeight*scaleFactor*0.9;
  
  // Set the position of the placed item
  placedItem.position = [xOffset, yOffset];

  
  // Create a rectangle frame
  var frame = layer.pathItems.rectangle(placedItem.top, placedItem.left, placedItemWidth*scaleFactor*0.9, placedItemHeight*scaleFactor*0.9);
  frame.stroked = true;
  frame.strokeColor = new RGBColor();
  frame.strokeColor.red = 0;
  frame.strokeColor.green = 0;
  frame.strokeColor.blue = 0;
  frame.strokeWidth = 1;
  frame.fillColor = new NoColor();
  
  
  // Export the new document as JPG
  var exportOptions = new ExportOptionsJPEG();
  exportOptions.antiAliasing = true;
  exportOptions.qualitySetting = 100;
  exportOptions.horizontalScale = 100;
  exportOptions.verticalScale = 100;
  exportOptions.artBoardClipping = true;
  
  var exportFile = new File(inputFile.path + "/" + inputFile.name.replace(".pdf", ".jpg"));
  newDoc.exportFile(exportFile, ExportType.JPEG, exportOptions);
  
  // Close the new document without saving
  newDoc.close(SaveOptions.DONOTSAVECHANGES);
  
  // Close the original PDF file
  doc.close(SaveOptions.DONOTSAVECHANGES);
}

// Display a message when processing is complete
//alert("Batch export completed!");
SomethingDark
  • 13,229
  • 5
  • 50
  • 55

0 Answers0