I am trying to take screenshot of the URLs using puppeteer
and save screenshots in Word document, but it is not working giving me an error – Script encountered an error: TypeError: Cannot read properties of undefined (reading 'creator')
using docx.
code –
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();
for testing pourpose as I have check, screenshot is captured and saved in the directory too, but word document is not creating and saving the screenshot in it.