使用Puppeteer截图+docx生成Word文档时遇TypeError错误求助
使用Puppeteer截图生成Word文档时出现TypeError错误
问题描述
尝试通过Puppeteer抓取指定URL的截图,并将截图插入到Word文档中,但运行时出现错误。截图已成功保存到本地目录,但Word文档无法创建。
错误信息
Script encountered an error: TypeError: Cannot read properties of undefined (reading 'creator') Script encountered an error: TypeError: Cannot read properties of undefined (reading 'creator') at new ta (C:\Users\upadh\OneDrive\Desktop\dynamic-urls\node_modules\docx\build\index.js:2:310449) at runScript (C:\Users\upadh\OneDrive\Desktop\dynamic-urls\server.js:47:17)
相关代码
const puppeteer = require('puppeteer'); const { Document, Packer, Media, Paragraph, TextRun } = require('docx'); const fs = require('fs-extra'); const urls = [ 'https://www.ptinews.com/press-release/pti/62401.html', 'https://www.mysmartprice.com/gear/oneplus-nord-n30-5g-geekbench-listing-specifications-revealed/', 'https://www.dnaindia.com/hindi/entertainment/bollywood/news-kailash-kher-blasts-organizers-khelo-india-university-games-2023-event-bbd-university-lucknow-4089120' ]; async function captureScreenshots() { const browser = await puppeteer.launch(); const page = await browser.newPage(); for (let i = 0; i < urls.length; i++) { const url = urls[i]; await page.goto(url); await page.screenshot({ path: `screenshot${i}.png` }); } await browser.close(); } async function createWordDocument() { const doc = new Document(); const paragraph = new Paragraph(); for (let i = 0; i < urls.length; i++) { const imagePath = `screenshot${i}.png`; const imageBuffer = fs.readFileSync(imagePath); const image = Media.addImage(doc, imageBuffer, 400, 300); paragraph.addRun(new TextRun().addImage(image)); } doc.addParagraph(paragraph); const packer = new Packer(); const buffer = await packer.toBuffer(doc); fs.writeFileSync('output.docx', buffer); console.log('Word document created successfully!'); } async function runScript() { try { await captureScreenshots(); const doc = new Document(); await createWordDocument(); } catch (error) { console.error('Script encountered an error:', error); } } runScript();
解决方案
1. 移除多余的Document实例化
runScript函数中存在一行无意义的const doc = new Document();,这行代码未被使用且会触发构造函数错误,直接删除即可。
2. 修正Document构造函数调用
docx库新版本要求Document构造函数必须传入配置对象(即使是空对象),将createWordDocument中的:
const doc = new Document();
改为:
const doc = new Document({});
3. 适配Packer的最新API
新版本docx库中,Packer无需实例化,直接调用静态方法Packer.toBuffer即可,将:
const packer = new Packer(); const buffer = await packer.toBuffer(doc);
改为:
const buffer = await Packer.toBuffer(doc);
修正后的完整代码
const puppeteer = require('puppeteer'); const { Document, Packer, Media, Paragraph, TextRun } = require('docx'); const fs = require('fs-extra'); const urls = [ 'https://www.ptinews.com/press-release/pti/62401.html', 'https://www.mysmartprice.com/gear/oneplus-nord-n30-5g-geekbench-listing-specifications-revealed/', 'https://www.dnaindia.com/hindi/entertainment/bollywood/news-kailash-kher-blasts-organizers-khelo-india-university-games-2023-event-bbd-university-lucknow-4089120' ]; async function captureScreenshots() { const browser = await puppeteer.launch(); const page = await browser.newPage(); for (let i = 0; i < urls.length; i++) { const url = urls[i]; await page.goto(url); await page.screenshot({ path: `screenshot${i}.png` }); } await browser.close(); } async function createWordDocument() { const doc = new Document({}); const paragraph = new Paragraph(); for (let i = 0; i < urls.length; i++) { const imagePath = `screenshot${i}.png`; const imageBuffer = fs.readFileSync(imagePath); const image = Media.addImage(doc, imageBuffer, 400, 300); paragraph.addRun(new TextRun().addImage(image)); } doc.addParagraph(paragraph); const buffer = await Packer.toBuffer(doc); fs.writeFileSync('output.docx', buffer); console.log('Word document created successfully!'); } async function runScript() { try { await captureScreenshots(); await createWordDocument(); } catch (error) { console.error('Script encountered an error:', error); } } runScript();
额外建议
- 执行
npm update docx确保使用最新稳定版的docx库; - 文件读取建议改用异步方法
fs.readFile,避免阻塞事件循环。
内容的提问来源于stack exchange,提问作者amit kumar
相关产品推荐
相关产品推荐

