簡體   English   中英

在新標簽頁中打印內容將打印空白頁

[英]printing contents in a new tab prints a blank page

我正在嘗試將div的內容打印到新選項卡,但是,它僅是第一次打印空白頁。 當我手動打印頁面時,它工作正常。 我正在加載2表包裝在一個div中。

HTML結構:

<div class="receipt_bg" id="print_receipt">
  <div id="div1">
    table1
  </div>
  <div id="div2">
    table2
  </div>
</div>
<button type="button" id="print_btn">Print</button>

Javascript:

$('#print_btn').click(printClick);

function printClick(receipt_bg) {
  var DocumentContainer = $('.receipt_bg').html();
  var WindowObject = window.open("PrintWindow","_blank");
  WindowObject.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">'+ '<html><head><title>test print receipt</title>    <link href="../css/style.defaultprint.css" rel="stylesheet" type="text/css" /></head><body>' +     DocumentContainer + '</body></html>')
  setTimeout(WindowObject.print(), 5);
  WindowObject.close();
}

我什至嘗試使用WindowObject.onload = WindowObject.print(); 而不是setTimeout(WindowObject.print(),5); 仍然似乎不起作用。 我看到了類似的帖子,但是它的解決方案對我不起作用。

檢查此URL。 它包含一些從頁面打印內容的示例,它具有您所需要的一切。

我曾經使用jQuery Print Element 1.2進行打印,在這種情況下,如果您要打印大量內容,則需要將超時時間從50增加到500,否則,當您遇到問題時,它將顯示空白頁面,但是對我來說,這僅僅是發生在Chrome瀏覽器中,除此之外我還沒有遇到這個問題。 檢查printElement js。 網址下方包含帶有演示和源代碼的漂亮示例。

檢查此: jQuery打印頁面選項

這是代碼,它將為您提供幫助。

function printClick(receipt_bg) {
    var DocumentContainer = $('.receipt_bg').html();
    var WindowObject = window.open("", "printElementWindow", "width=650,height=440,scrollbars=yes");
    WindowObject.document.writeln('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">'+ '<html><head><title>test print receipt</title>    <link href="../css/style.defaultprint.css" rel="stylesheet" type="text/css" /></head><body>' +     DocumentContainer + '</body><script type="text/javascript">setTimeout(printPage(),1000);  function printPage(){focus();print();}<\/script></html>')
    WindowObject.close();
 }

這將為您工作,在這里我向您的窗口代碼添加了一個JavaScript函數,該函數將在超時后執行。 嘗試一下,讓我知道,它是否對您有用。 您可以根據您的內容延長或減少超時。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM