admin
2025-03-29 9cd0e111d7315074f5574558dab3cae087ff1f68
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
45
46
47
48
49
50
51
52
const Print = function(options) {
  const defaults = {
    id: '',
    popTitle: '',
    beforePrint: () => {},
    callback: () => {}
  };
 
  const settings = Object.assign({}, defaults, options);
 
  const $element = settings.id;
  const html = $element.innerHTML;
 
  const div = document.createElement('div');
  div.innerHTML = html;
 
  const style = document.createElement('style');
  style.innerHTML = `
    body {
      margin: 0;
      padding: 0;
    }
    @page {
      margin: 10mm;
      size: auto;
    }
  `;
 
  const iframe = document.createElement('iframe');
  iframe.setAttribute('style', 'position:absolute;width:0;height:0;left:-500px;top:-500px;');
  document.body.appendChild(iframe);
 
  const doc = iframe.contentWindow.document;
  doc.write('<html><head><title>' + settings.popTitle + '</title>');
  doc.write(style.outerHTML);
  doc.write('</head><body>');
  doc.write(div.innerHTML);
  doc.write('</body></html>');
  doc.close();
 
  settings.beforePrint();
 
  iframe.contentWindow.focus();
  iframe.contentWindow.print();
 
  setTimeout(() => {
    document.body.removeChild(iframe);
    settings.callback();
  }, 100);
};
 
export default Print;