如何使用javascript打印没有PrintPreview的div

时间:2013-07-23 11:15:42

标签: javascript asp.net

我想在不显示预览窗口的情况下打印特定的div。请帮忙

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

上面的代码首先打开一个新窗口,然后显示打印对话框。我不想打开一个新窗口

2 个答案:

答案 0 :(得分:0)

试试这个

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

答案 1 :(得分:-1)

检查一下。

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