Печать с помощью кнопки из window.open - PullRequest
0 голосов
/ 08 ноября 2019

У меня есть следующая кнопка, которая открывает новое окно

<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>

В моем файле js у меня есть эта функция, которая создает окно: -

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();
}

У меня есть следующеекнопка в новом окне, которое я создал там, у меня есть кнопка, я хочу напечатать таблицу, которая находится в окне. пожалуйста, посмотрите 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>

Я хочу напечатать таблицу, которая находится в новом окне. Может ли кто-нибудь помочь мне добиться этого ?? Любая помощь будет оценена.

Ответы [ 3 ]

0 голосов
/ 08 ноября 2019

Вам необходимо добавить: mywindow.print(). Поскольку окно ссылается на текущее окно, вам необходимо напечатать новое окно. Итак, сделайте mywindow переменную global и вызовите метод 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

0 голосов
/ 08 ноября 2019

попробуйте использовать "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>
0 голосов
/ 08 ноября 2019

Добавьте window.print() в вашу функцию печати, это заставит ее печатать принудительно.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...