Here is my solution using puppeteer to screenshot on the table element
First of all you need to generate the table HTML code
here is the code to generate that code
async function generateHtml(rows) {
return `<!DOCTYPE html>
<html>
<head>
<style>
thead,
tfoot {
background-color: #3f87a6;
color: #fff;
}
tbody {
background-color: #e4f0f5;
}
caption {
padding: 10px;
caption-side: bottom;
}
table {
border-collapse: collapse;
border: 2px solid rgb(200, 200, 200);
letter-spacing: 1px;
font-family: sans-serif;
font-size: .8rem;
}
td,
th {
border: 1px solid rgb(190, 190, 190);
padding: 5px 10px;
}
td {
text-align: center;
}
</style>
</head>
<body>
<table>
<caption>Pornhub Pages Summary</caption>
<thead>
<tr>
<th>ID</th>
<th scope="col">Progress</th>
<th scope="col">Stucked</th>
<th scope="col">Finished</th>
<th scope="col">Busy</th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
</table>
</body>
</html>`
}
And here is the code for generate the rows
argument of the above function
async function getTheImgOfTheSummaryOfThePages() {
const rows = []
for (const [index, val] of statuesOfThePages.entries()) {
const row = `<tr>
<th scope="row">${index}</th>
<th>${val.progress}</th>
<th>${val.stucked}</th>
<th>${val.finished}</th>
<th>${val.pageBusy}</th>
</tr>`
rows.push(row)
}
const path = './summaryOfThePagesOfPornhub.png'
const html = await generateHtml(rows.join('\n'))
await util.takescrrenshotOnTheHtml(html, browser, path, 'table')
return path
}
And here is the code for screenshot on the table element
async function takescrrenshotOnTheHtml(html, browser, pathToSave, onElement) {
const page = await newPage(browser);
await page.setContent(html)
const element = await page.$(onElement)
await element.screenshot({path: pathToSave})
await page.close()
}
Here is the result
Well you just need to change the table headers and the rows of the table