Skip to content

Instantly share code, notes, and snippets.

@noizbuster
Created November 19, 2020 00:11
Show Gist options
  • Save noizbuster/8366bd0c44258789a1b0e5ab57a7c11d to your computer and use it in GitHub Desktop.
Save noizbuster/8366bd0c44258789a1b0e5ab57a7c11d to your computer and use it in GitHub Desktop.
how to print react component as full-size
function ReactPage(props) {
const printingTarget = useRef(null);
const iframe4print = useRef(null);
function onPrintButtonClick() {
let content = printingTarget.current;
let pri = iframe4print.current.contentWindow;
pri.document.open();
pri.document.write(`<html lang="ko">${document.head.innerHTML}<body>${content.innerHTML}</body></html>`);
pri.document.close();
pri.focus();
pri.print();
}
return (
<div>
<Button style={{ width: '100%' }} variant="contained" color="primary" onClick={() => onPrintButtonClick()}>
print
</Button>
<div ref={printingTarget}>
<SimpleDataTable data={queryResult} column={assResultColumn} />
</div>
<iframe
title="printer"
ref={iframe4print}
style={{ display: 'none', height: '0px', width: '0px', position: 'absolute' }}
/>
</div>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment