1

I'm learning about puppeteer and firebase at the moment. What I am trying to do is create a pdf of a web page and upload to firebase storage. This is my code.

const puppeteer = require('puppeteer');
const fs = require('fs').promises;
const firebase = require('firebase');
require("firebase/storage");

const url = process.argv[2];
if (!url) {
    throw "Please provide URL as a first argument";
}

var firebaseConfig = {
        #Firebase Config Goes here
  };
  // Initialize Firebase
  firebase.initializeApp(firebaseConfig);


#Function to generate PDF file
async function run () {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();

    //await page.goto(url);
    await page.goto(url, {waitUntil: 'domcontentloaded', timeout: 60000} );

    //await page.pdf({ path: 'api.pdf', format: 'A4' })

    const myPdf = await page.pdf();
    
     await browser.close()

     return myPdf;
}

const myOutput = run();

#Upload to Firebase based on the instruction here https://firebase.google.com/docs/storage/web/upload-files
 
var storageRef = firebase.storage().ref();
// Create a reference to 'mountains.jpg'
storageRef.child(myOutput).put(myOutput)

However, I'm running into this error when executing my code

$ node screenshot.js https://en.wikipedia.org/wiki/Aung_San_Suu_Kyi
C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:1040
        .split('/')
         ^

TypeError: childPath.split is not a function
    at child (\Test\node_modules\@firebase\storage\dist\index.cjs.js:1040:10)
    at getChild (\Test\node_modules\@firebase\storage\dist\index.cjs.js:2610:19)
    at ReferenceCompat.child (Test\node_modules\@firebase\storage\dist\index.cjs.js:2833:25)
    at Object.<anonymous> (C:\Users\ppham\NucampFolder\Test\screenshot.js:55:12)
    at Module._compile (internal/modules/cjs/loader.js:936:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:947:10)
    at Module.load (internal/modules/cjs/loader.js:790:32)
    at Function.Module._load (internal/modules/cjs/loader.js:703:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:999:10)
    at internal/main/run_main_module.js:17:11

It is saying that childPath.split is not a function. I'm pretty confused at this since I've already installed all the firebase packages. I've been searching for this error for a while but no luck so far. Anyone knows how to resolve this issue?

===============================================================================

Edit #1: As Frank pointed out below, I have to change storageRef.child(myOutput).put(myOutput) to something like storageRef.child("filename.pdf").put(myOutput). It runs but I'm running into this error afterwards.

$ node screenshot.js https://google.com
Promise { <pending> }
(node:20636) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'byteLength' of undefined
    at C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:833:40
    at Array.forEach (<anonymous>)
    at Function.FbsBlob.getBlob (C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:832:25)
    at multipartUpload (C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:1519:24)
    at C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:2003:31
    at C:\Users\ppham\NucampFolder\Test\node_modules\@firebase\storage\dist\index.cjs.js:1900:21
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
(node:20636) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:20636) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

This looks to imply that myOutput doesn't contain anything. I thought that I have return the pdf in the run function and it will be passed over to the upload function? I've been reading the Puppeteer documentation and couldn't find any reason why this wouldn't work. Anyone knows why this is not valid?

JoeP_108
  • 53
  • 5

1 Answers1

2

The error message is saying that the value you're passing to child() is not a string.

Since you're calling:

storageRef.child(myOutput).put(myOutput)

That kind'of makes sense. My guess is that you want to pass a file name into child(), like:

storageRef.child("filename.pdf").put(myOutput)
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thank you. That looks to be the reason. But I'm running into this issue afterwards. I'll update the post with it. – JoeP_108 Feb 04 '21 at 13:31
  • If you have a new/different problem after making this change, please open a new question. – Frank van Puffelen Feb 04 '21 at 15:21
  • Thanks Frank. I've created a new question here - https://stackoverflow.com/questions/66048849/trouble-with-generating-pdf-file-to-upload-to-firebase – JoeP_108 Feb 04 '21 at 15:37