You call print()immediately after you have finished writing tags <img>. However, it takes time to download all of these images (asynchronously). Thus, by the time you call print(), the images have not finished loading, and some may not have determined their height, which led to an unexpected number of pages.
To fix this, you should print()only call after the event has onloadtriggered all the elements of the image. Here's how I solved it:
var nImages = _totalPages;
function imageLoaded() {
if (--nImages === 0) {
_printWindow.print();
}
}
for (var i = 1; i <= _totalPages; i++) {
var img = new Image;
img.addEventListener('load', imageLoaded);
img.src = ;
_printWindow.document.body.appendChild(img);
}
source
share