JSFiddle - React, Tailwind, and code Playground
HTML
<script>
function printfoobarHTML() {
var pr_cont = '<h1>Hello world HTML</h1>';
var iframe = document.getElementById('printframe');
var doc = iframe.contentWindow.document
doc.open();
doc.write(pr_cont);
doc.close();
}
function printfoobarXHTML() {
var pr_cont = '<h1>Hello world XHTML</h1>';
var iframe = document.getElementById('printframe');
iframe.src = 'data:application/xhtml+xml; charset=utf-8,' + pr_cont;
}
function frload() {
var iframe = document.getElementById('printframe');
iframe.contentWindow.print();
}
</script>
<iframe id="printframe" name="printframe" style="width:1px;height:1px;border:0;position: absolute; top: -10px; left: -10px;" onload="frload()"></iframe>
<button onclick="printfoobarHTML()">Print</button>
<button onclick="printfoobarXHTML()">Print</button>