0

I am purely newbie in nodejs . I am trying to export pdf to docx using PDF API available from Adobe API services. I got few code online to achieve this from nodejs (Python not suitable here). I have file in AWS S3 , need to export the file into docx. I did minor changes in code . When I try running this . It throws an exception as : "Exception encountered while executing operation-2 Error: No input was set for operation" Need some help or advice to overcome this .

/*
 * Copyright 2019 Adobe
 * All Rights Reserved.
 *
 * NOTICE: Adobe permits you to use, modify, and distribute this file in
 * accordance with the terms of the Adobe license agreement accompanying
 * it. If you have received this file from a source other than Adobe,
 * then your use, modification, or distribution of it requires the prior
 * written permission of Adobe.
 */
// Lambda handler code //
console.log('Loading function');
exports.handler = async (event, context) => {
    console.log('Loading function');
    const PDFServicesSdk = require('@adobe/pdfservices-node-sdk');
    
    // Start reading the file from s3//
    const aws = require('aws-sdk');
    //const s3 = new aws.S3({ apiVersion: '2006-03-01' });
    const bucket = 'translation-bucket-qa-v1';
    const key = 'TranslationPipeline/input_pdf_img/Gas_bill_sample.pdf'
    const filename = 'Gas_bill_sample.pdf'
    /*
    const params = {
        Bucket: bucket,
        Key: key,
    };
    
    try {
        const { ContentType } = await s3.getObject(params).promise();
        console.log('CONTENT TYPE:', ContentType);
        return ContentType;
    } catch (err) {
        console.log(err);
        const message = 'Error getting object ${key} from bucket ${bucket}. Make sure they exist and your bucket is in the same region as this function.';
        console.log(message);
        throw new Error(message);
    }*/
    
    
    //Reading the file ends here !!!!///

    /**
     * This sample illustrates how to export a PDF file to a Word (DOCX) file
     * <p>
     * Refer to README.md for instructions on how to run the samples.
     */

    try {
        // Initial setup, create credentials instance.
        const credentials =  PDFServicesSdk.Credentials
            .serviceAccountCredentialsBuilder()
            .fromFile("pdfservices-api-credentials.json")
            .build();
        
        //Create an ExecutionContext using credentials and create a new operation instance.
        const executionContext = PDFServicesSdk.ExecutionContext.create(credentials),
            exportPDF = PDFServicesSdk.ExportPDF,
            exportPdfOperation = exportPDF.Operation.createNew(exportPDF.SupportedTargetFormats.DOCX);

        /* Commenting it to test s3 read//
        
        // Set operation input from a source file
        const input = PDFServicesSdk.FileRef.createFromLocalFile('resources/exportPDFInput.pdf'); //about input
        exportPdfOperation.setInput(input);
        
        / Commenting it to test s3 read*/
        
        //Download  from s3 //
        const filePath = '/tmp/';
        var s3 = new aws.S3();
        
        const downloadFile = (filePath, bucket, key) => {
            const params = {
                    Bucket: bucket,
                    Key: key
                    };
            s3.getObject(params, (err, data) => {
                if (err) console.error(err);
                fs.writeFileSync(filePath, data.Body.toString());
                console.log(`${filePath} has been created!`);
                });
                };
        
        console.log('File downloaded')
        //Changed till here//
        
        // Execute the operation and Save the result to the specified location.
        exportPdfOperation.execute(executionContext)
            .then(result => result.saveAsFile(filePath+'exportPdfOutput.docx')) // about output
            .catch(err => {
                if(err instanceof PDFServicesSdk.Error.ServiceApiError
                    || err instanceof PDFServicesSdk.Error.ServiceUsageError) {
                    console.log('Exception encountered while executing operation -1', err);
                } else {
                    console.log('Exception encountered while executing operation-2', err);
                }
            });
        console.log('File is in temp')
    } catch (err) {
        console.log('Exception at first try', err);
    }
};

Please help and let me know what more info you need. The exception is thrown here "Exception encountered while executing operation-2"

rahul
  • 35
  • 3

1 Answers1

0

It's an async issue. Before you call our (Adobe's) APIs, you need to ensure the file is completely downloaded from S3. You are calling the operation but aren't waiting for it to finish. So basically - wait for s3 to finish - and then call our API.

Raymond Camden
  • 10,661
  • 3
  • 34
  • 68