打印内容与页面相差较大时,指定打印页面

本文介绍了一个使用HTML、CSS和JavaScript实现网页打印功能的方法。通过特定的样式设置确保打印效果符合需求,并利用JavaScript来控制打印过程及窗口管理。此外,还特别针对IE浏览器进行了页眉页脚的去除操作。

================html=====================

================css======================  

<!-- 打印的样式-->
 <style media="print">
  @page {
  	/* 纵向: */
   size: portrait;
   
   size: auto;
   /* 
   	上外边距是 0mm
	右外边距是 10mm
	下外边距是 0mm
	左外边距是 10mm
   */
   margin: 0mm 10mm 0mm 10mm;
  }
 </style>

================html======================  

<button type="submit" onclick="print2(${list.id});" id="js_print"  class="btn" style="padding:0px;font-size:20px; background:none;outline: none;box-shadow: none;">
     打印
</button>

================js====================== 

        var tata,wind;
        //打印
    	function print2(id) {
            //打开要打印的新窗口
    		wind = window.open('Prints?id='+id,'wind');
            if (!!window.ActiveXObject || "ActiveXObject" in window) {
                remove_ie_header_and_footer();
            }
    	
            //打印
    		wind.print();    

            //打印2秒后关闭新窗口
    		setTimeout(myClose,2000); 
    		
    		
    	 
    	}
    	 function myClose(){
    		wind.opener=null;wind.open('','_self');wind.close();
    	}    

        function remove_ie_header_and_footer() {
       	 	var hkey_root, hkey_path, hkey_key;
hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
        	try {
           	 	var RegWsh = new ActiveXObject("WScript.Shell");
            	RegWsh.RegWrite(hkey_path + "header", "");          //设置页眉为空
            	RegWsh.RegWrite(hkey_path + "footer", "");			//设置页脚为空
        	} catch (e) {}
    	}

==================控制层=====================

@RequestMapping("Prints")
public String prints(Model model,Integer id) {
	model.addAttribute("list", PrService.getList(id));
	return "Print"; 
}

 

======================Print.jsp==========================

<div>

            要打印的内容

</div>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值