I want to print contents from Div in vaadin and what I have done is print the innerHTML of the div using script which is shown below:
String script = "var p = document.getElementById('print-area');" +
"if(p == null) window.innerHTML.print(); " +
"else {" +
" var mywindow = window.open('', 'PRINT', 'height=400,width=600');\n" +
" mywindow.document.write('<html><head><title>' + document.title + '</title>');\n" +
" mywindow.document.write('</head><body >');\n" +
" mywindow.document.write('<h1>' + document.title + '</h1>');\n" +
" mywindow.document.write(p.innerHTML);\n" +
" mywindow.document.write('</body></html>');\n" +
" mywindow.document.close(); // necessary for IE >= 10\n" +
" mywindow.focus(); // necessary for IE >= 10*/\n" +
" mywindow.print();\n" +
" mywindow.close();" +
"}";
This script prints the contents of the Div but it removes all the css class applied on it. So, is there any better way to do it ?
Or How can I print all the contents of the Div with css applied on it ?
Update:
By using media query taking reference from here:
How to print a Component in Vaadin?
My Code:
Div div = new Div();
div.addClassName("printable");
Print Button:
UI.getCurrent().getElement().executeJs("print();")
Css
@media print {
body, body * {
visibility: hidden;
}
.printable, .printable * {
visibility: visible;
}
.printable {
position: absolute;
left: 0;
top: 0;
}
}
It Shows all the css applied to it but gives more space as shown in picture below.
But If I manually decrease the Scale shown in the dialog and set it to 50-75 it seems working.
So, how can it be fixed ?