jquery print web page

1. Quote js

<script src="../js/jquery-1.11.3.min.js"></script>
<script src="../js/jquery-migrate-1.1.0.js"></script>
<script src="../js/jquery.jqprint-0.3.js">

Note that the version of jquery needs to be 1, and the higher version has compatibility issues

2. Add a div as the print area and set the id to printarea

3. Print this area

function printtable(){
    $("#printarea").jqprint({
	debug: false, //如果是true则可以显示iframe查看效果(iframe默认高和宽都很小,可以再源码中调大),默认是false
	importCSS: true, //true表示引进原来的页面的css,默认是true。(如果是true,先会找$("link[media=print]"),若没有会去找$("link")中的css文件)
	printContainer: true, //表示如果原来选择的对象必须被纳入打印(注意:设置为false可能会打破你的CSS规则)。
	operaSupport: true//表示如果插件也必须支持歌opera浏览器,在这种情况下,它提供了建立一个临时的打印选项卡。默认是true
    });	
}

4. Remove header and footer under IE

var HKEY_Root,HKEY_Path,HKEY_Key;
	HKEY_Root="HKEY_CURRENT_USER";
	HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
	//设置网页打印的页眉页脚为空
	function PageSetup_Null()
	{
		try
		{
			var Wsh=new ActiveXObject("WScript.Shell");
			HKEY_Key="header";
			Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
			HKEY_Key="footer";
			Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
		}
		catch(e){}
	}
	//设置网页打印的页眉页脚为默认值 
	function pagesetup_default(){ 
		try{ 
			var RegWsh = new ActiveXObject("WScript.Shell") 
			hkey_key="header" 
			RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b页码,&p/&P") 
			hkey_key="footer" 
			RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d") 
		}catch(e){} 
	}

Modify the printing code

function printtable(){
		PageSetup_Null();  
//		pagesetup_default();
		$("#printarea").jqprint({
			debug: false, //如果是true则可以显示iframe查看效果(iframe默认高和宽都很小,可以再源码中调大),默认是false
			importCSS: true, //true表示引进原来的页面的css,默认是true。(如果是true,先会找$("link[media=print]"),若没有会去找$("link")中的css文件)
			printContainer: true, //表示如果原来选择的对象必须被纳入打印(注意:设置为false可能会打破你的CSS规则)。
			operaSupport: true//表示如果插件也必须支持歌opera浏览器,在这种情况下,它提供了建立一个临时的打印选项卡。默认是true
		});	
		pagesetup_default();
	}

5.chrome remove the header and footer, as shown in the figure below, remove the check in the red box

image.png

Guess you like

Origin blog.csdn.net/hifhf/article/details/107311543