Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It doesn't work on iPhone or iPad #1

Open
vannak62 opened this issue Oct 1, 2019 · 7 comments
Open

It doesn't work on iPhone or iPad #1

vannak62 opened this issue Oct 1, 2019 · 7 comments

Comments

@vannak62
Copy link

vannak62 commented Oct 1, 2019

This is really great solution to the old trick as you state in your website, however it still doesn't work on iPhone or iPad(no android to test). Can you explain why or any solution?

@n9yty
Copy link

n9yty commented Sep 28, 2021

I just encounted this same issue, but this is a great solution, there must be a way. Time to dig in I guess and diagnose what the iPad/iPhone doesn't like about it.

@szepeshazi
Copy link
Owner

@n9yty If you manage to resolve the issue for i* devices, please add a pull request with the patch. Thanks for your contribution!

@n9yty
Copy link

n9yty commented Sep 28, 2021

I do find it interesting that if I hook the iPad up to my computer and do remote debugging with Safari, I can put a breakpoint in the javascript on the window.print() line, and if I change the stylesheet from @media print to @media screen the screen on the iPad does properly render just the element.

That means that all the code that walks the tree and assigns the styles is working, and working correctly, it seems that for whatever reason the iPad/iOS is not applying the stylesheet when doing the printing. But yet I have read reports that iOS supposedly has supported @media print for a long time now.

Not sure if that is helpful, if I find anything more I'll come back. Chrome on iOS is no different, but Edge and Firefox on my iPad don't open the print dialog at all, but that is another problem. ;)

@szepeshazi
Copy link
Owner

@n9yty awesome, thanks for the update.

@n9yty
Copy link

n9yty commented Sep 28, 2021

I still can't get it 100%, but the issue seems to be that window.print() does not stop JavaScript execution on iPadOS/iOS, so what happens is that by the time the print dialog is on screen Safari has already removed the classes indicating what to print or not print.

I found that the "afterprint" event is supported, so removing the cleanup code into a separate function that is called as part of that event works "most" of the time. Sometimes, though, the print dialog opens and then "flashes" as it loads the printer options and re-renders the preview as full page. Closing and re-printing seems to work.

So the code I have looks like this: {can't get the code formatting to work here, sorry}

`
var _delayedCleanup = function (elements) {
for (var i = 0; i < elements.length; i++) {
_walkTree(elements[i], _cleanup);
}
}

var _print = function (elements) {
for (var i = 0; i < elements.length; i++) {
_walkTree(elements[i], _attachPrintClasses);
}
window.addEventListener('afterprint', function() { _delayedCleanup(elements) } );
window.print();
};
`

Hopefully that will spark some ideas on maybe a more proper "fix" or workaround for iOS/iPadOS. I'm still digging into JavaScript so am not the best person to deal with edge cases. :)

@n9yty
Copy link

n9yty commented Sep 29, 2021

In the end, neither proved reliable as when the print dialog refreshed for any reason the page elements were cleared out. In the end I made a _cleanAll() function that removes all the tags and I call that before setting a new print operation up, and also exposed it so it could be called via a button on the web page for the user to manually reset things. I will fork and send a push request through but it is not a 'fix' but makes changes to operation so you may not want to include it.

@szepeshazi
Copy link
Owner

@n9yty Ok. Thanks for your efforts!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants