使用iframe调出打印预览后 自动触发打印
时间: 2023-07-06 14:09:03 浏览: 88
在iframe加载完成后,可以使用JavaScript代码触发打印操作。下面是一个示例代码:
```javascript
var iframe = document.getElementById('myiframe'); // 获取iframe元素
iframe.onload = function() {
// iframe加载完成后触发打印操作
iframe.contentWindow.print();
}
```
这段代码假设你的iframe元素的id为“myiframe”。在iframe加载完成后,通过访问iframe的contentWindow属性可以获取到iframe中的文档对象。然后调用print()方法就可以触发打印操作了。
相关问题
js 在打印预览中触发打印按钮
在打印预览中触发打印按钮可以使用 JavaScript 的 `window.print()` 方法来实现。具体的实现步骤如下:
1. 获取打印按钮元素:
```javascript
const printButton = document.querySelector('#print-button');
```
2. 给打印按钮绑定点击事件:
```javascript
printButton.addEventListener('click', function() {
// 在点击打印按钮后触发打印操作
window.print();
});
```
3. 在打印预览中触发打印按钮:
```javascript
const previewIframe = document.querySelector('#preview-iframe');
const previewIframeDoc = previewIframe.contentDocument || previewIframe.contentWindow.document;
const printButton = previewIframeDoc.querySelector('#print-button');
printButton.click();
```
这样就可以在打印预览中触发打印按钮了。注意,由于浏览器的安全机制限制,只有当打印预览页面和当前页面的源相同时,才能在打印预览中触发打印操作。
jquery 使用iframe 打印文件流 并跳过预览
可以使用 jQuery 创建一个隐藏的 iframe,将文件流作为 iframe 的 src 属性值,这样浏览器就会自动下载并打印文件,同时也跳过了预览的过程。
具体实现步骤如下:
1. 创建一个隐藏的 iframe。
```javascript
var $iframe = $('<iframe>', {
id: 'print-frame',
name: 'print-frame',
style: 'display:none;'
}).appendTo('body');
```
2. 将文件流作为 iframe 的 src 属性值,并设置响应的 HTTP 头信息。
```javascript
$.ajax({
url: '/print',
type: 'POST',
data: fileStream,
processData: false,
contentType: false,
xhrFields: {
responseType: 'blob'
},
success: function(response) {
var blobUrl = URL.createObjectURL(response);
$iframe.attr('src', blobUrl);
$iframe.load(function() {
URL.revokeObjectURL(blobUrl);
$iframe.remove();
});
}
});
```
注意:以上代码仅供参考,具体实现需要根据实际情况进行调整。另外,需要注意打印功能的兼容性问题。不同浏览器可能有不同的实现方式,需要进行测试和兼容处理。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)