puppeteer生成pdf加载动态网络图片

通过puppeteer无头浏览器生成pdf但是需要动态加载传入的图片url时,可以通过axios下载图片,再转base64展示

1
2
3
4
5
6
7
8
9
10
11
12
const fetchBase64Img = async (url) => {
const arrayBuffer = await axios.get(url, {
responseType: 'arraybuffer'
})
const buffer = Buffer.from(arrayBuffer.data, 'binary').toString("base64");
return `data:${arrayBuffer.headers["content-type"]};base64,${buffer}`;
}


// puppeteer渲染的dom中可以直接通过img展示
const imgBase64 = await fetchBase64Img(url)
`<img src="${imgBase64}">`