Created
November 19, 2020 00:11
-
-
Save noizbuster/8366bd0c44258789a1b0e5ab57a7c11d to your computer and use it in GitHub Desktop.
how to print react component as full-size
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()}> | |
</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