Please share node js code to download signed document.
May 6Created
May 6Last Updated
3 years agoLast Reply
9Replies
4444Views
2Users
0Likes
0Links
Duo_Liang | Posts: 3776
Reply to: How to download PDF document in node js
Thursday, May 6, 2021 at 07:40am
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.
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
});
Reply to: How to download PDF document in node js
Thursday, May 6, 2021 at 07:40amHi 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
Reply to: How to download PDF document in node js
Friday, May 7, 2021 at 12:16amThanks 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);
}
}
Reply to: Thanks Duo for your response…
Friday, May 7, 2021 at 05:41amTry 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"));
Reply to: Try this instead: …
Friday, May 7, 2021 at 06:33amThank you!!
It's working now.
Reply to: How to download PDF document in node js
Friday, May 7, 2021 at 07:37amHey 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
Monday, May 10, 2021 at 01:50amHey Duo,
Any update on this?
Reply to: How to download PDF document in node js
Monday, May 10, 2021 at 06:10amHey 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.
Reply to: Hey Duo, When I upload that…
Monday, May 10, 2021 at 08:30amHi 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
Reply to: How to download PDF document in node js
Monday, May 10, 2021 at 09:05amDue Thank you so much!!
Its working.