Skip to content

Commit 7c9ba51

Browse files
committed
Add image.complete condition to confirm images are loaded in iframe
1 parent 07d51b7 commit 7c9ba51

File tree

1 file changed

+35
-40
lines changed

1 file changed

+35
-40
lines changed

src/js/print.js

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,39 @@ const Print = {
77
document.getElementsByTagName('body')[0].appendChild(printFrame)
88

99
// Get iframe element
10-
let iframeElement = document.getElementById(params.frameId)
10+
const iframeElement = document.getElementById(params.frameId)
1111

1212
// Wait for iframe to load all content
13-
if (params.type === 'pdf' && (Browser.isIE() || Browser.isEdge())) {
14-
iframeElement.setAttribute('onload', performPrint(iframeElement, params))
15-
} else {
16-
printFrame.onload = () => {
17-
if (params.type === 'pdf') {
18-
performPrint(iframeElement, params)
19-
} else {
20-
// Get iframe element document
21-
let printDocument = (iframeElement.contentWindow || iframeElement.contentDocument)
22-
if (printDocument.document) printDocument = printDocument.document
23-
24-
// Inject printable html into iframe body
25-
printDocument.body.innerHTML = params.htmlData
26-
27-
// Add custom style
28-
if (params.type !== 'pdf' && params.style !== null) {
29-
// Create style element
30-
const style = document.createElement('style')
31-
style.innerHTML = params.style
32-
33-
// Append style element to iframe's head
34-
printDocument.head.appendChild(style)
35-
}
36-
37-
// If printing image, wait for it to load inside the iframe
38-
if (printDocument.getElementsByTagName('img').length > 0) {
39-
loadIframeImages(printDocument).then(() => {
40-
performPrint(iframeElement, params)
41-
})
42-
} else {
43-
performPrint(iframeElement, params)
44-
}
45-
}
13+
iframeElement.onload = () => {
14+
if (params.type === 'pdf') {
15+
performPrint(iframeElement, params)
16+
return
17+
}
18+
19+
// Get iframe element document
20+
let printDocument = (iframeElement.contentWindow || iframeElement.contentDocument)
21+
if (printDocument.document) printDocument = printDocument.document
22+
23+
// Inject printable html into iframe body
24+
printDocument.body.innerHTML = params.htmlData
25+
26+
// Add custom style
27+
if (params.type !== 'pdf' && params.style !== null) {
28+
// Create style element
29+
const style = document.createElement('style')
30+
style.innerHTML = params.style
31+
32+
// Append style element to iframe's head
33+
printDocument.head.appendChild(style)
34+
}
35+
36+
// If printing images, wait for them to load inside the iframe
37+
const images = printDocument.getElementsByTagName('img')
38+
39+
if (images.length > 0) {
40+
loadIframeImages(images).then(() => performPrint(iframeElement, params))
41+
} else {
42+
performPrint(iframeElement, params)
4643
}
4744
}
4845
}
@@ -70,13 +67,11 @@ function performPrint (iframeElement, params) {
7067
}
7168
}
7269

73-
function loadIframeImages (printDocument) {
74-
let tagsImg = printDocument.getElementsByTagName('img')
75-
70+
function loadIframeImages (images) {
7671
const promises = []
7772

78-
for (let index = 0; index < tagsImg.length; index++) {
79-
promises.push(loadIframeImage(tagsImg[index]))
73+
for (let image of images) {
74+
promises.push(loadIframeImage(image))
8075
}
8176

8277
return Promise.all(promises)
@@ -85,7 +80,7 @@ function loadIframeImages (printDocument) {
8580
function loadIframeImage (image) {
8681
return new Promise(resolve => {
8782
const pollImage = () => {
88-
!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0
83+
!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0 || !image.complete
8984
? setTimeout(pollImage, 500)
9085
: resolve()
9186
}

0 commit comments

Comments
 (0)