如何使用 Adob​​e PDF Services 进行 2 个或更多调用并跳过使用文件系统在两者之间?

问题描述

只需调用一次 Adobe PDF Services获取结果并保存它是相当简单的,例如:

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

但是如果我想做两个或更多的操作,我是否需要继续将结果保存到文件系统并重新提供它(甚至是一个词;)给 API?

解决方法

所以这也让我绊倒了。在大多数演示中,您会看到:

result => result.saveAsFile()

接近尾声。但是,传递给已完成承诺的对象 result 是一个 FileRef 对象,然后可以用作另一个调用的输入。

以下示例采用输入 Word 文档并调用 API 方法来创建 PDF。然后它接受并在其上运行 OCR。包装 API 调用的两种方法都返回 FileRefs,所以最后我 saveAsFile 在它上面。 (请注意,此演示使用的是 SDK 的 v1,它与 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,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));

    });
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...