Reputation: 145
How to get base64 string instead the blob pdf encoding? My approach does not work and I get the pdf blob in frontend instead base64.
const pdfName = "cv123.pdf";
const pdfPath = path.join('data', pdfName)
const pdfDoc = new PDFDocument();
pdfDoc.pipe(fs.createWriteStream(pdfPath, {encoding: "base64"}));
pdfDoc.pipe(res)
pdfDoc.text("TEST").fontSize(20)
pdfDoc.end();
Upvotes: 0
Views: 571
Reputation: 145
My solution was to create read stream from the created PDF by the PDFkit, it works
const pdfName = `cv123${orderId}.pdf`;
res.setHeader("Content-Type", "application/pdf");
res.setHeader("Content-Disposition", `attachment; filename=${pdfName}`);
const pdfPath = path.join("data", pdfName);
const pdfStream = fs.createWriteStream(pdfPath);
const pdfDoc = new PDFDocument();
const writeStream = pdfDoc.pipe(pdfStream);
pdfDoc.fontSize(25).text(`Order ${orderId}`);
pdfDoc.fontSize(13).text(`User ${orderUser.name}`);
pdfDoc.end();
writeStream.on("finish", () => {
const fileStream = fs.createReadStream(pdfPath, { encoding: "base64" });
fileStream.on("data", (chunk: Buffer) => {
res.write(chunk);
});
fileStream.on("end", () => {
fs.unlink(pdfPath, () => {
res.end();
});
});
});
Upvotes: 2