0

调用Adob​​e PDF Services获取结果并保存相当简单,例如:

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

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

4

1 回答 1

1

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

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, 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));

    });
}
于 2021-06-25T15:39:06.820 回答