如何打印网页与iframe和css
How to print webpages with iframe and css
我必须打印对话框的内容。我添加了一个空的iframe,然后我在javascript中添加内容。
这是我的js代码,我添加HTML和CSS作为iframe的子元素
var layoutCSS = new String('<link href="css/ReportPageOne.css" rel="stylesheet" type="text/css">');
var pageOneCSS = new String('<link href="css/ReportLayout.css" rel="stylesheet" type="text/css">');
var pageTwoCSS = new String('<link href="css/ReportPageTwo.css" rel="stylesheet" type="text/css">');
var materialityCSS = new String('<link href="css/MaterialityMatrix.css" rel="stylesheet" type="text/css">');
window.frames.print_frame.document.head.innerHTML = layoutCSS + pageOneCSS + pageTwoCSS + materialityCSS;
window.frames.print_frame.document.body.innerHTML = $('#__dialog1-cont').html();
window.frames.print_frame.window.focus();
window.frames.print_frame.window.print();
正确打开打印对话框,但CSS应用得不太好。
在打印页面的选项,我在"背景图形"检查,我使用谷歌浏览器。
在样式加载之前调用frame.print()
"一旦样式表及其所有导入的内容加载和解析完成,在样式开始应用于内容之前,load事件就会触发。"
试试这个:
head.lastChild.addEventListener("load", function (event) {
// In IE, you have to focus() the Iframe prior to printing
// or else the top-level page will print instead
frame.focus();
frame.print();
}, 0);
引用
- https://github.com/abbotto/teleprint/blob/master/src/components/print.js
- https://developer.mozilla.org/en-US/docs/Web/HTML/Element/link Stylesheet_load_events
相关文章:
- 将iframe中的css类应用于主文档
- CSS 和 JS 文件正在由 iframe 缓存
- 将URL加载到CSS动画滑动iframe中
- 将自定义CSS注入WP Admin登录弹出iframe
- 如何在启用designMode的iframe中包装文本,必要时使用css和javascript
- 如何为iframe的内部html编写css
- iframe-css-src更改,如果父css src更改
- Iframe滚动溢出隐藏在css中不起作用
- 需要从父页面使用 javascript 将 css 样式添加到 iframe
- 为什么IE不支持在现有应用程序中使用的iFrame js和css文件
- 从 iframe 页面事件更改父级的 css
- 使用 JavaScript 在 iframe 中通过 ID 更改元素的 CSS 样式
- 如何将一个 iframe 的 html 内容复制到另一个 iframe(包括 CSS)
- 如何直接从响应中将 html 和 css 渲染到 iframe 中
- 将 CSS 应用于 iframe 父级(在同一域上)
- 如何使用javaScript在Iframe中添加CSS类和控件元素
- iFrame 中的实时预览和保存样式.css每 30 秒一次
- 从 iframe 外部通过 CSS 路径访问元素
- 在 IFrame 或嵌套 IFrames 中兼容 Javascript、HTML 和 CSS
- 重新加载iFrame CSS