0

It's fairly simple to make one call to Adobe PDF Services, get the result, and save it, for example:

// more stuff above
exportPdfOperation.execute(executionContext)
.then(result => result.saveAsFile(output))

But if I want to do two, or more, operations, do I need to keep saving the result to the file system and re-providing it (is that even a word ;) to the API?

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

1 Answers1

1

So this tripped me up as well. In most demos, you'll see:

result => result.saveAsFile()

towards the end. However, the object passes to the completed promise, result, is a FileRef object that can then be used as the input to another call.

Here's a sample that takes an input Word doc and calls the API method to create a PDF. It then takes that and runs OCR on it. Both methods that wrap the API calls return FileRefs, so at the end I saveAsFile on it. (Note, this demo is using v1 of the SDK, it would work the same w/ v2.)

const PDFToolsSdk = require('@adobe/documentservices-pdftools-node-sdk');
const fs = require('fs');

//clean up previous
(async ()=> {

    // hamlet.docx was too big for conversion
    const input = './hamlet2.docx';
    const output = './multi.pdf';
    const creds = './pdftools-api-credentials.json';

    if(fs.existsSync(output)) fs.unlinkSync(output);

    let result = await createPDF(input, creds);
    console.log('got a result');
    result = await ocrPDF(result, creds);
    console.log('got second result');

    await result.saveAsFile(output);

})();

async function createPDF(source, creds) {

    return new Promise((resolve, reject) => {

        const credentials =  PDFToolsSdk.Credentials
        .serviceAccountCredentialsBuilder()
        .fromFile(creds)
        .build();

        const executionContext = PDFToolsSdk.ExecutionContext.create(credentials),
                createPdfOperation = PDFToolsSdk.CreatePDF.Operation.createNew();

        // Set operation input from a source file
        const input = PDFToolsSdk.FileRef.createFromLocalFile(source);
        createPdfOperation.setInput(input);

        let stream = new Stream.Writable();
        stream.write = function() {

        }
        
        stream.end = function() {
            console.log('end called');
            resolve(stream);
        }

        // Execute the operation and Save the result to the specified location.
        createPdfOperation.execute(executionContext)
        .then(result => resolve(result))
        .catch(err => {
            if(err instanceof PDFToolsSdk.Error.ServiceApiError
            || err instanceof PDFToolsSdk.Error.ServiceUsageError) {
                reject(err);
            } else {
                reject(err);
            }
        });

    });
}

async function ocrPDF(source, creds) {

    return new Promise((resolve, reject) => {

        const credentials =  PDFToolsSdk.Credentials
        .serviceAccountCredentialsBuilder()
        .fromFile(creds)
        .build();

        const executionContext = PDFToolsSdk.ExecutionContext.create(credentials),
            ocrOperation = PDFToolsSdk.OCR.Operation.createNew();

        // Set operation input from a source file.
        //const input = PDFToolsSdk.FileRef.createFromStream(source);
        ocrOperation.setInput(source);

        let stream = new Stream.Writable();
        stream.end = function() {
            console.log('end called');
            resolve(stream);
        }

        // Execute the operation and Save the result to the specified location.
        ocrOperation.execute(executionContext)
       .then(result => resolve(result))
       .catch(err => reject(err));

    });
}
Raymond Camden
  • 10,661
  • 3
  • 34
  • 68