I have an Android app and a web app (hosted on Firebase Hosting). The Android app generates data and saves it to the Firebase Realtime Database. Both the Android app and web app can view the data.
Where is the best place to generate a PDF of the data from the Firebase Database - the Android app or the web app (via JavaScript, eg. jsPDF)? It would be saved in Firebase Storage.
I thought that the Android app could call the backend to generate a PDF, and a link to the PDF would be sent back to the Android app. However Firebase is a serverless architecture, so it seems that the best solution is for the PDF to be created on the Android app, and then upload it to Firebase Storage...?
since there is Firebase Functions (Firebase Introduction ), you can use NodeJS to create PDfs for example with the pdfkit(PDFKit). The following example descirbes a way to generate an PDF on AccountCreation(Event), save it to the storage and send it to the user via mail. All this Happens on the Firebase Server. For all used Librarys dont forget to npm install them and include them to the package.json. A tricky part is to add the Firebase Admin SDK to your Server. Also, this helped a lot.
const functions = require('firebase-functions');
const admin = require("firebase-admin");
const nodemailer = require('nodemailer');
const pdfkit = require('pdfkit');
const gmailEmail = 'yourmail@stackoverflow.com'
const gmailPassword = 'test123.ThisisNoTSave'
const mailTransport = nodemailer.createTransport( `smtps://${gmailEmail}:${gmailPassword}@smtp.gmail.com`);
const serviceAccount = require("./youradminsdk.json");
//Save this file to your functions Project
// Your company name to include in the emails
const APP_NAME = 'My App';
const PROJECT_ID = "google-testproject";
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: `https://${PROJECT_ID}.firebaseio.com`,
storageBucket: `${PROJECT_ID}.appspot.com`
});
var config = {
projectId: `${PROJECT_ID}`,
keyFilename: './youradminsdk.json'
};
const storage = require('@google-cloud/storage')( config);
const datastore= require('@google-cloud/datastore')(config);
// [START onCreateTrigger]
exports.sendPDFMail = functions.auth.user().onCreate(event => {
// [END onCreateTrigger]
// [START eventAttributes]
const doc = new pdfkit;
const user = event.data; // The Firebase user.
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
// [END eventAttributes]
const bucket = storage.bucket(`${PROJECT_ID}.appspot.com`);
console.log(bucket);
const filename = Date.now() + 'output.pdf';
const file = bucket.file(filename);
const stream = file.createWriteStream({resumable: false});
// Pipe its output to the bucket
doc.pipe(stream);
doc.fontSize(25).text('Some text with an embedded font!', 100, 100);
doc.end();
stream.on('finish', function () {
return sendPDFMail(email, displayName, doc);
});
stream.on('error', function(err) {
console.log(err);
});
})
;
// [END sendPDFMail]
// Sends a welcome email to the given user.
function sendPDFMail(email, displayName, doc) {
const mailOptions = {
from: '"MyCompany" <noreply@firebase.com>',
to: email,
};
mailOptions.subject = `Welcome to ${APP_NAME}!`;
mailOptions.text = `Hey ${displayName}!, Welcome to ${APP_NAME}. I hope you will enjoy our service.`;
mailOptions.attachments = [{filename: 'meinPdf.pdf', content: doc, contentType: 'application/pdf' }];
return mailTransport.sendMail(mailOptions).then(() => {
console.log('New welcome email sent to:', email);
});
}