简体   繁体   English

用户选择文件->打印时,仅打印iframe的内容

[英]Printing only the contents of an iframe when a user selects file->print

Problem: 问题:

I have an application which loads content from other servers into an <iframe> . 我有一个应用程序,可将其他服务器上的内容加载到<iframe> Users want to be able to print the content in the iframe, which can sometimes be quite long and cause the iframe to scroll. 用户希望能够在iframe中打印内容,该内容有时会很长,并导致iframe滚动。 When the print, only the area shown in the iframe's viewport is printed. 打印时,仅打印iframe视口中显示的区域。 The rest is cut off. 其余的被切断。

What I've tried: 我试过的

Using @media print styles, I can hide all the other elements on the page and expand the iframe to 100% width and height. 使用@media print样式,我可以隐藏页面上的所有其他元素,并将iframe扩展为100%的宽度和高度。 What I can't do AFAICT is resize it to the w/h of the actual content (using CSS alone), so if 100% isn't enough the content is truncated. 我不能做的就是将AFAICT的大小调整为实际内容的w / h(仅使用CSS),因此,如果100%不够,内容将被截断。

I know this is solvable via javascript, but browsers do not consistently offer an event hook for file->print, so I can't run the javascript when it's needed. 我知道这可以通过javascript解决,但是浏览器不能始终为文件-> print提供事件挂钩,因此我无法在需要时运行javascript。

I could make a custom "print" button, so that the JS could run and the iframe would be correctly printed, but it doesn't allow for "Print Preview", because browsers don't offere a JS method for this. 我可以创建一个自定义的“打印”按钮,以便可以运行JS并正确打印iframe,但不允许“打印预览”,因为浏览器没有为此提供JS方法。

I'm stumped here, is there an answer? 我被困在这里,有答案吗?

If you want to print the contents of your iframe you can use 如果要打印iframe的内容,可以使用

document.getElementById('ifr1').contentWindow.print()

This will call the print() on the contents of the iframe (and not the container window). 这将在iframe(而不是容器窗口)的内容上调用print() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM