2

We've created a Cloud Function that generates a PDF. The library that we're using is

https://www.npmjs.com/package/html-pdf

The problem is when we try to execute the

.create() 

method it times out with the following errors

"Error: html-pdf: PDF generation timeout. Phantom.js script did not exit.
    at Timeout.execTimeout (/srv/node_modules/html-pdf/lib/pdf.js:91:19)
    at ontimeout (timers.js:498:11)

This works fine on localhost but happens when we deploy the function on GCP.

Some solutions we've already tried:

Solution #1 Yes we've updated the timeout settings to

const options = {
        format: "A3",
        orientation: "portrait",
        timeout: "100000"
        // zoomFactor: "0.5"

        // orientation: "portrait"
      };

and it still doesn't work.

here's the final snippet that triggers the PDF function

const options = {
        format: "A3",
        orientation: "portrait",
        timeout: "100000"
        // zoomFactor: "0.5"

        // orientation: "portrait"
      };
      try {
        // let pdfRes = await new Promise(async (resolve, reject) => {
        console.log("Before pdf.create()")

        let pdfResponse = await pdf.create(html, options).toFile(localPDFFile, async function (err, res) {
          if (err) {
            console.log(err)
          }

          console.log('response of pdf.create(): ', res);
          let uploadBucket = await bucket.upload(localPDFFile, {
            metadata: { contentType: "application/octet-stream" }
          });

          let docRef = await db
            .collection("Organizations")
            .doc(context.params.orgId)
            .collection("regulations")
            .doc(context.params.regulationId)
            .collection("reports")
            .doc(context.params.reportId);

          await docRef.update({
            pdf: {
              status: "created",
              reportName: pdfName
            }
          });
        });
 } catch (error) {
        console.log('error: ', error);
      }
``

Sumair Baloch
  • 445
  • 3
  • 15
  • On the [best pratices doc](https://cloud.google.com/functions/docs/bestpractices/tips#ensure_http_functions_send_an_http_response) it's mentioned that if no response is sent back it could cause a timeout, or unpredictable behavior, this may be affecting you – Louis C Dec 10 '19 at 23:08
  • Actually we're using trigger based functions [https://firebase.google.com/docs/firestore/extend-with-functions] – Sumair Baloch Dec 11 '19 at 07:00
  • This would be the case when we're using an Http function, but not in ours. – Sumair Baloch Dec 11 '19 at 07:01

3 Answers3

1

I have seen many cases like this even in my current project we use step functions (when cloud functions needs more computational power we divide them into chunks i.e mini cloud functions). But i think step functions will not work in your case either because you are using single module. In your case you should use compute engine to perform this operation.

Vivek Anand
  • 1,264
  • 8
  • 15
1

Using promise, We can fix this timeout error

var Handlebars = require('handlebars');
var pdf = require('html-pdf');

var options = {
  height: "10.5in", // allowed units: mm, cm, in, px
  width: "8in" // allowed units: mm, cm, in, px
  "timeout": 600000
};
var document = {
  html: html1,
  path: resolvedPath + "/" + filename,
  data: {}
};


var create = function(document, options) {
return new Promise((resolve, reject) => {
    // Compiles a template
    var html = Handlebars.compile(document.html)(document.data);
    var pdfPromise = pdf.create(html, options);

    // Create PDF from html template generated by handlebars
    // Output will be PDF file
    pdfPromise.toFile(document.path, (err, res) => {
        if (!err)
            resolve(res);
        else
            reject(err);
    });
});
}
-1

This seems to be a problem with the html, my problem was that I had an image source linked to a deleted image in a server and that was what caused the time out, I solved it by putting the image in the server's route and that was it, I hope this to be useful to someone