#!/usr/bin/env node const puppeteer = require('puppeteer'); const yargs = require('yargs'); const path = require('path'); const argv = yargs .usage('Usage: url-to-image [options]') .demandCommand(1, 'You need to provide a URL') .option('w', { alias: 'width', describe: 'Set viewport width', type: 'number', default: 1920, }) .option('h', { alias: 'height', describe: 'Set viewport height', type: 'number', default: 1080, }) .option('n', { alias: 'name', describe: 'Filename for the screenshot', type: 'string', }) .help() .argv; const url = argv._[0]; const width = argv.w; const height = argv.h; const filename = argv.n || `${Date.now()}.jpg`; (async () => { const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.setViewport({ width, height }); await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'); await page.goto(url, { waitUntil: 'networkidle2', timeout: 60000 }); await page.screenshot({ path: path.resolve(filename) }); await browser.close(); console.log(`Screenshot saved as ${filename}`); })();