javascript - 无法生成 PDF 文件以上传到 Firebase
问题描述
我现在正在学习 puppeteer 和 firebase。我想做的是创建一个网页的pdf并上传到firebase存储。这是我的代码。
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)
但是,我在执行代码时遇到了这个错误
$ 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 文档,但找不到任何不可行的原因。任何人都知道为什么这是无效的?
解决方案
例如,您的代码与https://www.toptal.com/puppeteer/headless-browser-puppeteer-tutorial上的示例代码之间的差异微不足道,但有一个显着差异:
run
被定义为一个async
函数——在示例代码中,这可能无关紧要,因为示例的最后一行只是调用run()
,之后没有任何反应。但是,在您的代码中,您希望对该输出做一些事情。因此,您需要使用以下命令调用它:
const myOutput = await run();
但是,Node 不希望您await
在顶层使用 - await 只能在async
函数内部使用。因此,您可以使用then()
或只定义另一个异步包装器。所以,大概是这样的:
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();
推荐阅读
- android - StackOverflowError 导致 ConstraintLayout 崩溃
- javascript - 如何使用geofirestore查询设置半径内的所有地理查询?
- android - 获取 java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 同时从 firebaseui recyclerview 删除项目
- html - 背景图像在手机上显示奇怪
- windows - 无法找到用于杀戮的 .bat pid
- python - pandas 按另一列的平均值对一列的值进行排序
- android-studio - 如何更改文本编辑器中默认文本的显示方式
- python - 在python中对数据进行排序,忽略csv中的标头
- powerbi - 日间变化计算不正确
- dialogflow-es - 将对话流意图和实体导入 IBM 对话工作区