Browse Source

打印测测

Luolei 6 years ago
parent
commit
daf66fe8c5
1 changed files with 28 additions and 10 deletions
  1. 28 10
      src/common/components/ComplexModal/index.jsx

+ 28 - 10
src/common/components/ComplexModal/index.jsx

@@ -76,6 +76,7 @@ class ComplexModal extends Component {
   constructor(props) {
     super(props);
     this.onPrint = this.onPrint.bind(this);
+    this.doPrint3 = this.doPrint3.bind(this);
   }
   
   onPrint() {
@@ -92,29 +93,46 @@ class ComplexModal extends Component {
     let domWrap = document.createElement('div')
     prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));//从结束代码向前取html 
     domWrap.innerHTML=prnhtml; 
-    console.log(domWrap) 
-    $(domWrap).jqprint({
-      debug: false,
-      importCSS: true,
-      printContainer: true,
-      operaSupport: false,
-    });
+    console.log(domWrap)
     // window.document.body.innerHTML=prnhtml;  
     // window.print()
     // console.log(prnhtml)
   }
+  doPrint3(){
+  
+    //判断iframe是否存在,不存在则创建iframe
+    var iframe=document.getElementById("print-iframe");
+    if(!iframe){  
+            var el = document.getElementById("printcontent");
+            iframe = document.createElement('IFRAME');
+            var doc = null;
+            iframe.setAttribute("id", "print-iframe");
+            iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;');
+            document.body.appendChild(iframe);
+            doc = iframe.contentWindow.document;
+            //这里可以自定义样式
+            //doc.write("<LINK rel="stylesheet" type="text/css" href="css/print.css">");
+            doc.write('<div>' + el.innerHTML + '</div>');
+            doc.close();
+            iframe.contentWindow.focus();            
+    }
+    iframe.contentWindow.print();
+    if (navigator.userAgent.indexOf("MSIE") > 0){
+        document.body.removeChild(iframe);
+    } 
+  }
 
   render() {
     const { onclose,title,children,footer,shadeClose,icon,width,top,bottom} = this.props;
     const marginLeft = width? -parseInt(width)/2 : '';
     //const marginTop = height? -parseInt(height)/2: '';
     const domNode = document.getElementById('root');
-    return ReactDom.createPortal(<div className={style['container']} id="contents">
+    return ReactDom.createPortal(<div className={style['container']} id="printcontent">
       <div className={style['shade']} onClick={shadeClose===false?'':onclose}></div>
 
       <i>111111</i>
-      <div className={style['modal']} style = {{width: width?width:'auto', marginLeft:marginLeft, top:top?top+'px':'',bottom:bottom?bottom+'px':''}}>
-        <div className={style['close']}  onClick={this.onPrint}>
+      <div className={style['modal']} style={{width: width?width:'auto', marginLeft:marginLeft, top:top?top+'px':'',bottom:bottom?bottom+'px':''}}>
+        <div className={style['close']}  onClick={this.doPrint3}>
           {icon?<img src={icon} />:''}
           {title}
           <img src={close} onClick={onclose} className={style['closeIcon']} />