简体   繁体   English

如何使用JavaScript在没有PrintPreview的情况下打印div

[英]How to print a div without PrintPreview using javascript

I want to print a particular div without showing preview window. 我想在不显示预览窗口的情况下打印特定的div。 Please help 请帮忙

function PrintInvoice() {
        $(document).ready(function () {
            var toPrint = document.getElementById('div_print');
            var popupWin = window.open('', '_blank', 'width=1000,location=no,scrollbars=1,tollbar=0,top=0,left=200px,menubar=no,resizable=no');
            popupWin.document.open();
            popupWin.document.write('<html><body onload="window.print()">')
            popupWin.document.write(toPrint.innerHTML);
            popupWin.document.write('</html>');
            popupWin.document.close();
            popupWin.focus();
            popupWin.print();
            popupWin.close();
        });
    }

Above code opens a new window first and then print dialogue is showing. 上面的代码首先打开一个新窗口,然后显示打印对话框。 I dont want to open a new window 我不想打开一个新窗口

Try This one 试试这个

<head>
<style type="text/css">

#printable { display: none; }

@media print
{
    #non-printable { display: none; }
    #printable { display: block; }
}
</style>
</head>
<body>
<div id="non-printable">
    Your normal page contents
</div>

<div id="printable">
    Printer version
</div>

Check this. 检查一下。

<form>
  <input type="button" value="Print Page" onClick="window.print()">
</form>

<script language="VBScript">
  // THIS VB SCRIP REMOVES THE PRINT DIALOG BOX AND PRINTS TO YOUR DEFAULT PRINTER
  Sub window_onunload()
  On Error Resume Next
  Set WB = nothing
  On Error Goto 0
  End Sub

  Sub Print()
  OLECMDID_PRINT = 6
  OLECMDEXECOPT_DONTPROMPTUSER = 2
  OLECMDEXECOPT_PROMPTUSER = 1


  On Error Resume Next

  If DA Then
  call WB.ExecWB(OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER,1)

  Else
  call WB.IOleCommandTarget.Exec(OLECMDID_PRINT ,OLECMDEXECOPT_DONTPROMPTUSER,"","","")

  End If

  If Err.Number <> 0 Then
   If DA Then 
    Alert("Nothing Printed :" & err.number & " : " & err.description)
  Else
  HandleError()
 End if
 End If
 On Error Goto 0
 End Sub

If DA Then
  wbvers="8856F961-340A-11D0-A96B-00C04FD705A2"
Else
   wbvers="EAB22AC3-30C1-11CF-A7EB-0000C05BAE0B"
End If

document.write "<object ID=""WB"" WIDTH=0 HEIGHT=0 CLASSID=""CLSID:"
document.write wbvers & """> </object>"

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

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