简体   繁体   English

使用 window.open 中的按钮进行打印

[英]Print using a button from window.open

I have the following button which opens a new window我有以下按钮可以打开一个新的 window

<a href="javascript:void(0);" class="btn btn-lg green" onclick="PrintElem('#sample_editable_1', 'Fault Recording')"><i class="fa fa-file-text-o" aria-hidden="true" style="padding-right: 5px;"></i>Run Report</a>

In my js file, I have this function which creates the window:-在我的 js 文件中,我有这个 function 它创建了 window:-

function PrintElem(elem, h1)
{
      Popup($(elem).html(), h1);
}

function Popup(data, h1) 
{
    var mywindow = window.open('', 'my div', 'height=768,width=1024');
    mywindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
    mywindow.document.write('<html xmlns="http://www.w3.org/1999/xhtml">');
    mywindow.document.write('<html><head><title></title>');
    mywindow.document.write('<link rel="stylesheet" href="'+base_url+'/css/print1.css'+'" type="text/css" />');
    mywindow.document.write('<link rel="stylesheet" href="'+base_url+'/assets/global/plugins/font-awesome/css/font-awesome.min.css'+'" type="text/css" />');
    mywindow.document.write('<script src="'+base_url+'js/custom.js'+'" type="text/javascript"></script>');
    mywindow.document.write('</head><body>');
    mywindow.document.write('<h1 class="pdfTitleDv" >'+h1+'</h1>');
    mywindow.document.write($('#window_print').html());
    mywindow.document.write('<table border="0" cellpadding="0" cellspacing="0" class="table table-bordered table-striped table-condensed flip-content table-hover datatableDv " id="sample_editable_1">');
    mywindow.document.write(data);
    mywindow.document.write('</table>');
    mywindow.document.write('</body></html>');
    mywindow.document.close();
    mywindow.focus();
    return true;
}

function print(){
    window.print();
}

I have the following button in the new window which I created there I have a button I want to print the table which is in the window.我在那里创建的新 window 中有以下按钮 我有一个按钮我想打印 window 中的表格。 please see the HTML below:-请参阅下面的 HTML:-

<div class="btn-group pull-right btm-btn">
   <a href="javascript:void(0);" class="btn btn-lg green" onclick="print()"><i class="fa fa-print" aria-hidden="true" style="padding-right: 5px;"></i>Print</a> 
</div>

I want to print the table which is in the new window.我想打印新 window 中的表格。 Can anyone help me to achieve this??谁能帮我实现这一目标? Any help will be appreciated.任何帮助将不胜感激。

Add window.print() in your print function, It will push it to print forcefully.在您的打印 function 中添加window.print() ,它将强制打印。

You need to add: mywindow.print() .您需要添加: mywindow.print() Because window refers to the current window, you need to print the new window.因为window指的是当前的window,所以需要打印新的window。 So, make mywindow variable global and call print method.因此,将mywindow变量设为全局变量并调用 print 方法。

var mywindow;
function Popup(data, h1) 
{
    mywindow = window.open('', 'my div', 'height=768,width=1024');
   //rest of your code
} 
function print(){
    mywindow.print();
}

Demo演示

try to use "innerHTML" rather than "document.write" with `` back-ticks like this example:尝试使用带有“反引号”的“innerHTML”而不是“document.write”,如下例所示:

<!DOCTYPE html>
<html>
<body>

<p></p>

<button onclick="myFunction()">click here to open the new window</button>

<script>
function myFunction() {
   mywindow = window.open('', '', 'height=768,width=1024');

  mywindow.document.body.innerHTML = `<table style="width:100%">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
</table>`;

  mywindow.window.print();
}
</script>

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM