AEM 65 - Configure InDesign Server Job Options in Folder Metadata for PDF Generation using Presets

Goal


For InDesign files uploaded to AEM, specify Folder specific PDF Presets and generate PDFs using AEM InDesign Server integration. Add IDS Job Options file name in AEM folder metadata, read and use in the PDF generation script executed in InDesign Server. A Job Options file is an Adobe PDF Preset file that defines the properties of a PDF generated using applications like InDesign.

This extension is for configuring the PDF presets per AEM folder. Here a folder in AEM is configured with preset EAEM Gray and the generated PDFs uploaded to that folder by InDesign Server have all colors converted to gray

About AEM folder metadata schemas check documentation here

About InDesign PDF Job Options check documentation here

Demo | Package Install | Github


PDF Generated by Product



PDF with Preset [EAEM Gray] generated by Extension



Solution


1) Create the specific PDF preset EAEM Gray using Adobe InDesign 2019 CC



2) Job options file for the preset - EAEM Gray.joboptions (download here) gets created in folder C:\Users\<user>\AppData\Roaming\Adobe\Adobe PDF\Settings. Following properties in the job options file define color conversion...

                                  /ConvertColors /ConvertToGray
                                  /DestinationProfileName (Dot Gain 15%)

3) Copy the file to InDesign server PDF Presets location e.g. C:\Program Files\Adobe\Adobe InDesign CC Server 2019\Resources\Adobe PDF\settings\mul

Similarly copy all necessary PDF presets joboptions files into this location (only the name of joboptions file is configured in AEM folder metadata, actual file resides in InDesign Server)



4) Create a folder metadata schema Experience AEM InDesign for configuring the preset name EAEM Gray and apply to a folder in AEM e.g. /content/dam/experience-aem



5) Create InDesign script PDFExportUsingJobOptions.jsx with the following code, for reading preset name in AEM folder metadata using query builder, generate the PDF,  upload to AEM (create the script in location /apps/settings/dam/indesign/scripts and make sure user idsjobprocessor has read permissions on the folder)

app.consoleout('START - Creating PDF Export using jobOptions....');

exportPDFUsingJobOptions();

app.consoleout('END - Creating PDF Export using jobOptions....');

function exportPDFUsingJobOptions(){
    var QUERY = "/bin/querybuilder.json?path=/content/dam&nodename=" + getAEMNodeName(),
        META_EAEM_PDF_JOBOPTIONS = "eaemPDFJobOptions";

    app.consoleout("QUERY : " + QUERY);

    var response = getResponse(host, QUERY, credentials);

    if(!response){
        return;
    }

    var hitMap = JSON.parse(response);

    if(!hitMap || (hitMap.hits.length == 0)){
        app.consoleout('No Query results....');
        return;
    }

    var aemFilePath = hitMap.hits[0]["path"],
        folderPath = getAEMParentFolder(aemFilePath);

    app.consoleout("Folder path : " + folderPath);

    response = getResponse(host, folderPath + "/jcr:content/metadata.json", credentials);

    var metadataMap = JSON.parse(response);

    var jobOptionsName = metadataMap[META_EAEM_PDF_JOBOPTIONS];

    if(!jobOptionsName){
        app.consoleout('jobOptions does not exist in folder metadata....');
        return;
    }

    var exportFolderPdf = new Folder(exportFolder.fullName + "/pdf"),
        pdfFileName = fileName + ' - ' + jobOptionsName + '.pdf';

    try{
        exportFolderPdf.create();

        app.consoleout("Creating pdf file " + (exportFolderPdf.fullName + pdfFileName) + " with preset job option - " + jobOptionsName);

        var pdfOutputFile = new File(exportFolderPdf.fullName + pdfFileName);

        with (app.pdfExportPreferences) {
            viewDocumentAfterExport = false;
        }

        document.exportFile(ExportFormat.pdfType, pdfOutputFile, app.pdfExportPresets.item("[" + jobOptionsName+ "]"));

        app.consoleout("Uploading to AEM pdf file " + pdfFileName + ' to location: ' + target + '/jcr:content/renditions');

        putResource (host, credentials,  pdfOutputFile, pdfFileName, 'application/pdf', target);
    }catch (err) {
        //app.consoleout(err);
    }finally {
        cleanup(exportFolderPdf);
    }
}

function getAEMNodeName(){
    return resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
}

function getAEMParentFolder(filePath){
    return filePath.substring(0, filePath.lastIndexOf("/"));
}

function getResponse(host, uri, creds){
    var aemConn = new Socket, body = "", response, firstRead = true;

    if (!aemConn.open(host, "UTF-8")) {
        return;
    }

    aemConn.write ("GET "+ encodeURI(uri) +" HTTP/1.0");
    aemConn.write ("\n");
    aemConn.write ("Authorization: Basic " + creds);
    aemConn.write ("\n\n");

    while( response = aemConn.read() ){
        response = response.toString();

        if(!firstRead){
            body = body + response;
            continue;
        }

        var strings = response.split("\n");

        for(var x = 0; x < strings.length; x++){
            if( (x == 0) && (strings[x].indexOf("200") === -1)){
                return body;
            }

            if(x === (strings.length - 1)){
                body = body + strings[x];
            }
        }

        firstRead = false;
    }

    return body;
}

6) Add the above script in DAM Update asset workflow's Media Extraction step



7) Upload an InDesign file to the folder in AEM e.g./content/dam/experience-aem; the following messages are logged in InDesign Server console



8) The preset specific PDF is uploaded to renditions folder of the asset by InDesign server, with name <file name> - <preset name>.pdf





No comments:

Post a Comment