Puppeteer PDF Creation on Firebase Functions (Google Cloud) times out

juliusphysics

I have a function on Firebase to create a PDF file which times out every time. To investigate the issue I added debug logs with numbers. The source code of the function that I run is:

const createPDF = async (html, outputPath) => {

    console.log(1);
    let pdf;

    try {
        console.log(2);

        const browser = await puppeteer.launch();
        console.log(3);

        const page = await browser.newPage();
        console.log(4);

        await page.emulateMediaType('screen');

        console.log(5);
        await page.setContent(html, {
            waitUntil: 'networkidle0'
        });

        console.log(6);
        pdf = await page.pdf({
           // path: outputPath,
            format: 'A4',
            printBackground: true,
            margin: {
                top: "50px",
                bottom: "50px"
            }
        });

        console.log(7);
        await browser.close();

    } catch (e) {
        console.error(e);
    }

    console.log(8);
    return pdf;
};

The log says:

10:58:12.490 AM 1
10:58:12.492 AM 2
10:58:16.469 AM 3
10:58:31.236 AM Function execution took 20003 ms, finished with status: 'timeout' 

The script works, when I deploy it locally. What did I do wrong?

vitooh

I think launch should be used with argument { args: ['--no-sandbox'] }. I have found example with this approach here.

I created test combining provided code and helloworld cloud function, and indeed in always ended with timeout after 3. However when I used above argument it started to work fine. I tested it with 256MiB and 30 sec timeout.

Working code:

const puppeteer = require('puppeteer');
 
exports.helloWorld = async (req, res) => {
  console.log(2);

  const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
  console.log(3);

  const page = await browser.newPage();
  console.log(4);
  
  let message = req.query.message || req.body.message || 'Hello World!';
  res.status(200).send(message);
};

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Image Moderation always times out on Google Firebase cloud functions?

Firebase Functions times out when using puppeteer's browser.newPage()

running puppeteer code in firebase cloud functions

Create PDF in Firebase Cloud Functions

Firebase cloud functions not triggering on user creation

Cloud Functions for Firebase invoked many times

google firebase cloud functions + firestore

Google Cloud Build of Angularfire project times out

Puppeteer wait for page and setTimeout not working in Firebase cloud functions

Using Firebase Cloud Functions to fan out data

Post Request Firebase Cloud Functions Timing Out

Is it safe to use Google cloud functions and puppeteer for user code sandbox?

How to send email verification after user creation with Firebase Cloud functions?

Firebase Cloud Functions V2: A user creation trigger

Can you trigger (firebase) cloud functions on auth login (but not account creation)?

Cloud Functions times out when calling an external HTTP with a POST request

Firebase/Google cloud functions - is it possible to hook into cloud functions error event?

Cloud Function writing to Firebase Realtime Database times out

How to generate pdf with pdfmake and store it at firebase storage with firebase cloud functions

index creation times out with ProcessClusterEventTimeoutException

Accessing Firebase admin from google cloud functions

Receiving Emails with Google Cloud Functions in Firebase

Rate limiting for Google/Firebase cloud functions?

Understanding the Firebase and purpose of google cloud functions

Fetching data from Firebase - Google Cloud Functions

Google Cloud Function HTTP request connection times out

Google Cloud Build via cloudbuild.yaml times out randomly

google cloud function times out, apparently during database access

Firebase cloud functions -@google-cloud/storage initialization