简体   繁体   English

在 Firefox 中打印 iframe 时如何打开打印预览对话框?

[英]How to open the print preview dialog when printing an iframe in Firefox?

Unlike just about every other major browser, in Firefox, window.print opens the print dialog instead of the print preview dialog.与几乎所有其他主要浏览器不同,在 Firefox、 window.print打开打印对话框而不是打印预览对话框。 This issue was recognized by Firefox devs a long time ago, and, rather than "fix" it, they decided to implement a different, Firefox-only function browser.tabs.printPreview that opens the print preview dialog for the current active tab. Firefox 开发人员很久以前就认识到了这个问题,他们决定实现一个不同的、仅限 Firefox 的 function browser.tabs.printPreview ,它打开当前活动选项卡的打印预览对话框,而不是“修复”它。

I am printing a hidden iframe, and ideally want to display the print preview dialog, not the print dialog.我正在打印隐藏的 iframe,理想情况下希望显示打印预览对话框,而不是打印对话框。 It's easy enough for me to feature detect browser.tabs.printPreview , however, I haven't been able to find a way to make it act on an iframe instead of the current tab.检测browser.tabs.printPreview对我来说很容易,但是,我无法找到一种方法让它作用于 iframe 而不是当前选项卡。

Is there a way to open the print preview dialog for an iframe in Firefox, either using browser.tabs.printPreview or some other method?有没有办法使用browser.tabs.printPreview或其他方法打开 Firefox 中 iframe 的打印预览对话框?

Clarification: this is for a React library that I maintain, react-to-print , that wraps a user's JSX with our component.澄清:这是针对我维护的一个 React 库react-to-print ,它用我们的组件包装用户的 JSX。 Except for what is wrapped by the component (the content the user wants to print) I do not have any control over content on the page.除了组件包装的内容(用户想要打印的内容)之外,我对页面上的内容没有任何控制权。

You'll want to use some CSS to change the iframe style just for printing.您需要使用一些 CSS 来更改 iframe 样式仅用于打印。

@media print { 
    iframe { /* Use your own selector here if you need more specificity. */
        display: block;
        position: absolute;
        top: 0;
        left: 0;
        width: 100vw;
        height: 100%;
        z-index: 10000;
    }
}

CSS media queries change the display of webpages on different devices. CSS 媒体查询更改网页在不同设备上的显示。 You can write CSS specifically for browsers with @media screen and CSS specifically for printing with @media print您可以编写 CSS 专门用于具有@media screen的浏览器和 CSS 专门用于使用@media print

The CSS assumes that you don't have any other absolute elements floating above z-index: 10000 , and that the iframe isn't inside an element with position: relative . CSS 假定您没有任何其他绝对元素浮动在z-index: 10000之上,并且 iframe 不在具有position: relative的元素内。 If it is, you'll need to do some more styling to make sure the element is floating above the rest of the page on print.如果是,您需要进行更多样式设置以确保元素浮动在打印页面的 rest 上方。

This CSS snippet will ensure that only your iframe is displayed in print previews, and is similar to a technique which I am currently using in production (on Firefox too).这个 CSS 片段将确保只有您的 iframe 显示在打印预览中,并且类似于我目前在生产中使用的技术(也在 Firefox 上)。

Not that I am aware of, but you can use @media print In your css to show the iframe and hide everything else on the page.我不知道,但是您可以在 css 中使用@media print来显示 iframe 并隐藏页面上的其他所有内容。 If the content of the iframe is not a fixed size, then you could use a library of mine called iframe-resizer to overcome that problem.如果 iframe 的内容不是固定大小,那么您可以使用我的一个名为iframe- resizer 的库来解决这个问题。

I accept that this is all a bit of a workaround rather than a simple one line answer, but if no one comes up with a better answer this should get you the result your after.我接受这只是一种解决方法,而不是简单的单行答案,但如果没有人想出更好的答案,这应该会让你得到你想要的结果。

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

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