I have developed a function that take an one page HTML, convert it to PDF using htmp-pdf package and upload to firebase storage. When I run this function from my pc it takes less than 5 seconds, but when I run it from firebase function it takes up to 6 minutes. This is the code:
pdf.create(estadoCuenta, { orientation: 'portrait', type: 'pdf', timeout: '360000' }).toStream(function(err, stream) {
console.log('Nombre del archivo generado: ' + file.name);
stream.pipe(file.createWriteStream({
metadata: {
contentType: 'application/pdf',
metadata: {
origin: 'created by ...'
}
},
public: true,
validation: "md5"
}))
.on('error', function(err) {
console.log('error en la carga de archivo: ' + err);
})
.on('finish', function() {
// The file upload is complete.
let estadoCuentaPDF = file.name.replace('/','%2F');
console.log('estadoCuentaPDF:' + estadoCuentaPDF);
});
});
Variable "estadoCuenta" contains the html to convert.
This is my package.json:
{
"name": "dialogflowFirebaseFulfillment",
"description": "This is the default fulfillment for a Dialogflow agents using Cloud Functions for Firebase",
"version": "0.0.1",
"author": "xxx",
"repository": "https://yyy@bitbucket.org/zzz/www.git",
"license": "xxx",
"private": true,
"scripts": {
"start": "firebase serve --only functions:dialogflowFirebaseFulfillment",
"deploy": "firebase deploy --only functions:dialogflowFirebaseFulfillment"
},
"engines": {
"node": "10"
},
"dependencies": {
"-": "0.0.1",
"@google-cloud/firestore": "^3.7.5",
"actions-on-google": "^2.10.0",
"cors": "^2.8.5",
"dialogflow": "^1.2.0",
"dialogflow-fulfillment": "^0.6.1",
"dotenv": "^8.2.0",
"envfile": "^6.9.0",
"express": "^4.17.1",
"firebase-admin": "^8.0.0",
"firebase-functions": "^3.6.1",
"firebase-tools": "^7.4.0",
"html-pdf": "^2.2.0",
"isomorphic-fetch": "^2.2.1",
"node-env-file": "^0.1.8",
"nodejs-base64": "^1.0.3",
"nodemailer": "^6.3.0",
"pg": "^7.18.2",
"string-similarity": "^3.0.0",
"twilio": "^3.31.1",
"unirest": "^0.6.0"
}
}
This is because the computational resources assigned to a function can't be comparable with your local device.
The CPU speed on cloud functions is directly proportional to the Memory assigned as is mentioned in this document, please try to set up 2GB Memory to your function.
With more processing resources your function will be faster.
If the function is not responding in an acceptable time for you, the next step is to use another serverless product such as App Engine or Cloud Run that allows you to set more resources to your application.
These changes will be affect your bill.