trying to convert docx into pdf on s3 using cloudconvert-api in nodejs

648 Views Asked by At

I am trying to convert docx file to pdf while uploading on s3 spaces using cloudconvert api in nodejs. When I run my code, it is uploading docx file but the conversion is not happening and it is not giving any errors also. I don't understand what i am doing wrong. Here is my code bellow.

app.post('/upload/file', upload.single('file'), (req, res) => {
    cloudconvert.createProcess(
            { inputformat: 'docx', outputformat: 'pdf' },
            (err, process) => {
              if (err) {
                console.error(`CloudConvert Process creation failed: ${err}`)
              } else {
                process.start({
                  input: {
                    s3: {
                      accesskeyid: SPACES_ACCESS_KEY_ID,
                      secretaccesskey: SPACES_SECRET_ACCESS_KEY,
                      bucket: 'files'
                    } 
                  },
                  file: req.file.key,
                  outputformat: 'pdf',
                  output: {
                    s3: {
                      accesskeyid: SPACES_ACCESS_KEY_ID,
                      secretaccesskey: SPACES_SECRET_ACCESS_KEY,
                      bucket: 'files'
                    }
                  }
                }, (err, process) => {
                  if(err) return console.log(err.message)

                  console.log('process', process)
                })
              }
            }
          )
     })

I am using multer to handle multipart form-data. while I am trying to run the code it is not showing any errors and didn't convert the file into pdf. Please let me know what I am doing wrong. Thank you.

1

There are 1 best solutions below

0
On

Hi I had the same issue and I am not an expert but something that you could do to fix the problem is put this code inside a promise, so you will wait until the conversion process finishes, here you could find an example

inputformat is the extension of your file outputformat you desired extension params the require parameters to process and save the file

 return new Promise((resolve, reject) => {
    response = cloudconvert.createProcess({inputformat: inputformat,
                                           outputformat: outputformat},
               function(error, process){
                 process.start(params, function(error, process) {
                   if (error){
                     reject({"status": "error" , "message": error})
                   } else {
                     process.wait(function(error, process){
                       if (error){
                         reject({status: "error", "error": error})
                       } else {
                         console.log(process.data.message)
                         resolve({"status": "ok" , 
                                  "message": "process complete"})
                       }
                     });
                   }
               });
           });
      });

hope this help you, the idea is that you can ensure you are getting and completing the conversion process