Trouble with generating PDF file to upload to firebase

房东的猫 提交于 2021-02-11 13:11:15

问题


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("Name.pdf").put(myOutput)

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

$ 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 created the pdf file after executing the run() function, assigned it to the myOutput variable and passed it 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?


回答1:


The difference between your code and the example code at https://www.toptal.com/puppeteer/headless-browser-puppeteer-tutorial, for example, is trivial, but there's one significant difference:

run is defined as an async function -- in the example code, this may be inconsequential, as the last line of the example just calls run() and nothing happens afterwards. But, in your code, you expect to do something with that output. So, you'll need to call it with:

const myOutput = await run();

But, Node won't want you to use await at the top level -- await can only be used inside an async function. So, you can either use then() or just define another async wrapper. So, probably something like this:

async function runAndUpload() {
    const myOutput = await run();
    console.log(myOutput); //let's make sure there's output
    var storageRef = firebase.storage().ref();
    storageRef.child("Name.pdf").put(myOutput)
}

runAndUpload();


来源:https://stackoverflow.com/questions/66048849/trouble-with-generating-pdf-file-to-upload-to-firebase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!