js列印分頁 去頁尾 頁邊距
阿新 • • 發佈:2019-02-07
做檔案系統的時候。需要列印多張圖片。客戶使用ie9-11 背景如上。下面是開啟列印頁面。使用了ie自帶的execwb
圖片的html累加完後再開啟新頁面if(format==0){ for(var i=0;i<pageArray.length;i++){ if (i==0){ firstPrintPageno=pageArray[i]; } pagenum=pageArray[i]; var fileName=viewOtherPages(pagenum);//傳遞是第幾張圖片 // sourceFile=viewPath+"/"+encodetoASCII(fileName.split(".")[0])+".tif";//中文件號的列印 sourceFile=viewPath+"/"+fileName; var xx=img.src.replace(".jpg",".tif");//因為格式有2種 var iim=new Image(); iim.src=img.src; if(!IsExist(img.src))//判斷格式是否正確 { iim.src=xx; } var wdd=iim.width; var htt=iim.height; while (iim.naturalWidth==0)//判斷圖片的寬高是否載入完 { } //計算寬高 if(iim.naturalWidth>700) { var wwd=700/iim.naturalWidth; wdd=iim.naturalWidth*wwd; htt=iim.naturalHeight*wwd; if(htt>1100) { var hht=1100/iim.naturalHeight; wdd=wdd*hht; htt=htt*hht; } }else if(iim.naturalHeight>1100) { var hht=1100/iim.naturalHeight; wdd=iim.naturalWidth*hht; htt=iim.naturalHeight*hht; } //設定傳過去的html if(i==0){ printimg="<img src="+iim.src+"; width='"+wdd+"'; height='"+htt+"'; />"; }else{ printimg=printimg+"<div class='PageNext'></div> <img src="+iim.src+"; width='"+wdd+"'; height='"+htt+"'; />"; // ehView1.appendImage(printFile,sourceFile); }
window.open("printImage.html");
下面是printImage.html<!DOCTYPE html>
下面是設定頁首頁邊距時參考的文章<!DOCTYPE html> <HTML> <HEAD> <meta charset="UTF-8"> <TITLE></TITLE> <META http-equiv=Content-Type content="text/html; charset=gb2312" /> <style media=print> .Noprint{display:none;} .PageNext{page-break-after: always;} </style> <SCRIPT language=javascript> var hkey_root, hkey_path, hkey_key; hkey_root = "HKEY_CURRENT_USER"; hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; //設定網頁列印的頁首頁尾為空 設定頁邊距 function pagesetup_null() { try { var RegWsh = new ActiveXObject("WScript.Shell"); hkey_key = "header"; RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, ""); hkey_key = "footer"; RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, ""); HKEY_Key = "margin_bottom"; //設定下頁邊距(0) RegWsh.RegWrite(hkey_root + hkey_path + HKEY_Key, "0"); HKEY_Key = "margin_left"; //設定左頁邊距(0) RegWsh.RegWrite(hkey_root + hkey_path + HKEY_Key, "0"); HKEY_Key = "margin_right"; //設定右頁邊距(0) RegWsh.RegWrite(hkey_root + hkey_path + HKEY_Key, "0"); HKEY_Key = "margin_top"; //設定上頁邊距(8) RegWsh.RegWrite(hkey_root + hkey_path + HKEY_Key, "0.313"); } catch (e) { } } //這個是我本來直接用問號傳引數時用來獲取引數的方法。後來想起傳過來的值長度有限。列印多張圖片,後面會沒有 function GetQueryString(name) { var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)"); var r = window.location.search.substr(1).match(reg); if(r!=null)return unescape(r[2]); return null; } function printsetup(){ wb.execwb(8,1); } function printpreview(){ //列印預覽 wb.execwb(7,1); } function printit() { //這是直接列印 wb.execwb(6,6) } window.onload=function() { var x=window.opener.printImgtext();//呼叫父視窗的方法 printImgtext() 返回累積的html這個方法要自己寫 document.getElementById("cen").insertAdjacentHTML("afterEnd",x); pagesetup_null();//消去頁尾頁首 wb.execwb(7,1); } </SCRIPT> </HEAD> <BODY> <DIV id='cen' align=center> <OBJECT id=wb height=0 width=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 name=wb></OBJECT> </DIV> </BODY> </HTML>
http://www.cnblogs.com/shenyixin/archive/2013/04/10/3011937.html