我正在尝试将pdf转换为base64并作为附件发送到电子邮件,但我无法转换为base64而不是创建一个文件,我想将其转换为base64,以便我可以作为附件发送。这里是代码
const fs = require("fs");
const path = require("path");
const utils = require("util");
const puppeteer = require("puppeteer");
const hb = require("handlebars");
const readFile = utils.promisify(fs.readFile);
(async () => {
const A = "invoice";
const htmlContent = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h2>Approve Page ,${A}</h2>
</body>
</html>
`;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
await page.pdf({ path: "html.pdf", format: "A4" });
await browser.close();
})(); …Run Code Online (Sandbox Code Playgroud) 我需要读取一个文件夹下的多个 json 文件。数据文件路径如下:
./data/json1.json
./data/json2.json
Run Code Online (Sandbox Code Playgroud)
我的初始化类的工作原理如下:
const j1 = require('./data/json1.json');
const j2 = require('./data/json2.json');
init(){
return j1.concat(j2);
}
Run Code Online (Sandbox Code Playgroud)
有没有更好的方法来执行此操作,因为数据下的文件列表可能会增加并且每次都需要修改?
我最好避免在文件夹中循环并读取文件以附加到init().
我正在尝试使用整页 PDF puppeteer,我的代码是这样的,但结果是多个页面的高度等于页面的高度。
我如何解决这个问题?TIA。
const puppeteer = require("puppeteer");
function sleep(ms) {
return new Promise(resolve => {
setTimeout(resolve, ms);
});
}
(async () => {
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto("http://localhost:3000/longpage", {
waitUntil: "networkidle2"
});
let height = await page.evaluate(
() => document.documentElement.offsetHeight
);
console.log("Height", height);
await page.pdf({
path: "hni.pdf",
printBackground: true,
margin: "none",
height: height + "px"
});
await browser.close();
})();
Run Code Online (Sandbox Code Playgroud) 我每天需要打开大约 100,000 个 URL,以便图像和 html 缓存到 Cloudflare 中,因为内容变化相当频繁。
我怀疑 Curl 可能会比无头浏览器执行得更快(通过 puppeteer 无头浏览器)
有没有人有这方面的经验或有更好的方法吗?
我在使用 puppeteer 抓取用户时间轴上的所有推文 URL 时遇到问题。
使用 puppeteer,脚本应该在函数中 while 循环的每次迭代中向下滚动时间线,scrollToEnd直到到达底部。为了监视进度,我让脚本输出变量的值,该值是每次执行滚动之前评估的previousHeight当前值。scrollheightdocument.body
然而,一旦输出值变为 285,834,滚动就会停止。令人费解的是,该脚本既没有跳出 while 循环,也没有page.waitForFunction抛出超时错误。
我应该如何重写scrollToEnd函数或脚本的任何其他部分以便函数正确结束?
这是我的代码片段。为了简洁起见,省略了不相关的功能。
const puppeteer = require('puppeteer');
var UserUrls = ['https://twitter.com/someuser'];
// more functions here
async function scrollToEnd(
page,
ScrollDelay = 1000
) {
try {
let previousHeight = 0;
let notEnd = await page.waitForFunction(`document.body.scrollHeight > ${previousHeight}`);
while (notEnd) {
previousHeight = await page.evaluate('document.body.scrollHeight');
await page.evaluate('window.scrollBy(0, document.body.scrollHeight)');
await page.waitFor(ScrollDelay);
notEnd = await page.waitForFunction(`document.body.scrollHeight > ${previousHeight}`);
console.log(previousHeight)
};
return; …Run Code Online (Sandbox Code Playgroud) const browser = await puppeteer.launch({
args: ['--incognito', '--aggressive-cache-discard'],
headless: false
});
const context = await browser.createIncognitoBrowserContext();
const page = await context.newPage();
Run Code Online (Sandbox Code Playgroud)
你好呀,
当上面的代码运行时,将创建两个浏览器会话,请参见下面的屏幕截图。
我想知道是否可以不创建新页面()并使用浏览器启动的第一个页面?
因为当我出于某种奇怪的原因secureConnectionStart打电话时,第二个浏览器总是返回 0 。window.performance.timing