JS:window.open - 如何设置Open Sans字体?

时间:2017-08-16 15:14:28

标签: javascript printing font-family

我无法让它发挥作用。我唯一需要的 - 将所有内容字体系列设置为import matplotlib.pyplot as plt fig, ax = plt.subplots() ax.set_aspect('equal') ax.plot([0,1],[0,1]) ax2 = fig.add_axes(ax.get_position()) ax2.set_facecolor("None") ax2.set_aspect('equal') ax2.plot([2,0],[0,2], color="red") ax2.tick_params(bottom=0, top=1, left=0, right=1, labelbottom=0, labeltop=1, labelleft=0, labelright=1) plt.show() 。这就是我现在所拥有的

'Open Sans'

这段代码显示空白页面,但当我删除 var popupWin = window.open('', '_blank', 'width="100%",height="100%"'); popupWin.document.write('<html><head><link href=\'http://fonts.googleapis.com/css?family=Open+Sans\' rel=\'stylesheet\' type=\'text/css\'></head><body>' + content + '</body></html>'); popupWin.document.body.setAttribute('style', 'font-family: \'Open Sans\' !important'); popupWin.document.close(); popupWin.print(); popupWin.close(); } 属性时,内容显示,但默认系列。有什么想法吗?

P.S。 content变量包含一个简单的html标记,我试图将样式设置为

link

但没有运气。

1 个答案:

答案 0 :(得分:1)

http更改为https。您可能还希望在打印前添加延迟。请尝试以下方法:

document.querySelector('button').onclick = function() {
  var content = document.querySelector('textarea').innerHTML;

  var popupWin = window.open('', '_blank', 'width="100%",height="100%"');
  if (popupWin) {
    console.log('Popup was blocked');
    return false;
  }
  popupWin.document.write('<html><head><link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet" type="text/css"></head><body>' + content + '</body></html>');
  popupWin.document.body.setAttribute('style', 'font-family: "Open Sans" !important');
  setTimeout(function() {
    popupWin.document.close();
    popupWin.print();
    popupWin.close();
  }, 2000);
};
<h1>Test</h1>
<textarea>Put this on the popup page</textarea>
<button>Print</button>