生成PDF文件以上传到Firebase时遇到问题 [英] Trouble with generating PDF file to upload to firebase

查看:80
本文介绍了生成PDF文件以上传到Firebase时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此刻,我正在学习操纵p和火力地堡.我正在尝试做的是创建网页pdf,然后上传到Firebase存储.这是我的代码.

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.

这似乎暗示myOutput不包含任何内容.我以为我已经在执行run()函数之后创建了pdf文件,将其分配给myOutput变量并将其传递给了上载函数?我一直在阅读Puppeteer文档,找不到任何无法解决该问题的原因.有人知道为什么这无效吗?

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?

推荐答案

您的代码与 https://www.toptal.com/puppeteer/headless-browser-puppeteer-tutorial 很小,但是有一个显着的区别:

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 被定义为 async 函数-在示例代码中,这可能是无关紧要的,因为示例的最后一行仅调用 run(),此后没有任何反应.但是,在您的代码中,您希望对该输出执行某些操作.因此,您需要使用以下命令来调用它:

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 =等待run();

但是,Node不想让您在顶层使用 await - await只能在 async 函数内使用.因此,您可以使用 then()或仅定义另一个异步包装器.因此,大概是这样的:

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();

这篇关于生成PDF文件以上传到Firebase时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆