Skip to main content
Inspiring
March 24, 2025
Answered

• HR & LR PDF via scripting (both same weight, why?)

  • March 24, 2025
  • 1 reply
  • 182 views

Hello  -

 

I'm try to make a Illustrator Javascript that save one time my .ai file as "High Resolution" PDF and another time as "Low Resolution" PDF.

Two PDFs are created/exported, but they both have more or less the same weight.

I'm sure that my two PDF presets (.jobOptions) are correct and available…

 

Any help/clue would be highly appreciate, thx… Enjoy your say…

 

/***

    Export LOW-Resolution PDF
    
***/
function exportLowResPDF(packageFolder_DC) {
    try {
        var myDoc_DC = app.activeDocument;
        var lowResPresetName_DC = "PDF_Small2"; // Without .joboptions       // "[Smallest File Size]"
        var pdfFolder_DC = new Folder(packageFolder_DC + "/_PDF_LOW_RESOLUTION_FOR_CONTROL_ONLY");

        var docName_DC = myDoc_DC.name.replace(/\.pdf$/i, ""); // Remove .pdf extension
        var pdfFileName_DC = docName_DC.replace("_HR", "_LR"); // Replace _HR with _LR
        var pdfFile_DC = new File(pdfFolder_DC + "/" + pdfFileName_DC + ".pdf");

        // Check if the preset exists by its exact name
        var pdfPresetExists = checkPresetExists(lowResPresetName_DC);

        if (!pdfPresetExists) {
            alert("Error: The PDF preset '" + lowResPresetName_DC + "' is not available in Illustrator. Please ensure the preset is installed correctly.");
            return;
        }

        // Check for layers related to "White Ink" and hide them if they exist
        var whiteInkLayers = ["WhiteInk", "white ink", "White_Ink", "white_ink", "Printed White", "Printed_White", "printed white"];

        var hiddenWhiteInkLayers_DC = []; // Store the hidden layers to unhide them later
        for (var i = 0; i < whiteInkLayers.length; i++) {
            try {
                var whiteInkLayer_DC = myDoc_DC.layers.getByName(whiteInkLayers[i]);
                if (whiteInkLayer_DC) {
                    whiteInkLayer_DC.visible = false; // Hide the layer
                    hiddenWhiteInkLayers_DC.push(whiteInkLayer_DC); // Keep track of the hidden layers
                }
            } catch (e) {
                // Skip if the layer is not found
                continue;
            }
        }


        // Ensure preset is applied
        // app.executeMenuCommand("Adobe PDF Preset Options");

        // Rasterize images to 72 dpi
        // app.executeMenuCommand("raster");

        // Flatten transparency to reduce file size
        // app.executeMenuCommand("flattenTransparency");





        // Export PDF using the preset (low-resolution)
        var pdfExportLowOptions_DC = new PDFSaveOptions();
        pdfExportLowOptions_DC.preset = lowResPresetName_DC;
        pdfExportLowOptions_DC.colorProfileID = ColorProfile.INCLUDEALLPROFILE; // Ensure the color profile is included

        pdfExportLowOptions_DC.compatibility = PDFCompatibility.ACROBAT5; // Older compatibility reduces size
        // pdfExportLowOptions_DC.compressArt = true; // Ensure compression is applied
        // pdfExportLowOptions_DC.colorCompression = CompressionQuality.LOW; // JPEG compression
        pdfExportLowOptions_DC.downsampleImages = true;
        pdfExportLowOptions_DC.resolution = 72; // Force downsample to 72dpi
        pdfExportLowOptions_DC.optimizeForFastWebView = true; // Remove metadata
        // pdfExportLowOptions_DC.preserveEditability = false; // Remove Illustrator data

        myDoc_DC.saveAs(pdfFile_DC, pdfExportLowOptions_DC); // Save low-res PDF

        // Unlock all layers
        for (var i = 0; i < myDoc_DC.layers.length; i++) {
            myDoc_DC.layers[i].locked = false; // Unlock each layer
        }

        // Unlock all objects
        app.executeMenuCommand('unlockAll');

        // Select all objects
        app.executeMenuCommand('selectall');

        // Check if everything is selected (for outline command to work)
        if (app.selection.length === 0) {
            alert("No objects are selected for outlining. Please select all objects.");
            return;
        }

        // Apply the outline command
        app.executeMenuCommand('outline');

        // Unhide the white ink layers after PDF export
        for (var j = 0; j < hiddenWhiteInkLayers_DC.length; j++) {
            hiddenWhiteInkLayers_DC[j].visible = true; // Unhide the layer
        }

        // Unselect everything after saving the .ai file
        app.selection = null;

        // Save the document in Illustrator format with "_VEC" before the extension
        var aiFolder_DC = new Folder(packageFolder_DC + "/_ADOBE_CC");

        var aiFileName_DC = myDoc_DC.name.replace(/(_HR|_LR)(?=\.[^.]+$)/, ""); // Remove _HR or _LR before the extension
        aiFileName_DC = aiFileName_DC.replace(/\.pdf$/i, "") + "_VEC.ai"; // Remove .pdf extension and add "_VEC" before .ai extension

        var aiFile_DC = new File(aiFolder_DC + "/" + aiFileName_DC);

        var aiSaveOptions_DC = new IllustratorSaveOptions();
        aiSaveOptions_DC.embedICCProfile = true; // Ensure ICC profile is embedded
        aiSaveOptions_DC.colorProfileID = ColorProfile.INCLUDEALLPROFILE; // Use a generic profile inclusion

        myDoc_DC.saveAs(aiFile_DC, aiSaveOptions_DC); // Save as Illustrator file

        alert("Low-resolution PDF and Illustrator file exported successfully!");

    } catch (e) {
        alert("Error exporting low-resolution PDF and Illustrator file: " + e.message);
    }
}

 

Correct answer dimitri_cas

Hello…

 

I figured out.

I made a copy-past from ChatGPT, but apparently there were a few errors like:

pdfExportLowOptions_DC.preset = lowResPresetName_DC;

 

Isn't correct, but the following is correct:

pdfExportLowOptions_DC.pDFPreset = lowResPresetName_DC;

 

So yeah… be careful when using ChatGPT  😉

Cheers

1 reply

dimitri_casAuthorCorrect answer
Inspiring
March 24, 2025

Hello…

 

I figured out.

I made a copy-past from ChatGPT, but apparently there were a few errors like:

pdfExportLowOptions_DC.preset = lowResPresetName_DC;

 

Isn't correct, but the following is correct:

pdfExportLowOptions_DC.pDFPreset = lowResPresetName_DC;

 

So yeah… be careful when using ChatGPT  😉

Cheers