我在页面上有一个带有以下 Greasemonkey (Tampermonkey) 代码的信息亭,用于启用收据打印:
$("input").css("font-size", "xx-large");
$("#newcheckout").append ('<div id="autoCheckOrder"></div>');
$("#autoCheckOrder").append ('<button>Print Receipt?</button>');
var loanTable = $("#loanTable")
var loanHTML = loanTable.html().replace(/(\d{2}\/\d{2}\/\d{4})/g, "<br><b>$1</b>");
$("#autoCheckOrder button").click ( function () {
var newWin = window.open ("");
newWin.document.write ( "<!DOCTYPE html>" );
newWin.document.write( "<html>" );
newWin.document.write( "<head>" );
newWin.document.write( "<title>" );
newWin.document.write( "</title>" );
newWin.document.write( "<style>" );
newWin.document.write( "@media print { " );
newWin.document.write( "body { " );
newWin.document.write( "background-color: white;" );
newWin.document.write( "height: 100%;" );
newWin.document.write( "width: 60mm;" );
newWin.document.write( "position: fixed;" );
newWin.document.write( "top: 0;" );
newWin.document.write( "left: 0;" );
newWin.document.write( "margin: 0;" );
newWin.document.write( "padding: 15px;" );
newWin.document.write( "font-size: 14px;" );
newWin.document.write( "line-height: 18px;" );
newWin.document.write( "}" );
newWin.document.write( "}" );
newWin.document.write( "</style>" );
newWin.document.write( "</head>" );
newWin.document.write( "<body>" );
newWin.document.write ( loanHTML );
newWin.document.write( "</body>" );
newWin.document.write( "</html>" );
if(newWin.print()) {
newWin.close();
} else {
newWin.close();
}
} );
这在 Firefox 中运行良好——而不是在 Chrome beta 19 中。今天刚刚更新。您按下“打印”按钮,打印预览出现并显示:
打印预览失败,请使用系统对话框。
更新
请参阅下面的 Brock Adam 的回答,然后是下面的工作代码(仅 CSS 为简洁起见):
@media print {
body {
background-color: white;
width: 55mm;
position: absolute;
top: 0;
left: 0;
padding: 0;
...ETC。