export firebase data node to pdf report

Viewed 1527

What a have is a mobile app emergency message system which uses Firebase as a backend. When the end of an emergency event ends, I would like to capture the message log in a pdf document. I have not been able to find any report editors that work with Firebase. This means I may have to export this to php mysql. The Firebase php SDK looks to be to much overkill for this task. I have been googling php get from firebase and most responses have to do with using the Firebase php SDK. Is this the only way it can be acomplished?

1 Answers

You could use PDF Kit (...) on Cloud Functions (it's all nodeJS, no PHP available there).

On npmjs.com there are several packages for @firebase-ops, googleapis and @google-cloud.

In order to read from Firebase and write to Storage Bucket or Data Store; that example script would still require a database reference and a storage destination, to render the PDF content (eventually from a template) and puts it, where it belongs. also see firebase / functions-samples (especially the package.json which defines the dependencies). npm install -g firebase-tools installs the tools required for deployment; also the requires need to be installed in order to be locally known (quite alike composer - while remotely these are made known while the deployment process).

You'd need a) Firebase Event onUpdate() as the trigger, b) check the endTime of the returned DeltaSnapshot for a value and c) then render & store the PDF document. the code may vary, just to provide a coarse idea of how it works, within the given environment:

'use strict';

const admin = require('firebase-admin');
const functions = require('firebase-functions');
const PDFDocument = require('pdfkit');
const gcs = require('@google-cloud/storage')();
const bucket = gcs.bucket( 'some-bucket' );
const fs = require('fs');

// TODO: obtain a handle to the delta snapshot

// TODO: render the report
var pdf = new PDFDocument({
    size: 'A4', 
    info: {Title: 'Tile of File', Author: 'Author'}
});
pdf.text('Emergency Incident Report');

pdf.pipe(
    // TODO: figure out how / where to store the file
    fs.createWriteStream( './path/to/file.pdf' )
).on('finish', function () {
    console.log('PDF closed');
});

pdf.end();

externally running PHP code is in this case nevertheless not run on the server-side. the problem with it is, that an external server won't deliver any realtime trigger and therefore the file will not appear instantly, upon time-stamp update (as one would expect it from a Realtime Database). one could also add external web-hooks (or interface them with PHP), eg. to obtain these PDF files through HTTPS (or even generated upon HTTPS request, for externally triggered generation). for local testing one can use command firebase serve, saves much time vs. firebase deploy.

the point is, that one can teach Cloud Function how the PDF files shall look alike, when they shall be created and where to put them, as micro-service which does nothing else but to render these files. scripting one script should be still within acceptable range, given all the clues provided.

Related