hrms-report-template/test-run/url_play2pdf.ts

80 lines
No EOL
2.7 KiB
TypeScript

/*
// // ใช้เพื่อทดสอบ Plwywright
import {chromium} from 'playwright'
(async () => {
//const targetUrl = "https://bma-dashboard.frappet.synology.me/d/5EwyjelSk/1408ef66-0081-5b3f-aa00-5e70aa9bdbf1?orgId=1&kiosk=true"
//const targetUrl = "https://bma-dashboard.frappet.synology.me/d/OLZwQhPVz/4Liq4Lit4Lia4LmB4LiC4LmI4LiH4LiC4Lix4LiZ?orgId=1&kiosk"
const targetUrl = "https://blognone.com"
const width_px = Number(process.env.PDF_WIDTH_PX) || 800;
const browser = await chromium.launch()
const page = await browser.newPage()
await page.setViewportSize({ width: width_px, height: 800 });
await page.goto(targetUrl, { waitUntil: 'networkidle' })
console.log("document.body.scrollHeight "+ await await page.evaluate(() => document.body.scrollHeight) )
let x = await page.evaluate(async () => {
const scrollableSection = document.querySelector('.scrollbar-view');
if (scrollableSection) {
const childElement = scrollableSection.firstElementChild;
let scrollPosition = 0;
let viewportHeight = window.innerHeight;
if (childElement)
while (scrollPosition < childElement.scrollHeight) {
scrollableSection.scrollBy(0, viewportHeight);
await new Promise(resolve => setTimeout(resolve, 500));
scrollPosition += viewportHeight;
}
return scrollPosition
}
return 0
});
console.log("scrollPosition=" + x)
const totalHeight = await page.evaluate(() => {
const scrollableSection = document.querySelector('.scrollbar-view');
if(scrollableSection&&scrollableSection.firstElementChild){
return scrollableSection.firstElementChild.scrollHeight
}
return document.body.scrollHeight
});
console.log("totalHeight="+totalHeight)
if (!totalHeight) {
throw new Error("Unable to determine the page height. The selector '.scrollbar-view' might be incorrect or missing.");
} else {
console.log("Page height adjusted to:", totalHeight);
}
console.log("set viewport ")
await page.setViewportSize({
width: width_px,
height: totalHeight,
});
page.emulateMedia({ media: 'print' })
await page.screenshot({
path: 'url_play.png',
fullPage: true,
//quality: 100,
// type:"png"
});
await page.pdf({
path: '.output/url_play.pdf',
width: width_px + 'px',
height: totalHeight + 'px',
printBackground: true,
// format: 'A4',
displayHeaderFooter: true,
// headerTemplate: '<span style="font-size:10px;">SaaS Report Header</span>',
// footerTemplate: '<span style="font-size:10px;">Page <span class="pageNumber"></span> of <span class="totalPages"></span></span>',
margin: { top: '20px', bottom: '20px', right: '10px', left: '10px' }
});
await browser.close()
})();
*/