从前端 javascript 打印?
是否可以在浏览器中使用带有javascript的打印机打印一些东西?
我想打印收据号,所以如果可能的话,最快的打印机是什么,这样当用户单击按钮时它就会打印出来,例如。小纸上写着“1234”。
谢谢
Is it possible to print something with a printer with javascript in the browser?
I want to print a receipt number, so if it's possible, what is the fastest printer so when the user clicks on a button it will print out eg. "1234" on a small paper.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您无法直接从 Javascript 访问打印机,但可以调用
window.print()
这将启动标准浏览器打印行为。使用此功能,您可以尝试两种技术来实现您想要的效果:在调用
window.print()
之前注入一个动态打印样式表,该样式表仅显示包含您要打印的文本的元素。您需要小心清理以前的打印样式表。或者事实上,您可以只使用元素这是打印样式表中唯一可见的元素,并在其中插入要打印的任何文本。 (请注意,如果这是用户可能真正想要打印的网站)
也可以直接在 iframe 窗口上调用
print()
,您可以在该窗口中填充所需的文本。例如:You can't access the printer directly from Javascript but you can call
window.print()
which will initiate the standard browser print behaviour. Using this, you could try two techniques to achieve what you're after:Just before calling
window.print()
inject a dynamic print stylesheet that only shows the elements with the text you're wanting to print. You would need to be careful to cleanup any previous print stylesheets. Or in fact you could just use an element<div id="printable">
which is the only visible element in your print stylesheet and insert any text to be printed in that. (Just be mindful if this is a website which users may actually want to print)It's also possible to call
print()
directly on an iframe window, which you could populate with your desired text. For example:您无法从浏览器中访问打印设置。
这是出于安全考虑,否则全世界的打印机都会不停地打印。
关于在调用
window.print();
中用 javascript 进行打印,您唯一能做的就是。You can't access print settings from within the browser.
This is due to security considerations, otherwise printers worldwide would be printing non-stop.
The only thing you can do regarding printing in javascript in call
window.print();
.