Print.js: In the latest version of chrome, it can not work as expected

Created on 15 Jun 2017  Â·  10Comments  Â·  Source: crabbly/Print.js

In Safari, it is normal. But in chrome, print html content, it can not recognize the style. How is it?

Most helpful comment

I make it work by adding this line
iframeElement.setAttribute('style', 'display:block;')
before iframeElement.focus() in finishPrint function

All 10 comments

system: MACOS 10.0+
browser: Chrome 59.0+

This issue is probably related to my issue #54

@joster422 I have a good idea to print and work well in Chrome.

``javascript function print(id, style) { let printHtml = document.querySelector(#${id}).innerHTML let iframe = document.createElement('iframe') let div = document.createElement('div') div.innerHTML = printHtml document.body.appendChild(iframe) iframe.contentDocument.body.appendChild(div) let tagStyle = document.createElement('style') tagStyle.innerHTML = style; iframe.contentDocument.head.appendChild(tagStyle) if (navigator.userAgent.toLowerCase().indexOf("firefox") != -1) { iframe.contentWindow.print(); } else { iframe.contentWindow.document.execCommand('print', false, null); } document.body.removeChild(iframe) }

@joster422 to try

I make it work by adding this line
iframeElement.setAttribute('style', 'display:block;')
before iframeElement.focus() in finishPrint function

@yuanwowo my idea is printed of html page.

@tstrilogy its a good solution but then you see the new iframe content window in the background of the print model. One of the selling points of print.js is the ability to print the page using the print model without having another window open up.

@yuanwowo this fixes the issue. Going to try to merge this up. Thank you so much!

Should mention that the solution had an issue, the iframe would still be visible with the display being "block" now instead of "none".

iframeElement.setAttribute('style', 'display:block; position: absolute; top: -9999px; left: -9999px;')

this makes it not display and takes it out of the flow of the document so your scrollbars arn't affected

Hey guys, does anybody know why making the iframe visible fixes the layout issue?

Looks like this issue is not directly related to a Chrome version, but instead, how the library process the style / css applied to the html element.

Closing this in favor of #74

Was this page helpful?
0 / 5 - 0 ratings

Related issues

deysudip picture deysudip  Â·  4Comments

JahsonKim picture JahsonKim  Â·  5Comments

SleepyFanjo picture SleepyFanjo  Â·  4Comments

Orcinuss picture Orcinuss  Â·  8Comments

r3wt picture r3wt  Â·  8Comments