user1184205 user1184205 - 3 years ago 269
Javascript Question

Create PDF in Firebase Cloud Functions

I am new to javascript and I'm trying to make a PDF file from a firebase function using pdfkit. Below is my function code.

const pdfkit = require('pdfkit');
const fs = require('fs');

exports.PDFTest = functions.https.onRequest((req, res) => {

var doc = new pdfkit();

var loremIpsum = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam in...';

doc.y = 320;
doc.text(loremIpsum, {
paragraphGap: 10,
indent: 20,
align: 'justify',
columns: 2

doc.pipe( res.status(200) )


The function starts but then a timeout error happens.
Is this the best way of going about creating a pdf file in firebase?
I have some html that I want made into a pdf file.

Answer Source

Just working on this too, for saving the PDF in the storage it works like this

const myPdfFile ='/test/Arbeitsvertrag.pdf');
const doc = new pdfkit();
const stream = doc.pipe(myPdfFile.createWriteStream());
doc.fontSize(25).text('Test 4 PDF!', 100, 100);

return res.status(200).send();

Guess you should wait until the stream is closed and listen for Errors and Things, but this is the first working example I was able to make, now working on how to get a Image from the storage into the PDF.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download