Im trying to setup the pdf download and i will share the script file which i have created
function generateHTML(audits) {
return <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Audit Report</title> <style> body { font-family: Arial, sans-serif; margin: 20px; } h1 { text-align: center; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: left; } th { background-color: #f4f4f4; } tr:nth-child(even) { background-color: #f9f9f9; } </style> </head> <body> <h1>Audit Report</h1> <table> <thead> <tr> <th>ID</th> <th>Timestamp</th> <th>Operation</th> <th>Resource ID</th> <th>User ID</th> </tr> </thead> <tbody> ${audits .map( (audit) =>
${audit.id}
${audit.timestamp}
${audit.operation}
${audit.resourceId !== null ? audit.resourceId : “N/A”}
${audit.userId || “N/A”}
, ) .join("")} </tbody> </table> </body> </html> ;
}
async function generateAuditPDF(filteredAudits, pdfPath) {
const htmlContent = generateHTML(filteredAudits);
const puppeteer = require(“puppeteer”);
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(htmlContent);
const pdfBuffer = await page.pdf({
format: “A4”,
printBackground: true,
});
await browser.close();
return pdfBuffer
}
const audits = bh.local.result
const pdfPath = “audit-report.pdf”
async function gen() {
try {
const pdf = await generateAuditPDF(audits, pdfPath)
console.log(‘thisu is te pdf’, pdf)
bh.local.pdf = pdf
} catch (err) {
throw new Error(err)
}
}
gen()
here how can i set the pdf to the bh object so i can return that through the http out node right or is there is any other better approach for handling this ?
this is only dowloading a pdf with no content
