index.js 982 Bytes
Newer Older
陈龙's avatar
陈龙 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import React from 'react';
import ReactDOM from 'react-dom';
import Simple_table from './modal/simple_table';

const Print = (data, printTemp) => {
  //创建iframe标签
  const iframe = document.createElement('iframe');

  iframe.setAttribute(
    'style',
    'position:absolute;width:0px;height:0px;left:500px;top:500px;size:auto;margin:0mm;',
  );

  //将iframe添加到body
  document.body.appendChild(iframe);

  //创建打印内容document对象
  let doc = iframe.contentWindow.document;
  let tem = '';

  switch (printTemp) {
    case 'simple_table':
      tem = <Simple_table data={data}/>;
      break;
    default:
      tem = <Simple_table data={data}/>;
  }
  ReactDOM.render(tem, doc);

  setTimeout(() => {
    doc.close();

    //打印
    iframe.contentWindow.focus();
    iframe.contentWindow.print();

    //清理iframe
    if (navigator.userAgent.indexOf('MSIE') > 0) {
      document.body.removeChild(iframe);
    }
  }, 1000);
};

export default Print;