I have the following code in cloud functions that triggers whenever the user make an order. Then I want to generate an invoice in pdf using firebase storage and then email the pdf to the user. I have a hard time to get the pdf generate as I am not sure exactly how I should be doing it. The email gets send with no problem to the user once the order is placed. I know how to make an attachment in the mailoptions below but I cant figure out how to create the pdf and insert the product info.
// Triggers once the user place an order
exports.notifyUserOfOrderConfirmation = functions.firestore.document('masterOrders/{masterOrderId}').onCreate((snap, context) => {
const data = snap.data();
const orderedUserEmail = data.userEmail;
const customerOrderId = data.customerOrderId;
const userId = data.userId;
const masterOrderId = context.params.masterOrderId;
return orderProductsQuery(data, masterOrderId, orderedUserEmail, customerOrderId, userId)
});
// Query the firestore to get the ordered products
function orderProductsQuery(data, masterOrderId, orderedUserEmail, customerOrderId, userId) {
return db.collection('users').doc(userId).collection('orders').doc(customerOrderId).collection('orderCart').get().then(snap => {
const orderedProducts = [];
snap.forEach(doc => {
const productData = doc.data();
orderedProducts.push(productData);
})
// return notifyUserOfOrderConfirmation(data, masterOrderId, orderedUserEmail, customerOrderId, orderedProducts)
return generatePDF(data, masterOrderId, orderedUserEmail, customerOrderId, orderedProducts)
}).catch((err) => {
console.log('Error getting documents', err);
return Promise.reject(err);
})
}
This is where I need help with; below code is what I have written so far:
const pdfkit = require("pdfkit");
const Storage = require('@google-cloud/storage');
// Creates a client
const storage = new Storage ({projectId: MYPROJECTID});
// Lists all buckets in the current project
const buckets = storage.getBuckets();
function generatePDF(data, masterOrderId, orderedUserEmail, customerOrderId, orderedProducts) {
const doc = new pdfkit();
const filename = `/${customerOrderId}/test-` + Date.now() + '.pdf';
const bucket = storage.bucket(MYBUCKETNAME)
const file = bucket.file(filename);
const bucketFileStream = file.createWriteStream();
doc.pipe(bucketFileStream);
doc.end();
bucketFileStream.on('finish', function () {
return notifyUserOfOrderConfirmation(filename, data, masterOrderId, orderedUserEmail, customerOrderId, orderedProducts)
});
bucketFileStream.on("error", function (err) {
console.error(err);
});
}
And here is when I send the email; I know this code below works just fine without the pdf codes so there is no issue with sending emails without pdf:
async function notifyUserOfOrderConfirmation(filename, data, masterOrderId, orderedUserEmail, customerOrderId, orderedProducts) {
const bucket = storage.bucket(MYBUCKETNAME);
const file = bucket.file(filename);
const mailOptions = {
from: `${APP_NAME} <MYEMAIL>`,
to: orderedUserEmail
};
mailOptions.subject = `Order Confirmation`;
mailOptions.text = `We received your order. attached is your invoice`;
mailOptions.attachments = [{
filename: "test.pdf",
content: file.createReadStream()
}];
await transporter.sendMail(mailOptions);
console.log('New welcome email sent to:', orderedUserEmail);
return null;
}
When I deploy this; I get no error in my deployment. But when I place an order in the app; I get the following error in the functions of firebase:
Error: socket hang up
at TLSSocket.onHangUp (_tls_wrap.js:1148:19)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
at TLSSocket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
Unhandled rejection
Any idea what is the problem here and I how I can generate the pdf correctly in this scenario?