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;
|