Export PDF to JPG - Node.js - Posting PDF to endpoint, return Zip File
Hi,
I'm trying to use the Document Cloud Service SDK to create an endpoint in Node.js which converts PDFs to zip files of jpegs and return it to the caller. I built a quick implementation based on the node.js sample code:
app.post('/processor', function(req, res) {
// Get file from request
var file = req.files[Object.keys(req.files)[0]]
// Generate Temporary Name
var temp_name = shortid.generate()
var filename = "./uploads/"+temp_name +".pdf"
var zipname = temp_name +".zip"
fs.writeFile(filename, file.data, function(err) {
if(err) {
console.log(err)
res.status(500).send(err)
}
// Initial setup, create credentials instance.
const credentials = DCServicesSdk.Credentials
.serviceAccountCredentialsBuilder()
.fromFile("dc-services-sdk-credentials.json")
.build();
//Create an ExecutionContext using credentials and create a new operation instance.
const executionContext = DCServicesSdk.ExecutionContext.create(credentials),
exportPDF = DCServicesSdk.ExportPDF,
exportPdfOperation = exportPDF.Operation.createNew(exportPDF.SupportedTargetFormats.JPEG);
// Set operation input from a source file
const input = DCServicesSdk.FileRef.createFromLocalFile(filename);
exportPdfOperation.setInput(input);
// Execute the operation and Save the result to the specified location.
exportPdfOperation.execute(executionContext)
.then(result => result.saveAsFile('output/'+zipname))
.then(function(result) {
res.sendFile(__dirname+'/output/'+zipname)
})
.catch(err => {
if(err instanceof DCServicesSdk.Error.ServiceApiError
|| err instanceof DCServicesSdk.Error.ServiceUsageError) {
console.log('[1] Exception encountered while executing operation'); console.log(err);
} else {
console.log('[2] Exception encountered while executing operation'); console.log(err);
}
res.status(500).send(err)
})
})
})
Roughly half of the time I attempt to upload a PDF to the endpoint, the following error occurs:
{ Error: ENOENT: no such file or directory, rename '/root/pdfservice/node-service/pdfServicesSdkResult/gYpsItQfRPLG.zip' -> 'output/pxQkNzIrz.zip'
errno: -2,
code: 'ENOENT',
syscall: 'rename',
path: '/root/pdfservice/node-service/pdfServicesSdkResult/gYpsItQfRPLG.zip',
dest: 'output/pxQkNzIrz.zip' }
The source of the error is saveAsFile method in src/internal/io/file-info.js
saveAsFile(destinationPath) {
if (!this._isOperationResult) {
throw new Error('saveAsFile can only be called on operation result instances');
}
if (destinationPath) {
const actualFilePath = getActualTargetFilePath(destinationPath, this._extension);
logger.info(`Moving the file from temporary location ${this._fileSource} to ${actualFilePath}.`);
return moveFile(this._fileSource, actualFilePath, { overwrite: false })
.then(() => {
this._isOperationResult = false;
});
}
throw new Error('No destination path provided for saving file');
}
Processing these files using the sample code provided does not cause this issue, therefore I don't believe that the issue is with the PDFs. Is there an issue with my code that is causing the error? Otherwise, what could be the potential cause? Thanks.
Edit:
Operating System: Ubuntu 18.04
Node Version: 8.10.0
