对于以下代码是否有好的解决方法,可以用于打印到不同的打印机,如收据打印机,身份证打印机和喷墨/激光打印机?我不想使用pdf来保存数据和打印,而是直接从web表单或数据库打印。
我试过了,但是没有成功。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<script type="text/javascript">
function Print(divID)
{
//Get the HTML of div
var divElements = document.getElementById(divID).innerHTML;
//Get the HTML of whole page
var oldPage = document.body.innerHTML;
//Reset the page's HTML with div's HTML only
document.body.innerHTML = "<html><head><title></title></head><body>" + divElements + "</body>";
//Print Page
window.print();
//Restore orignal
HTML document.body.innerHTML = oldPage;
}
</script>
<input type="button" value="Print" onClick="javascript:Print('print')" />
<div id="print"> Content to be printed </div>
</body>
</html>
<script>
function Print(divID) {
//Get the HTML of div
var divElements = document.getElementById(divID).innerHTML;
//Get the HTML of whole page
var oldPage = document.body.innerHTML;
//Reset the page's HTML with div's HTML only
document.body.innerHTML =
"<html><head><title></title></head><body>" +
divElements + "</body>";
//Print Page
window.print();
//Restore orignal HTML
document.body.innerHTML = oldPage;
}
</script>
<input type="button" value="Print" onclick="javascript:Print('print')" />
<div id="print">
Content to be printed
</div>