js chrome 调用打印功能 跳过预览
时间: 2023-08-19 13:04:52 浏览: 993
可以使用以下代码实现在 Chrome 中调用打印功能并跳过预览:
```javascript
function printWithoutPreview() {
// 打印设置
var printParams = {
silent: true, // 静默打印,不显示打印预览窗口
printBackground: true, // 打印背景颜色和图片
color: false, // 不打印颜色
margin: {
// 设置页边距
marginType: 'custom',
top: 0,
left: 0,
bottom: 0,
right: 0,
},
};
// 调用打印
window.print(printParams);
}
```
使用时只需要调用 `printWithoutPreview()` 函数即可。需要注意的是,该方法只能在用户点击按钮等交互操作后调用,否则浏览器会拒绝打印请求。
相关问题
js chrome iframe 调用打印功能 跳过预览
你可以使用 `window.print()` 方法来触发打印,然后在打印之前设置一些参数,以跳过预览。
以下是使用 iframe 打印并跳过预览的示例代码:
```javascript
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
const iframeWindow = iframe.contentWindow || iframe;
const iframeDocument = iframeWindow.document || iframe.contentDocument;
// 将要打印的内容添加到 iframe 中
iframeDocument.body.innerHTML = '要打印的内容';
// 跳过打印预览并直接打印
iframeWindow.onafterprint = function() {
iframeWindow.close();
};
iframeWindow.print();
```
在这个示例中,我们创建了一个隐藏的 iframe,然后将要打印的内容添加到 iframe 中。接下来,我们通过触发 `window.print()` 方法来打印内容,并通过在 `onafterprint` 事件处理程序中关闭 iframe 来跳过预览。
chrome浏览器中 使用js 调用打印功能 并跳过预览
在Chrome浏览器中,可以使用`window.print()`方法调用打印功能。为了跳过打印预览窗口,可以在调用`window.print()`方法前设置一些打印参数。
以下是一个示例代码,演示如何调用打印功能并跳过预览:
```javascript
function printWithoutPreview() {
// 创建一个隐藏的iframe元素
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
// 将要打印的内容写入iframe中
const content = document.querySelector('#myContent').innerHTML;
const doc = iframe.contentWindow.document;
doc.write(content);
doc.close();
// 设置打印参数
const printParams = {
silent: true, // 静默打印,跳过预览窗口
printBackground: true, // 打印背景颜色和图片
deviceName: 'Microsoft Print to PDF', // 打印机名称
};
// 调用打印功能
iframe.contentWindow.print(printParams);
// 移除隐藏的iframe元素
document.body.removeChild(iframe);
}
```
在上面的示例代码中,我们通过创建一个隐藏的`iframe`元素,将要打印的内容写入其中,并在调用`print()`方法时设置了一些打印参数,从而跳过了预览窗口。需要注意的是,打印机名称应该替换成你本地的打印机名称。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)