Hello team, am using bootstrp template in ASP.NET MVC and when I tried to print with this code it print buttons and the sidebar all together please can one help me. thank in advance.
$('#invoice-printed').click(function(){
Popup($('.invoice')[0].outerHTML);
function Popup(data)
{
window.print();
return true;
}
});

Vishal JoshiRank #377 · 6.7K reputationPosted Mar 19, 2023, 8:13 AM EDT
Hello
Please try below code to get it work
$('#invoice-printed').click(function(){ $('body').css('visibility','hidden'); $('#invoice-printed').css('visibility','visible') window.print(); $('body').css('visibility','visible'); });Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 17, 2023, 11:16 AM EDT
If you want to exclude the sidebar and buttons when printing, you can add some CSS styles to hide them specifically for printing. For example:
@media print { .sidebar, .buttons { display: none; } }You can add this CSS to your page, and it will hide any elements with the classes
sidebarandbuttonswhen the page is printed.Thanks
Naimish Makwana
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Mar 17, 2023, 3:19 PM EDT
Naimish I did but it didn't work, in MVC @ is not working in css I even used @@ but still it underlined it as error
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2023, 9:57 PM EDT
One way to fix this issue is to modify the CSS of the page so that only the content of the "invoice" element is visible when printing. You can do this by adding a print-specific CSS using the
@media printrule.Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2023, 9:59 PM EDT
@media print { .sidebar { display: none; } /* Hide the print button */ #invoice-printed { display: none; } }Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2023, 9:59 PM EDT
Modify your JavaScript code to add the print.css stylesheet to the page before printing
$('#invoice-printed').click(function(){ // Create a link element for the print stylesheet var link = $('', { rel: 'stylesheet', href: '/Content/print.css', type: 'text/css', media: 'print' }); // Add the link element to the head of the document $('head').append(link); // Call the Popup function to print the invoice Popup($('.invoice')[0].outerHTML); // Remove the link element from the head of the document link.remove(); function Popup(data) { window.print(); return true; } });The print.css stylesheet is added to the page before printing, which hides the sidebar and print button.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2023, 10:01 PM EDT
To include a literal @ symbol in your CSS code, you can escape it using a backslash (\@).
@media print { /* Hide the sidebar */ .sidebar { display: none; } /* Hide the print button */ \#invoice-printed { display: none; } }Notice the backslash before the @ symbol in \#invoice-printed.
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 17, 2023, 10:02 PM EDT
Or you can enclose your CSS code in a
element in your Razor view, which will not require escaping the@symbol@media print { /* Hide the sidebar */ .sidebar { display: none; } /* Hide the print button */ #invoice-printed { display: none; } }By enclosing the CSS code in a
element, you can use the@symbol as usual without it being interpreted as Razor syntax.Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Mar 18, 2023, 9:27 AM EDT
Ohk Paul I will try and revert, thank you for the response
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Mar 19, 2023, 6:34 PM EDT
Thank you guys for your swift response, but Vishal's code has done the magic