For this, you have to add options in html2canvas method scrollY
{scrollY: -window.scrollY} it will take the screenshot of the fully rendered page.
html2canvas(data, {scrollY: -window.scrollY, scale: 1}
Apart from this as we know we have data present in the scroll bar. For this, you have to remove scroll temporarily and need to add after pdf generate.
You can do this by simply adding id to this
<ul class="list-group list-group-flush vert-scrollable-150" id="alldata">
element.
// coded for disabling the scroll
document.getElementById('alldata').style.overflow = 'inherit';
document.getElementById('alldata').style.maxHeight = 'inherit';
async downloadPdf() {
var data = document.getElementById('pdfDownload');
$('pdfOpenHide').attr('hidden', true);
// disable the scroll
document.getElementById('alldata').style.overflow = 'inherit';
document.getElementById('alldata').style.maxHeight = 'inherit';
await html2canvas(data, {scrollY: -window.scrollY,
scale: 1}).then(canvas => {
// Few necessary setting options
var imgWidth = 150;
var imgHeight = canvas.height * imgWidth / canvas.width;
const contentDataURL = canvas.toDataURL('image/png', 1.0)
// enabling the scroll
document.getElementById('alldata').style.overflow = 'scroll';
document.getElementById('alldata').style.maxHeight = '150px';
let pdf = new jspdf('l', 'mm','a4'); // A4 size page of PDF
var position = 0;
// add tghis width height according to your requirement
const divHeight = data.clientHeight
const divWidth = data.clientWidth
const ratio = divHeight / divWidth;
const width = pdf.internal.pageSize.getWidth();
let height = pdf.internal.pageSize.getHeight();
height = ratio * width;
pdf.addImage(contentDataURL, 'PNG', 0, position, width, height);
window.open(pdf.output('bloburl', { filename: 'new-file.pdf' }), '_blank');
});
}
you can also add a page if data is more in scroll bar using jspdf.
For more reference, you can check this
HTML2Canvas does not render full div, only what is visible on screen?
Another Solution: If data is more
async downloadPdf() {
var data = document.getElementById("pdfDownload");
$("pdfOpenHide").attr("hidden", true);
// To disable the scroll
document.getElementById("alldata").style.overflow = "inherit";
document.getElementById("alldata").style.maxHeight = "inherit";
await html2canvas(data, { scrollY: -window.scrollY, scale: 1 }).then(
canvas => {
const contentDataURL = canvas.toDataURL("image/png", 1.0);
// enabling the scroll
document.getElementById("alldata").style.overflow = "scroll";
document.getElementById("alldata").style.maxHeight = "150px";
let pdf = new jspdf("l", "mm", "a4"); // A4 size page of PDF
let imgWidth = 300;
let pageHeight = pdf.internal.pageSize.height;
let imgHeight = (canvas.height * imgWidth) / canvas.width;
let heightLeft = imgHeight;
let position = 0;
pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft >= 0) {
position = heightLeft - imgHeight;
pdf.addPage();
pdf.addImage(contentDataURL, "PNG", 0, position, imgWidth, imgHeight);
heightLeft -= pageHeight;
}
window.open(
pdf.output("bloburl", { filename: "new-file.pdf" }),
"_blank"
);
}
);
}