ManishRautela

How to download PDF document in node js

0 votes

Please  share node js code to download signed document.


Reply to: How to download PDF document in node js

0 votes

Hi Manish,

 

In a previous post,  I've created some sample codes on how to download signed documents with Node JS and push to front end (with Fetch API). Just adjust the code if you want to store the file locally.

 

Node JS end:

var request = require('request');

var eslFunctions = module.exports = 
{

    downloadZip: function(req, res){

        var options = 
        { 
            method: 'GET',
            url: 'https://sandbox.esignlive.com/api/packages/Xs-rVmDCCHqiVLMyoaxwnceO8tI=/documents/zip',
            headers: 
            { 
                accept: 'application/zip',
                authorization: 'Basic xxxxxxxx'
            }
        };

          request(options).on('response', function(response) {
            res.set({
                "Content-Type": "application/zip",
                  "Content-Disposition" : "attachment; filename=download.zip"
            });
        })
        .pipe(res);
    }

};

 

Front end:
fetch('http://localhost:8080/oss/zip')
 .then(resp => {
     const blob = resp.blob()
     console.log(resp)
     console.log(blob)
     return blob
 })
  .then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = url;
    // the filename you want
    a.download = 'attachment.zip';
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
    alert('your file has downloaded!'); 
  })
  .catch(() => alert('oh no!'));

 

Duo

Duo Liang OneSpan Evangelism and Partner Integrations Developer


Reply to: How to download PDF document in node js

0 votes

Thanks Duo for your response.

 

Please let me know one thing how to download that file in node js side instead of front end.

 

And when I download from node js, that time it shows me file is encrypted , I have attached my code as well.

 

export let DownloadEsignDocument = (req: Request, res: Response, next: NextFunction) => {

    try {

        var options = {

            'method': 'GET',

            'url': 'https://sandbox.esignlive.com/api/packages/eQPdmcgAhn7djTl_-gY9cn3-i-E=/documents/6bdedd15e9c3eedf4d5b860af33a396d60ac7f709947a600/pdf',

            'headers': {

              'Authorization': 'Basic xxxxxxxxxxxxxxxxxxx',

              'Accept': 'application/pdf'

            }

          };

        request(options, function (error, response) {

            if (error) throw new Error(error);

            else {          

                const fs = require('fs');

                

                fs.writeFileSync('some1.pdf', arrByte.toString());

                fs.writeFileSync('some2.pdf', response.body);

                response.body.toString(fs.createWriteStream('some3.pdf'));

                fs.writeFile('som4.pdf', response.body);

 

                res.status(200).json({

                    "status": true,

                    "data": response.body,

                    "message": ""

                });

                

            }

        });

    }

    catch (error) {

        next(error);

    }

}

 

 


Attachments

Reply to:

1 votes

Try this instead:
 

        var options = {

            'method': 'GET',

            'url': 'https://sandbox.esignlive.com/api/packages/wceFulLHCY4YhOzaOzokFjlsNF4=/documents/3de161e2d4ddad1eed436d84614962c5f7e2b0918b2937e3/pdf',

            'headers': {

              'Authorization': 'Basic QVRRxxxVA4TA==',

              'Accept': 'application/pdf'

            }

          };

 

            request(options).on('response', r => {
                ......
            }).pipe(fs.createWriteStream("/temp/my2.pdf"));

      
 

Duo Liang OneSpan Evangelism and Partner Integrations Developer


Reply to: How to download PDF document in node js

1 votes

Hey Duo,

 

One more query , how to get response in buffer instead of .pipe(fs.createWriteStream("/temp/my2.pdf"))

because I need to upload that file in AWS also.


Reply to: How to download PDF document in node js

0 votes

Hey Duo,

 

Any update on this?


Reply to: How to download PDF document in node js

0 votes

Hey Duo,

When I upload that file in AWS that time uploaded file shows password encrypted message.

 

 let package_id = req.params.id;

        console.log(package_id)

        var request = require('request');

        var options = {

            'method': 'GET',

            'url': 'https://sandbox.esignlive.com/api/packages/' + package_id + '/documents/6bdedd15e909947a600/pdf',

            'headers': {

                'Authorization': 'Basic xxxxxx==',

                'Accept': 'application/pdf'

            }

        };
 

        request(options, function (error, response) {

            if (error) throw new Error(error);

            else {

                UploadFile(Buffer.from(response.body, 'utf8'), "TestManish05may.pdf");

            }

        });

 

 

Please suggest me what i am doing wrong.


Attachments

Reply to:

0 votes

Hi Manish,

 

I think you should still use the stream.pipe() function to process the request response by passing in a writable stream storing the data to memory buffer, see below:    

 

            var bufs = [];
            var stream = require('stream');
            const writableStream = new stream.Writable()
            writableStream._write = (chunk, encoding, next) => {
                bufs.push(Buffer.from(chunk))
                next()
            }

            var stream = request(options).on('response', r => {
                console.log('Response received: ' + r);
                response = r;
             }).pipe(writableStream);


            stream.on('finish', function(){
                var b = Buffer.concat(bufs);    //b with type of Buffer
                //upload to AWS
            });

 

Duo

Duo Liang OneSpan Evangelism and Partner Integrations Developer


Reply to: How to download PDF document in node js

0 votes

Due Thank you so much!!

Its working.


Hello! Looks like you're enjoying the discussion, but haven't signed up for an account.

When you create an account, we remember exactly what you've read, so you always come right back where you left off