-
-
Save andrewlimaza/490a69417d9fe2df3f668195a7661605 to your computer and use it in GitHub Desktop.
<script> | |
function printDiv(divName){ | |
var printContents = document.getElementById(divName).innerHTML; | |
var originalContents = document.body.innerHTML; | |
document.body.innerHTML = printContents; | |
window.print(); | |
document.body.innerHTML = originalContents; | |
} | |
</script> | |
<h1> do not print this </h1> | |
<div id='printMe'> | |
Print this only | |
</div> | |
<button onclick="printDiv('printMe')">Print only the above div</button> |
All other js stopped working on DOM after print call.
It's going to override the document and rest of the js can't operate properly.
Maybe a reload of the entire page would be better, instead of that "restore" operation that destroys all attached listeners.
Hi, Actually this method removes all my CSS inside that div, Is there any way to print that div as it is?
.printme {
display: none;
}
@media print {
.no-printme {
display: none;
}
.printme {
display: block;
}
}
<h1 class = "no-printme"> do not print this </h1>
<div class='printme'>
Print this only
</div>
<button onclick={window.print()}>Print only the above div</button>
Just use this css class its the perfect solution for this usecase
Above things works work perfects but i am struck with how to select specific printer while doing window.print(),, Any body can help me? Thanks in advance
but this not work of input content after changed
Hello Sir this code is not working content will changed
Thank you!
` <s:text name="tituloTramite" />
<legend class="L_secundaria"><s:text name="datosSolicitud" /></legend>
<tiles:insertDefinition name="mensajes" />
<div class="row">
<tiles:insertDefinition name="datosSolicitudEstatico" />
</div>
<s:form method="post" namespace="/clave" action="descargarPaso3"
enctype="multipart/form-data" id="formPaso3">
</s:form>`
I use tag-tiles to reference an embedded JSP that has multiple
.printme { display: none; } @media print { .no-printme { display: none; } .printme { display: block; } } <h1 class = "no-printme"> do not print this </h1> <div class='printme'> Print this only </div> <button onclick={window.print()}>Print only the above div</button>
Just use this css class its the perfect solution for this usecase
thank you. it worked
.printme { display: none; } @media print { .no-printme { display: none; } .printme { display: block; } } <h1 class = "no-printme"> do not print this </h1> <div class='printme'> Print this only </div> <button onclick={window.print()}>Print only the above div</button>
Just use this css class its the perfect solution for this usecase
thank you. it worked
Thanks its work properly.
Hello everyone, the above code works perfectly. In my case I am hiding the sidebar to the left of the frame that I want to print, question here, is there any way I can make the frame as big as the page at printing time?
just
var printContents = document.getElementById(divName).innerHTML;
change
var printContents = document.getElementById(divName).outerHTML;
css will not break.
Thanks :D
If you're using bootstrap, just add classname d-print-none to the elements you don't want to display in print
If you're using bootstrap, just add classname d-print-none to the elements you don't want to display in print
Great! Didn't even think that bootstrap would have this.
this code worked fine, but it's not working for input fields. Input field values are not printing any solution? @TranQuocViet
function printPageArea(areaID){
var printContent = document.getElementById(areaID);
var WinPrint = window.open('', '', 'width=900,height=650');
WinPrint.document.write(printContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
}
link - https://www.codexworld.com/print-page-area-javascript/
My approach - Simple CSS and JS. Works on React/NextJS too.
const handlePrint = e => {
e.preventDefault();
const bodyElement = document.getElementsByTagName('body')[0];
bodyElement.classList.add('printing');
window.print();
bodyElement.classList.remove('printing');
};
.printing {
visibility:hidden;
}
.printView {
visibility:visible;
}
.printing .printView {
/* You can have any CSS here to make the view better on print */
position:absolute;
top:0;
}
What it does?
- Adds
.printing
class to the body element. With CSS, we hide all body content withvisibility:hidden;
- At the same time, we keep CSS ready with
.printing .printView
to have any kind of view we want for the print area. - Trigger
window.print();
- Remove
.printing
class from the body element when the user cancels / prints.
Example:
<button onclick="handlePrint">
Download PDF
</button>
<div>
<h1>Don't print this</h1>
<div class="printView">Print this</div>
</div>
Let me know if this helps anyone :)
Edit: If it works for you, please vote on SO - https://stackoverflow.com/a/72896189/11058652
Is there any ways to automate the download pdf option after window.print() ?
@CodingCreate101 your solution worked for me thanks.
@dhruvroytalukdar , you are welcome. Vote on SO? - https://stackoverflow.com/a/72896189/11058652
This is a great solution but it breaks the Dom. nothing works after. is there a way to make it open a new tab to do this ?
Here's a proper, modern solution:
export function printElement(element: HTMLElement) {
const body = document.body
// Store current children of the body in an array
const originalChildren: Node[] = [...body.children]
// Clone the element to be printed
const elementClone = element.cloneNode(true) as HTMLElement
// Remove all current children from the body
while (body.firstChild) {
body.firstChild.remove()
}
// Add the cloned element to be printed
body.append(elementClone)
// Call window.print and revert the state back after printing
window.print()
// Revert the state back
while (body.firstChild) {
(body.firstChild as HTMLElement).remove()
}
for (const child of originalChildren) body.append(child)
}
By keeping a reference to the nodes that have been temporarily removed from the DOM, they can be added back without an issue: All listeners and references to these nodes are still valid.
@Vanilagy not sure how you got this one to work but i get an error Type 'HTMLCollection' must have a '[Symbol.iterator]()' method that returns an iterator
at [...body.children]
and when i run the function i get element.cloneNode is not a function
in the inspector console
Hi This is vidhya we are trying to print price tag for the product from browser application. Not able to get the desired format while doing it from browser but it is possible from desktop application. Tried with PNR file format, and PDF File format still not getting the desired alignment.
here is the PNR file format
I8,A
q540
O
JF
WN
ZT
Q80,25
N
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"BFAY00037"
A510,45,2,2,1,1,N,"BFAY00037"
A303,53,2,2,1,1,N,"10.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"BFAY00038"
A510,45,2,2,1,1,N,"BFAY00038"
A303,53,2,2,1,1,N,"10.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"OG00012"
A510,45,2,2,1,1,N,"OG00012"
A303,53,2,2,1,1,N,"10.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"SK00001"
A510,45,2,2,1,1,N,"SK00001"
A303,53,2,2,1,1,N,"35.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"OS00001"
A510,45,2,2,1,1,N,"OS00001"
A303,53,2,2,1,1,N,"20.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"FK00025"
A510,45,2,2,1,1,N,"FK00025"
A303,53,2,2,1,1,N,"10.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"MOS00001"
A510,45,2,2,1,1,N,"MOS00001"
A303,53,2,2,1,1,N,"9.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"MOS00002"
A510,45,2,2,1,1,N,"MOS00002"
A303,53,2,2,1,1,N,"15.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"MOS00003"
A510,45,2,2,1,1,N,"MOS00003"
A303,53,2,2,1,1,N,"19.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B520,80,2,1C,1,2,30,N,"MOS00004"
A510,45,2,2,1,1,N,"MOS00004"
A303,53,2,2,1,1,N,"40.000 Gms"
P1
A343,53,2,2,1,1,N,"Wt:"
B517,60,2,1C,1,2,30,N,"000000"
A500,24,2,2,1,1,N,"000000"
A303,53,2,2,1,1,N,"0.000"
P1
Printer URL --- https://www.tvs-e.in/label-printers/lp-46-lite/-;.- What should be done to get the desired alignment would be a great help. Thank you in advanced
Use NPM react-to-print
It will print your desired format, any tag you like with your custom alignments.
https://www.npmjs.com/package/react-to-print
If you want example code it can provide you with that
@azeenGAN Thank you so much. I still can't get the desired alignment, is there any other options to do?
I set @page size 2in,3in but is only work chrome not other? If any idea?
Source taken from - http://stackoverflow.com/questions/468881/print-div-id-printarea-div-only