我纯粹是nodejs的新手。我试图使用Adobe服务提供的pdf将PDF导出到docx。我从nodejs (Python不适合这里)获得了很少的在线代码来实现这一点。我在AWS S3中有文件,需要将文件导出到docx中。我对代码做了一些小改动。当我尝试运行这个的时候。它抛出一个异常为:“执行操作时遇到的异常-2错误:没有为操作设置输入”需要一些帮助或建议来克服这一点。
/*
* 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);
}
};请帮忙,让我知道你还需要什么信息。在此抛出异常“执行操作-2时遇到的异常”。
发布于 2022-09-21 19:31:13
这是一个异步问题。在调用我们的(Adobe)之前,需要确保文件完全从S3下载。您正在调用操作,但没有等待操作完成。所以基本上--等待s3完成-然后调用我们的API。
https://stackoverflow.com/questions/73788644
复制相似问题