Pdfmake: Example of working with PDFMake in a Firebase Cloud Function context

Created on 4 Mar 2018  路  8Comments  路  Source: bpampuch/pdfmake

Right now, I'm using pdfkit to generate a PDF and write it to Firebase Storage in a Firebase Cloud Function. But pdfkit is really limited and it's imperative. I would love to be able to use pdfmake's declarative syntax and table support but I can't figure out how to use it properly in a Firebase Cloud Function context and in conjunction with Firebase Storage. The only element I could find is this question on StackOverflow, and it doesn't say a lot about storing the document.

Would it be possible to add a small example showing how this would work?

Most helpful comment

Hi Keleabens,
You can find a full example here https://medium.com/@luigibynight/generate-a-pdf-with-google-cloud-functions-e153c815be5d

I've tried the following steps and it works.

  • create a folder name 'fonts' that contains the 4 font files which are Roboto-Regular.ttf, Roboto-Italic.ttf, Roboto-BoldItalic.ttf and Roboto-Bold.ttf
  • put the folder 'fonts' in the folder 'functions' (generated by using firebase CLI command when you want to use firebase's cloud function)
  • add following code in the index.js file found in folder 'functions'

const functions = require('firebase-functions');
const {Storage} = require('@google-cloud/storage');
const BUCKET = 'gs://xxxxxxx.appspot.com'; //link found in Storage from Firebase console

// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp();

const PdfPrinter = require('pdfmake/src/printer');

function createPDF() {
// Return a new promise.
return new Promise(function(resolve, reject) {
let pdfCreation = false;
var docDefinition = {
pageSize: 'A4',
pageMargins: [ 40, 60, 40, 60 ],
content: [
'First paragraph',
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
]
};
const fontDescriptors = {
Roboto: {
normal: './fonts/Roboto-Regular.ttf',
bold: './fonts/Roboto-Bold.ttf',
italics: './fonts/Roboto-Italic.ttf',
bolditalics: './fonts/Roboto-BoldItalic.ttf',
}
};
const printer = new PdfPrinter(fontDescriptors);
const pdfDoc = printer.createPdfKitDocument(docDefinition);
const storage = new Storage({
projectId: 'projectIdName', //found in firebase console: click on Setting then on Project parameters
});
let file_name = 'facture.pdf';
const myPdfFile = storage.bucket(BUCKET).file(file_name);

pdfDoc.pipe(myPdfFile.createWriteStream())
.on('finish', function (){
  console.log('Pdf successfully created!');
  resolve(file_name);
})
.on('error', function(err){
  console.log('Error during the wirtestream operation in the new file');
  reject('Error: something goes wrong ! '+ err);
});

pdfDoc.end();

}
);
}

//following test function will be triggered when you add a file in storage. You can add a file manually from firebase console: select Storage then click on 'import a file'
exports.generatePdf = functions.storage.object().onFinalize((object) => {
return createPDF()
.then(function(file_name){
console.log("The request was successfully authorized and pdf generated.n You can find your pdf in the cloud storage " + file_name);
})
.catch( function(error) {
console.error("Failed!" + error);

});

All 8 comments

I do not know much about Firebase. But pdfmake is builded on pdfkit so it should do the same.

This generates a PDF document:

var fonts = {
    Roboto: {
        normal: 'fonts/Roboto-Regular.ttf',
        bold: 'fonts/Roboto-Medium.ttf',
        italics: 'fonts/Roboto-Italic.ttf',
        bolditalics: 'fonts/Roboto-MediumItalic.ttf'
    }
};

var PdfPrinter = require('pdfmake');
var printer = new PdfPrinter(fonts);

var docDefinition = {
    content: [
        'First paragraph',
        'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
    ]
};

var pdfDoc = printer.createPdfKitDocument(docDefinition);

And in variable pdfDoc is same object as returns pdfkit, therefore, acquisition of pdf document should be same as in pdfkit.

Is there any example of printing images from Firebase Storage on PDF?

Hi guys, I follow the above guide from liborm85. However when I deploy the functions, I get following error 'no such file or directory'. I've tried to put the fonts folder, which contains the 4 font files, in different places (in root, in functions folder or in node_modules) but I always get the same error! Did I miss something?

@haitrungpham I'm running into the same issue right now. Did you ever figure this out?

Hi Keleabens,
You can find a full example here https://medium.com/@luigibynight/generate-a-pdf-with-google-cloud-functions-e153c815be5d

I've tried the following steps and it works.

  • create a folder name 'fonts' that contains the 4 font files which are Roboto-Regular.ttf, Roboto-Italic.ttf, Roboto-BoldItalic.ttf and Roboto-Bold.ttf
  • put the folder 'fonts' in the folder 'functions' (generated by using firebase CLI command when you want to use firebase's cloud function)
  • add following code in the index.js file found in folder 'functions'

const functions = require('firebase-functions');
const {Storage} = require('@google-cloud/storage');
const BUCKET = 'gs://xxxxxxx.appspot.com'; //link found in Storage from Firebase console

// Import and initialize the Firebase Admin SDK.
const admin = require('firebase-admin');
admin.initializeApp();

const PdfPrinter = require('pdfmake/src/printer');

function createPDF() {
// Return a new promise.
return new Promise(function(resolve, reject) {
let pdfCreation = false;
var docDefinition = {
pageSize: 'A4',
pageMargins: [ 40, 60, 40, 60 ],
content: [
'First paragraph',
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
]
};
const fontDescriptors = {
Roboto: {
normal: './fonts/Roboto-Regular.ttf',
bold: './fonts/Roboto-Bold.ttf',
italics: './fonts/Roboto-Italic.ttf',
bolditalics: './fonts/Roboto-BoldItalic.ttf',
}
};
const printer = new PdfPrinter(fontDescriptors);
const pdfDoc = printer.createPdfKitDocument(docDefinition);
const storage = new Storage({
projectId: 'projectIdName', //found in firebase console: click on Setting then on Project parameters
});
let file_name = 'facture.pdf';
const myPdfFile = storage.bucket(BUCKET).file(file_name);

pdfDoc.pipe(myPdfFile.createWriteStream())
.on('finish', function (){
  console.log('Pdf successfully created!');
  resolve(file_name);
})
.on('error', function(err){
  console.log('Error during the wirtestream operation in the new file');
  reject('Error: something goes wrong ! '+ err);
});

pdfDoc.end();

}
);
}

//following test function will be triggered when you add a file in storage. You can add a file manually from firebase console: select Storage then click on 'import a file'
exports.generatePdf = functions.storage.object().onFinalize((object) => {
return createPDF()
.then(function(file_name){
console.log("The request was successfully authorized and pdf generated.n You can find your pdf in the cloud storage " + file_name);
})
.catch( function(error) {
console.error("Failed!" + error);

});

Hello All, Trying to get the example to work but getting
Failed!TypeError: Cannot read property 'advanceWidth' of null
Any idea how to resolve this ??

Hi guys, I follow the above guide from liborm85. However when I deploy the functions, I get following error 'no such file or directory'. I've tried to put the fonts folder, which contains the 4 font files, in different places (in root, in functions folder or in node_modules) but I always get the same error! Did I miss something?

Not sure if this helps but I ended up using the inbuilt fonts to get past this problem.

https://pdfmake.github.io/docs/fonts/standard-14-fonts/

Is there any example of printing images from Firebase Storage on PDF?

@szymont did you find the solutions

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ValeSauer picture ValeSauer  路  3Comments

davidyeiser picture davidyeiser  路  3Comments

kamilkp picture kamilkp  路  3Comments

kumarandena picture kumarandena  路  3Comments

jokris1 picture jokris1  路  3Comments