Print.js: onPrintDialogClose not working with Chrome Version 86.0.4240.75 (Official Build) (64-bit)

I use the PrintJS library for pdf print and I used to call the onPrintDialogClose function to reload after I close the dialogue window. But since the latest chrome version, the function is being skipped altogether and not triggering. Although the dialogue window is opening but the onPrintDialogClose functionality is not triggering and skipping.

sample code which was working before:

window.printJS({printable:url, type:'pdf', onPrintDialogClose: function(){ window.location.href = "/"; window.location.reload(); }});

EDIT: Interesting point: If I switch screen or toggle any opened tab or open a new tab and come back. The reload function triggers then.

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 12
  • Comments: 21 (2 by maintainers)

Most upvoted comments

I opened a pull request.

What workround can I suggest:

After calling printJS ()

add the following code:

  const handler = () => {
    // Make sure the event only happens once.
    window.removeEventListener ('mouseover', handler)

---------- your callback function here--------------

    // Remove iframe from the DOM, by default 'printJS'
    const iframe = document.getElementById (frameId)

    if (iframe) {
      iframe.remove ()
    }
  }

  setTimeout (() => {window.addEventListener ('mouseover', handler)}, 1000)

It may still need to be checked for specific browsers, I checked it only for Chrome.

Tell me, if it doesn’t work, I’ll try to find something else.

I opened a pull request.

What workround can I suggest:

After calling printJS ()

add the following code:

  const handler = () => {
    // Make sure the event only happens once.
    window.removeEventListener ('mouseover', handler)

---------- your callback function here--------------

    // Remove iframe from the DOM, by default 'printJS'
    const iframe = document.getElementById (frameId)

    if (iframe) {
      iframe.remove ()
    }
  }

  setTimeout (() => {window.addEventListener ('mouseover', handler)}, 1000)

It may still need to be checked for specific browsers, I checked it only for Chrome.

Tell me, if it doesn’t work, I’ll try to find something else.

It seems to be not deterministic, worked 2 times on many attempts, not sure what made it work in my case.

My case was to print multiple documents. I’ve tried using printjs recursively like that:

const print = (dataArr) => {
   if (!dataArr.length) {
      return;
   }
   printJS({
      ...dataArr[0],
      onPrintDialogClose: () => {
         if (dataArr.length > 1) {
            printJS(dataArr.slice(1, dataArr.length));
         }
      },
   });
};

but onPrintDialogClose didn’t work. It’s not exact my code, so may have glitches. I finally managed to make printjs display multiple print dialogs by setting dynamic frameId

const print = (dataArr) => {
   dataArr.forEach((document, index) => {
      printJS({
         ...document,
         frameId: `printJS-${index}`,
      })
   })
}

I hope it’ll help someone

I opened a pull request.

What workround can I suggest:

After calling printJS ()

add the following code:

  const handler = () => {
    // Make sure the event only happens once.
    window.removeEventListener ('moseover', handler)

---------- your callback function here--------------

    // Remove iframe from the DOM, by default 'printJS'
    const iframe = document.getElementById (frameId)

    if (iframe) {
      iframe.remove ()
    }
  }

  setTimeout (() => {window.addEventListener ('mouseover', handler)}, 1000)

It may still need to be checked for specific browsers, I checked it only for Chrome.

Tell me, if it doesn’t work, I’ll try to find something else.

this is working but there is spelling mistake for mouseover event

Hey guys!

Any news about this defect?