【问题标题】:how to hide title and date on print screen in javascript如何在javascript中隐藏打印屏幕上的标题和日期
【发布时间】:2019-10-21 14:59:04
【问题描述】:

我正在使用 window.print 方法在屏幕下方打印。现在我想隐藏日期(图像和标题的左上角(不是大标题),由于某种原因我已经模糊了)。

我读过文章,上面写着@pages { margin: 0 } 但我不确定如何将其应用于 window.print();

我尝试过使用setAttribute(),但它没有帮助,或者我可能犯了一些错误

      const element = document.getElementById("qr-code").innerHTML;
      var printWindow = window.open('', '', 'height=800,width=800');
      printWindow.document.write(element);
      printWindow.document.close();
      const title = printWindow.document.title = "some title here";
      const heading = printWindow.document.createElement('h1');
      const text = printWindow.document.createTextNode(title);
      heading.appendChild(text);
      heading.setAttribute('style', 'order: -1; margin-bottom: 50px;');
      printWindow.document.body.appendChild(heading);
      printWindow.document.body.setAttribute('style', 'display: grid; width: 100%; justify-items: center; margin: 0;');
      printWindow.print();

【问题讨论】:

  • 你能在这里显示相关的代码吗?除非我们看到这一点,否则我们将无法真正提供帮助。您可以在发布之前更改任何我们看不到的信息。
  • 嗨 @AndrewLohr 用代码编辑

标签: javascript css vue.js


【解决方案1】:

https://stackoverflow.com/a/2573612/1309377 获取信息,您可以添加此 css

@page { size: auto;  margin: 0mm; }

到窗口,它将隐藏日期和标题(因为它们在边距中)。要添加此 css,您可以创建一个样式元素并将其附加到窗口,就像您已经对标题所做的那样。

const element = document.getElementById("qr-code").innerHTML;
var printWindow = window.open('', '', 'height=800,width=800');
printWindow.document.write(element);
printWindow.document.close();
const title = printWindow.document.title = "some title here";
const heading = printWindow.document.createElement('h1');
const text = printWindow.document.createTextNode(title);
heading.appendChild(text);

// Here is the style you can add to hide the date and title in the print screen.
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = '@page { size: auto;  margin: 0mm; }';

heading.setAttribute('style', 'order: -1; margin-bottom: 50px;');
printWindow.document.body.appendChild(heading);

// Append the style to the window's body
printWindow.document.body.appendChild(style);

printWindow.document.body.setAttribute('style', 'display: grid; width: 100%; justify-items: center; margin: 0;');
printWindow.print();

【讨论】:

  • 谢谢哥们,它工作得很好,我搜索了很多文章,最后你帮助了我
  • 很高兴为您提供帮助。
猜你喜欢
  • 1970-01-01
  • 2018-11-24
  • 2020-02-28
  • 2012-12-22
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多