javascript打印语句(js 直接打印)

图片授权基于 www.pixabay.com 相关协议

最近项目中有「打印」需求,要求把页面中一部分元素通过「打印机」打印出来,所以研究了一下 Print 打印功能。

Print() 方法解释
在 mozilla.org 网站上,是这样解释的。

Opens the Print Dialog to print the current document.

打开「打印对话框」打印当前文档

In most browsers, this method will block while the print dialog is open. However in more recent versions of Safari, it may return immediately.

在大多数浏览器,打开「打印对话框」时会阻塞其他方法。

https://developer.mozilla.org/en-US/docs/Web/API/Window/print
兼容性
javascript打印语句(js 直接打印)
PC 和 Mobile 端,主流浏览器都没有问题。
Print 方法使用
<button id=\\\"printFn\\\" onclick=\\\"printFn()\\\">打印</button><script>  function printFn(){    window.print(); //直接打印  }</script>
window.print() 方法,打印本页面所有元素。
如何打印局部?
有两种可以选择的方法:第一种是利用 @media 来监听 print 变化,这个就很简单,写对应 CSS 就 OK。可以改变布局、可以隐藏布局,还可以改变「颜色」「大小」「间距」等属性。
<style>  //只要获取到操作打印的动作就会执行下方 css  @media print{      .print{color: red;}  }</style>
第二种方法呢,可以利用浏览器中对于 print 监听方法 ,用来监听打印的状态。
打印前「beforeprint
The beforeprint event is fired when the associated document is about to be printed or previewed for printing.
这个 beforeprint 事件,在当文档将要打印或预览打印时会触发。
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeprint_event
    window.addEventListener(\\\'beforeprint\\\', (event) => {  console.log(\\\'Before print\\\');});
    打印后「afterprint

    The afterprint event is fired after the associated document has started printing or the print preview has been closed.

    这个 afterprint 事件,在文档开始打印或预览打印关闭后触发。

    https://developer.mozilla.org/en-US/docs/Web/API/Window/afterprint_event
      window.onafterprint = (event) => {  console.log(\\\'After print\\\');};
      这两个方法虽然可以在打印功能前后给一定的操作空间,但在 safari 上兼容性是不能被接受的。

      javascript打印语句(js 直接打印)

      我用的是「matchMedia」这个方法
      window.matchMedia(\\\'print\\\')

      The Window interface\\’s matchMedia() method returns a new MediaQueryList object representing the parsed results of the specified media query string.

      Window 的接口 matchMedia() 方法可以返回一个新的 MediaQueryList 对象,这个对象是媒体查询字符串的解析结果。

      https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
      matchMedia 方法使用
      let matchPrint = window.matchMedia(\\\"print\\\");matchPrint.addListener(res => {  if (res.matches) {    console.log(\\\"open\\\");//打开了「打印对话框」  } else {    console.log(\\\"close\\\");//关闭了「打印对话框」  }});
      通过这个方法,我们也可设置在「打开对话框」的时候去隐藏一些页面元素,这样可以「打印」出你想打印的部分。
      if (res.matches) {    let _id = document.getElementById(\\\'someId\\\');    _id.style.display = \\\'none\\\';    console.log(\\\"open\\\");//打开了「打印对话框」}
      兼容性还不错:

      如何去掉页码、去掉标题、去掉网址?

      在打印对话框中,可以选择右侧的「更多设置」中,取消「页眉页脚」,即可解决。

      上面的打印方法是不支持「背景图」的,如果想打印背景图就要更换布局方式。把背景图换成图片定位到 Demo 中去。
      如果项目非常复杂,上面几种办法不能解决的话,那就只能把你需要打印的 Demo 都渲染到 Canvas 上去了,这样通过 Canvas 再去打印 …

      推荐阅读

      JavaScript 编译原理
      JavaScript 引擎、编译器、作用域

      JavaScript 的装箱和拆箱


      参考资料:

      [1]https://developer.mozilla.org/en-US/docs/Web/API/MediaQueryList
      [2]https://developer.mozilla.org/en-US/docs/Web/API/Window/matchMedia
      [3]https://developer.mozilla.org/en-US/docs/Web/Guide/Printing
      [4]https://developer.mozilla.org/en-US/docs/Web/API/Window/print
      [5]https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeprint_event

      原创文章,作者:小道研究,如若转载,请注明出处:https://www.sudun.com/ask/34596.html

      (0)
      小道研究的头像小道研究
      上一篇 2024年4月19日
      下一篇 2024年4月19日

      相关推荐

      发表回复

      您的电子邮箱地址不会被公开。 必填项已用 * 标注