Print <div id=printarea></div> only?
Here is a general solution, using CSS only, which I have verified to work.
@media print {
body * {
visibility: hidden;
}
#section-to-print, #section-to-print * {
visibility: visible;
}
#section-to-print {
position: absolute;
left: 0;
top: 0;
}
}
Alternative approaches aren't so good. Using display
is tricky because if any element has display:none
then none of its descendants will display either. To use it, you have to change the structure of your page.
Using visibility
works better since you can turn on visibility for descendants. The invisible elements still affect the layout though, so I move section-to-print
to the top left so it prints properly.
Print the contents of a DIV
Slight changes over earlier version - tested on CHROME
function PrintElem(elem)
{
var mywindow = window.open('', 'PRINT', 'height=400,width=600');
mywindow.document.write('<html><head><title>' + document.title + '</title>');
mywindow.document.write('</head><body >');
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write(document.getElementById(elem).innerHTML);
mywindow.document.write('</body></html>');
mywindow.document.close(); // necessary for IE >= 10
mywindow.focus(); // necessary for IE >= 10*/
mywindow.print();
mywindow.close();
return true;
}
Print <div id=printarea></div> only?
Here is a general solution, using CSS only, which I have verified to work.
@media print {
body * {
visibility: hidden;
}
#section-to-print, #section-to-print * {
visibility: visible;
}
#section-to-print {
position: absolute;
left: 0;
top: 0;
}
}
Alternative approaches aren't so good. Using display
is tricky because if any element has display:none
then none of its descendants will display either. To use it, you have to change the structure of your page.
Using visibility
works better since you can turn on visibility for descendants. The invisible elements still affect the layout though, so I move section-to-print
to the top left so it prints properly.
Print <div id=printarea></div> only?
Here is a general solution, using CSS only, which I have verified to work.
@media print {
body * {
visibility: hidden;
}
#section-to-print, #section-to-print * {
visibility: visible;
}
#section-to-print {
position: absolute;
left: 0;
top: 0;
}
}
Alternative approaches aren't so good. Using display
is tricky because if any element has display:none
then none of its descendants will display either. To use it, you have to change the structure of your page.
Using visibility
works better since you can turn on visibility for descendants. The invisible elements still affect the layout though, so I move section-to-print
to the top left so it prints properly.
How to print React component on click of a button?
There is kind of two solutions on the client. One is with frames like you posted. You can use an iframe though:
var content = document.getElementById("divcontents");
var pri = document.getElementById("ifmcontentstoprint").contentWindow;
pri.document.open();
pri.document.write(content.innerHTML);
pri.document.close();
pri.focus();
pri.print();
This expects this html to exist
<iframe id="ifmcontentstoprint" style="height: 0px; width: 0px; position: absolute"></iframe>
The other solution is to use the media selector and on the media="print"
styles hide everything you don't want to print.
<style type="text/css" media="print">
.no-print { display: none; }
</style>
Last way requires some work on the server. You can send all the HTML+CSS to the server and use one of many components to generate a printable document like PDF. I've tried setups doing this with PhantomJs.
Forcing a page break only when necessary during print
Terns out that embedding divs are not supported for page-break-inside:avoid. In order to accomplish what I was trying to do, I needed to convert my html to a table and surround it with a single div. This also works on FF(38.0.5), IE(11), and Chrome(43.0.2357.130 m)
<div style="page-break-inside: avoid;">
<table style="width:100%;">
<tbody>
<tr>
<td style="width:400px;">
<h2>**Delinquency Notice** as of 12/31/13</h2>
</td>
<td style="width:400px;">
<h2>Recent Account History</h2>
</td>
</tr>
<tr>
<td class="padding"><b>You are late on your mortgage payments.</b>
As of 4/17/2015, you are 106 days late on your mortgage payments.
Failure to bring your loan current may result in fees and
foreclosure -- the loss of your home.<br/><br/>If you are experiencing financial difficulty, HUD
(U.S. Dept
of Housing and Urban Development) approved counseling agents
are available to provide mortgage and foreclosure counseling
assistance. Refer to the HUD disclosure section on the
statement front for contact information.
</td>
<td class="padding">
<ul style="list-style-type: disc;">
<li>Payment due 12/01/2014: Fully paid on 11/30/2014</li>
<li>Payment due 1/01/2015: Unpaid balance of $1,290.02</li>
<li>Payment due 2/01/2015: Fully paid on 1/31/2015</li>
<li>Payment due 3/01/2015: Fully paid on 2/28/2015</li>
<li>Payment due 4/01/2015: Fully paid on 3/17/2015</li>
<li>Current payment due 5/01/2015: $1,290.02</li>
</ul>
<br/><b>Total: $6,495.86 due. You must pay this amount to bring your loan current</b></td>
</tr>
</tbody>
</table>
</div>
Related Topics
How to Make an HTML Text Box Show a Hint When Empty
Jquery: Why Use Document.Ready If External Js At Bottom of Page
How to Show Popup Message Like in Stack Overflow
How to Retrieve the Display Property of a Dom Element
How to Add Multiple Classes to a Reactjs Component
How to Remove CSS Property Using JavaScript
What Is the Explicit Promise Construction Antipattern and How to Avoid It
Compare Two Dates With JavaScript
How to Clear the Content of a Div Using JavaScript
Creating Svg Elements Dynamically With JavaScript Inside Html
How to Generate a Simple Popup Using Jquery
How to Pause and Resume Css3 Animation Using JavaScript
Disable Drag and Drop on HTML Elements