Export PDF to JPG - Node.js - Posting PDF to endpoint, return Zip File

New Here ,
May 19, 2020 May 19, 2020

Copy link to clipboard

Copied

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

TOPICS
Service SDK

Views

180

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
May 29, 2020 May 29, 2020

Copy link to clipboard

Copied

LATEST

Thanks TSomann for using PDF Services SDK. 
We have looked into the issue and would like to understand the environment better to be able to fix it.
Since formidable library is being used to parse the mutlipart response, One probable cause is that formidable library might have an issue with some library or framework being used in this scenario.
Could you please share the details of any framework or libraries which are included in this node js server code along with services sdk package ? This will help us in replicating the issue on our side and find the conflict.
If it is possible to share the whole code as a zip file, that will be even better. 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines