onClick of a button I'm trying to GET a generated PDF (by Pdfmake library) file from server, although I'm getting really long base64 codes in preview & response section of chrome dev console like --
JVBERi0xLjMKJf////8KNyAwIG9iago8PAovVHlwZSAvRXh0R1N0YXRlCi9jYSAxCj4+CmVuZG9iago4IDAgb2JqCjw8Ci9UeXBlIC9FeHRHU3RhdGUKL2NhIDEKL0NBIDEKPj4KZW5kb2JqCjExIDAgKL0luZm8gMTMgMCBSCi9JRCBbPGIzZjEyYjhkYWYwN2Q4ZWZkZjFjYTkzOWE3YTQ2NjZiPiA8YjNmMTJiOGRhZjA3ZDhlZmRmMWNhOTM5YTdhNDY2NmI+XQo+PgpzdGFydHhyZWYKMjA5Nj
. ............very long....
I'm expecting to get the pdf on new browser window tab,
after manually typing this base64 code prefixed with data:application/pdf;base64,
then its showing the pdf, but I need it to popout on a new tab as soon as onClick occurs...
data:application/pdf;base64,JVBERi0xLjMKJf////8KNyAwIG9iago8PAovVHlwZSAvRXh0R1N0YXRlCi9jYSAxCj4+CmVuZG9iago4IDAgb2JqCjw8Ci9UeXBlIC9FeHRHU3RhdGUKL2NhIDEKL0NBIDEKPj4KZW5kb2JqCjExIDAgb2JqCjw8Ci9UeXBlIC9FeHRHU3RhdGUKL0NBIDEKPj4KZW5kb2JqgKI189+A10wPsfByPuXwnyXtcO17zDJBaRJTFEFLPpHqYmHEniQ7Wjpe/E/iXwbhHf8B6XIZX4gskibxEvuIM8G8E9zIfwk/kt/OfCYXCcuFT4YxqlOp11Tl1sVrSxGlqNa9pftQO1oo
Please answer as easy & detailed as possible, I have no idea how stream, buffer, base64, filesystem everything works and how to work around with, I'm new to the development.
Client Side --->
handleOrderInvoice(event) {
const orderid = event.currentTarget.id;
fetch(`/api/payment/order/${orderid}`, {
method: "GET",
headers: {
Accept: "application/pdf",
Authorization: localStorage.getItem("bearerToken")
}
})
.then(response => {
//HOW to work with pdf response???
})
.catch(err => console.log(`Error in fetching this order. -- ${err}`));
Server side (Express) -->
router.get(
"/order/:orderid",
passport.authenticate("jwt", { session: false }),
(req, res) => {
//Create PDF Document
const pdfDoc = pdfMake.createPdf(documentDefinition);
pdfDoc.getBase64(data => {
res.writeHead(200, {
"Content-Type": "application/pdf",
"content-Disposition": "inline; filename='invoice.pdf'"
});
const doc = Buffer.from(data.toString("utf-8"), "base64");
res.end(doc);
});
})
.catch(err =>
console.log(`Error in fetching user order response. -- ${err}`)
);
}
);