1、准备工作

连接打印机并设置默认打印机:在电脑中连接打印机,并确保已经设置一个默认打印机。
添加启动参数:在Chrome的快捷方式上加上--kiosk-printing参数,这样预览页会自动点击打印按钮。
2、使用window.open方法进行打印
创建新窗口:通过window.open方法可以创建一个新窗口,并在该窗口中写入需要打印的HTML内容,然后调用print()方法进行打印。
function dayin() {
var newWindow = window.open('', '_blank', 'width=1,height=1,top=10000,left=10000');
var html = ""; // 这里的html可由别处传参,也可自己去接口获取
newWindow.document.write(html);
newWindow.print();
newWindow.close();
}使用iframe解决位置问题:虽然window.open方法可以弹出新窗口,但会存在一些用户设置拦截的问题,此时可以用iframe来解决。

var iframe = document.createElement('IFRAME');
iframe.style.position = 'absolute';
iframe.style.width = '0px';
iframe.style.height = '0px';
iframe.style.left = '-500px';
iframe.style.top = '-500px';
document.body.appendChild(iframe);
iframe.contentWindow.focus();
iframe.contentWindow.print();
document.body.removeChild(iframe);3、使用window.print方法进行打印
基本用法:直接在页面上调用window.print()方法进行打印。
<button class="btn btn-success" onclick="window.print()">打印</button>控制打印内容:可以通过JavaScript动态修改页面内容,只保留需要打印的部分。
function printpage(){
document.getElementById("btn-success").style="display:none";
window.print();
document.getElementById("btn-success").style="display:inline";
return false;
}隐藏不需要打印的元素:通过CSS样式控制某些元素在打印时不显示。
-webkit-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
cursor:pointer;4、使用console.log和console.table进行调试

console.log打印输出:在JavaScript控制台中使用console.log()函数打印调试信息。
console.log("Hello, World!");console.table打印表格:使用console.table()函数打印表格数据。
var students = [
{ name: "Alice", age: 18, score: 90 },
{ name: "Bob", age: 19, score: 85 },
{ name: "Charlie", age: 20, score: 95 }
];
console.table(students);Chrome浏览器提供了多种方式通过JavaScript实现直接打印功能,包括使用window.open、iframe、window.print以及JavaScript控制台的console.log和console.table方法,开发者可以根据具体需求选择合适的方法来实现打印功能。
到此,以上就是小编对于“chromejs直接打印”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。