• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

save pdf and peg

Explorer ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

 

Hi,

I have this script which save me two pdf file, low and hight resolution.

I would like to include 2 more things

1. to overwrite file name in case I save several times

2. export jpeg file 

..a total of 3 files: pdf low, pdf hight and jpeg 

Thanks

 

function HsaveAsPDF() {
var pdfFile = new File(app.activeDocument.path + "/" + "print_" + app.activeDocument.name.split('.')[0] + '.pdf');
var pdfOptions = new PDFSaveOptions();
pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
pdfOptions.generateThumbnails = true;
pdfOptions.preserveEditability = false;
pdfOptions.registrationMarks = false;

pdfOptions.pDFPreset = "[Illustrator Default]";
app.activeDocument.saveAs(pdfFile, pdfOptions);
}
HsaveAsPDF()

function LsaveAsPDF() {
var LpdfFile = new File(app.activeDocument.path + "/" + "preview_" + app.activeDocument.name.split('.')[0] + '.pdf');
var LpdfOptions = new PDFSaveOptions();
LpdfOptions.compatibility = PDFCompatibility.ACROBAT5;
LpdfOptions.generateThumbnails = true;
LpdfOptions.preserveEditability = false;
LpdfOptions.registrationMarks = false;
LpdfOptions.pDFPreset = "[Smallest File Size]";
app.activeDocument.saveAs(LpdfFile, LpdfOptions);

}
LsaveAsPDF()

Views

245

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe
Community Expert ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

Hi,

Try following snippet

function HsaveAsPDF() {
    var pdfFile = new File(app.activeDocument.path + "/" + "print_" + app.activeDocument.name.split('.')[0] + '.pdf');
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    pdfOptions.generateThumbnails = true;
    pdfOptions.preserveEditability = false;
    pdfOptions.registrationMarks = false;

    pdfOptions.pDFPreset = "[Illustrator Default]";
    app.activeDocument.saveAs(pdfFile, pdfOptions);
}

function LsaveAsPDF() {
    var LpdfFile = new File(app.activeDocument.path + "/" + "preview_" + app.activeDocument.name.split('.')[0] + '.pdf');
    var LpdfOptions = new PDFSaveOptions();
    LpdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    LpdfOptions.generateThumbnails = true;
    LpdfOptions.preserveEditability = false;
    LpdfOptions.registrationMarks = false;
    LpdfOptions.pDFPreset = "[Smallest File Size]";
    app.activeDocument.saveAs(LpdfFile, LpdfOptions);

}


var dest = Folder(Folder.desktop);

function exportJPG() {

    String.prototype.hexEncode = function () {
        var hex = '';
        for (var i = 0; i < this.length; i++) {
            hex += '' + this.charCodeAt(i).toString(16);
        }
        return hex;
    };


    function writeFile(fileDestStr, contents) {
        var newFile = File(fileDestStr);
        newFile.open('w');
        newFile.write(contents);
        newFile.close();
    };


    var actionStr = [
        "/version 3",
        "/name [ 4",
        "54657374",
        "]",
        "/isOpen 1",
        "/actionCount 1",
        "/action-1 {",
        "/name [ 12",
        "53617665204d79204a504547",
        "]",
        "/keyIndex 0",
        "/colorIndex 0",
        "/isOpen 1",
        "/eventCount 1",
        "/event-1 {",
        "/useRulersIn1stQuadrant 0",
        "/internalName (adobe_exportDocument)",
        "/localizedName [ 9",
        "4578706f7274204173",
        "]",
        "/isOpen 1",
        "/isOn 1",
        "/hasDialog 1",
        "/showDialog 0",
        "/parameterCount 7",
        "/parameter-1 {",
        "/key 1885434477",
        "/showInPalette 0",
        "/type (raw)",
        "/value < 104",
        "0a00000001000000030000000200000000002c0101000000000000000100000069006d006100670065006d00610070000000310000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
        ">",
        "/size 104",
        "}",
        "/parameter-2 {",
        "/key 1851878757",
        "/showInPalette -1",
        "/type (ustring)",
        "/value [ PUT_FOLDERPATH_CHAR_LENGTH_HERE",
        "PUT_HEX_FOLDERPATH_HERE",
        "]",
        "}",
        "/parameter-3 {",
        "/key 1718775156",
        "/showInPalette -1",
        "/type (ustring)",
        "/value [ 16",
        "4a5045472066696c6520666f726d6174",
        "]",
        "}",
        "/parameter-4 {",
        "/key 1702392942",
        "/showInPalette -1",
        "/type (ustring)",
        "/value [ 12",
        "6a70672c6a70652c6a706567",
        "]",
        "}",
        "/parameter-5 {",
        "/key 1936548194",
        "/showInPalette -1",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-6 {",
        "/key 1935764588",
        "/showInPalette -1",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-7 {",
        "/key 1936875886",
        "/showInPalette -1",
        "/type (ustring)",
        "/value [ 1",
        "31",
        "]",
        "}",
        "}",
        "}"
    ].join("\n");

    if (app.documents.length == 0) {
        return;
    }


    var destStr = decodeURI(app.activeDocument.path.fsName);
    var actionFileDestStr = Folder.desktop + "/MyAction.aia";
    writeFile(actionFileDestStr, actionStr.replace("PUT_FOLDERPATH_CHAR_LENGTH_HERE", destStr.length).replace("PUT_HEX_FOLDERPATH_HERE", destStr.hexEncode()));
    var actionFile = File(actionFileDestStr);
    app.loadAction(actionFile);
    app.doScript("Save My JPEG", "Test");


    //clean up 
    actionFile.remove();
    app.unloadAction("Test", '');
};


exportJPG();
HsaveAsPDF();
LsaveAsPDF();

 

What do you mean by the statement to overwrite file name in case I save several times?

Do you mean to overwrite or not to overwrite? Because if you this document with the same it will overwrite it automatically.

 

 

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

 

What do you mean by the statement to overwrite file name in case I save several times?

Do you mean to overwrite or not to overwrite? Because if you this document with the same it will overwrite it automatically.

 

 


By @Charu Rajput

 

Tkanks!

I need a comand , if exist, to overwrite filename..If I save "test" document more than one time, will create a new documents with duplicate name:

first save "test_print.pdf"

second save "test_print_print.pdf"

third  save "test_print_print_print.pdf"

and so on..

 

this is update script

var doc = app.activeDocument;  
var originalDocPath = doc.path;  
var originalDocName = doc.name;  
var allLayers = doc.layers;  

originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")  
  
var lowSave = new PDFSaveOptions();  
lowSave.compatibility = PDFCompatibility.ACROBAT5;
lowSave.generateThumbnails = true;
lowSave.preserveEditability = false;
lowSave.registrationMarks = false;
lowSave.pDFPreset = "preview";  
doc.saveAs(File(originalDocPath + "/" + originalDocName + "_preview.pdf"), lowSave);
//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);


var hiSave = new PDFSaveOptions();  
hiSave.compatibility = PDFCompatibility.ACROBAT5;
hiSave.generateThumbnails = true;
hiSave.preserveEditability = false;
hiSave.registrationMarks = false;
hiSave.pDFPreset = "[Illustrator Default]"; 
doc.saveAs(File(originalDocPath + "/" + originalDocName + "_print.pdf"), hiSave);  
//app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 23, 2021 Mar 23, 2021

Copy link to clipboard

Copied

LATEST

Hi, Try following updated script

function HsaveAsPDF() {
    var fileName = originalDocPath + '/' + originalDocName + '_print.pdf';
    var pdfFile = new File(fileName);
    while (pdfFile.exists) {
        fileName = fileName.split('.pdf')[0] + '_print.pdf';
        var pdfFile = new File(fileName);
    }
    var pdfOptions = new PDFSaveOptions();
    pdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    pdfOptions.generateThumbnails = true;
    pdfOptions.preserveEditability = false;
    pdfOptions.registrationMarks = false;

    pdfOptions.pDFPreset = "[Illustrator Default]";
    app.activeDocument.saveAs(pdfFile, pdfOptions);
}

function LsaveAsPDF() {
    var fileName = originalDocPath + '/' + originalDocName + '_preview.pdf'
    var LpdfFile = new File(fileName);
    while (LpdfFile.exists) {
        fileName = fileName.split('.pdf')[0] + '_preview.pdf';
        var LpdfFile = new File(fileName);
    }
    var LpdfOptions = new PDFSaveOptions();
    LpdfOptions.compatibility = PDFCompatibility.ACROBAT5;
    LpdfOptions.generateThumbnails = true;
    LpdfOptions.preserveEditability = false;
    LpdfOptions.registrationMarks = false;
    LpdfOptions.pDFPreset = "[Smallest File Size]";
    app.activeDocument.saveAs(LpdfFile, LpdfOptions);

}

var dest = Folder(Folder.desktop);


function exportJPG(pth) { //Export 300dpi CMYK jpg
    var str = "";
    for (var i = 0; i < pth.length; i++) str += u16to8(pth.charCodeAt(i));
    var act = "/version 3/name [ 4 73657431]/isOpen 1"
        + "/actionCount 1/action-1 {/name [ 4 61637431]/keyIndex 0/colorIndex 0/isOpen 1/eventCount 1"
        + "/event-1 {/useRulersIn1stQuadrant 0/internalName (adobe_exportDocument)"
        + "/isOpen 0/isOn 1/hasDialog 1/showDialog 0/parameterCount 7"
        + "/parameter-1 {/key 1885434477/showInPalette 0/type (raw)"
        + "/value < 100 0a00000001000000030000000100000000002c01020000000000000001000000"
        + "69006d006100670065006d006100700000000000000000000000000000000000"
        + "0000000000000000000000000000000000000000000000000000000000000000"
        + "00000100>/size 100}" //Probably, parameter for exporter plugin
        + "/parameter-2 {/key 1851878757/showInPalette 4294967295"
        + "/type (ustring)/value [ " + str.length / 2 + " " + str + "]}"
        + "/parameter-3 {/key 1718775156/showInPalette 4294967295"
        + "/type (ustring)/value [ 16 4a5045472066696c6520666f726d6174]}" // JPEG file format
        + "/parameter-4 {/key 1702392942/showInPalette 4294967295"
        + "/type (ustring)/value [ 12 6a70672c6a70652c6a706567]}" //jpg,jpe,jpeg
        + "/parameter-5 {/key 1936548194/showInPalette 4294967295/type (boolean)/value 0}"
        + "/parameter-6 {/key 1935764588/showInPalette 4294967295/type (boolean)/value 1}"
        + "/parameter-7 {/key 1936875886/showInPalette 4294967295/type (ustring)/value [ 0]}}}"
    var tmp = File(Folder.desktop + "/set1.aia");
    tmp.open('w');
    tmp.write(act);
    tmp.close();
    app.loadAction(tmp);
    app.doScript("act1", "set1", false);
    app.unloadAction("set1", "");
    tmp.remove();
}

function u16to8(cd) {
    var out =
        (cd < 0x80
            ? toHex2(cd)
            : (cd < 0x800
                ? toHex2(cd >> 6 & 0x1f | 0xc0)
                : toHex2(cd >> 12 | 0xe0) +
                toHex2(cd >> 6 & 0x3f | 0x80)
            ) + toHex2(cd & 0x3f | 0x80)
        );
    return out;
}

function toHex2(num) {
    var out = '0' + num.toString(16);
    return out.slice(-2);
}


//Start
var doc = app.activeDocument;
var originalDocPath = doc.path;
var originalDocName = doc.name.replace(/\.pdf|\.ai/gi, "");
var fileName = doc.path + "/" + originalDocName + ".jpg";
var jpegFile = File(fileName);
var counter = 0;
while (jpegFile.exists) {
    fileName = fileName.split('.jpg')[0] + '_' + (++counter) + '.jpg';;
    var jpegFile = File(fileName);
}

exportJPG(jpegFile.fsName);
HsaveAsPDF();
LsaveAsPDF();
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

 

For High Res file, file will be saved as

test_print.pdf,

test_print_print.pdf,

test_print_print_print.pdf.

 

For Low Res file, file will be saved as 

test_preview.pdf,

test_preview_preview.pdf,

test_print_print_print.pdf.

 

For jpeg file, file will be saved as

test.jpg

test_1.jpg

test_2.jpg

and so on..

 

I hope this is what you required.

Best regards

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines