1

I need to be able to print a part of a website that have master page in PDF format. This can be done by using Chrome print capability. However when I used below javascript, the CSS style is not included.

<link type="text/css" href="~/StyleSheet.css" rel="stylesheet" />
<link type="text/css" href="~/StyleSheet.css" rel="stylesheet" media="print" />
<script type="text/javascript" src="../Scripts/jquery-3.1.1.min.js"></script>
<script type="text/javascript">
function printDiv(obj)
{
    var printContents = document.getElementById(obj).innerHTML;
    var originalContents = document.body.innerHTML;

    printContents = document.getElementById('banner').innerHTML + printContents;
    document.body.innerHTML = printContents;

    window.print();

    document.body.innerHTML = originalContents;
}

Below is my aspx code for my website;

<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
    <div id="banner">
        <asp:Image ID="imgBanner" runat="server" Height="75px" ImageUrl="~/Misc/Banner/Dashboard.jpg" />
    </div>
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="ContentPlaceHolder3" Runat="Server">
    <div class="topbar-divider-right">
        Print Dashboard as:
        <asp:Button ID="btnPDF" CssClass="btn btn-default" runat="server" Text="PDF" OnClientClick="javascript:return printDiv('divPrintDashboard');" />
    </div>
</asp:Content>
<asp:Content ID="Content5" ContentPlaceHolderID="ContentPlaceHolder4" Runat="Server">
    <div id="divPrintDashboard">
        ...Content...
    </div>
</asp:Content>

CSS style for this webpage. All the style is within 'divPrintDashboard'

/*Dashboard*/
@media sceen, print {
    .head-dashboard {
        background-color: black;
        color: white;
        /*font-weight: bold;*/
        border:1px solid gray;
        padding-left: 5px
    }
    .tbl-row-RM-dashboard td:nth-child(3),
    .tbl-row-RM-dashboard td:nth-child(4),
    .tbl-row-RM-dashboard td:nth-child(5),
    .tbl-row-RM-dashboard td:nth-child(6),
    .tbl-row-RM-dashboard td:nth-child(7) {
        width: 50px
    }
    .tbl-comp-count th {
        min-width: 75px
    }
    .tbl-comp-count tr:last-child {
        border-top: 2px solid black;
        border-bottom: double
    }
}

.head-dashboard {
    background-color: black;
    color: white;
    border:1px solid gray;
    padding-left: 5px
 }
.tbl-row-RM-dashboard td:nth-child(3),
.tbl-row-RM-dashboard td:nth-child(4),
.tbl-row-RM-dashboard td:nth-child(5),
.tbl-row-RM-dashboard td:nth-child(6),
.tbl-row-RM-dashboard td:nth-child(7) {
    width: 50px
}
.tbl-comp-count th {
    min-width: 75px
}
.tbl-comp-count tr:last-child {
    border-top: 2px solid black;
    border-bottom: double
}

I use the exact javascript from my colleague but mine not working but his own does work.

Original website

enter image description here

The result from window.print()

enter image description here

4
  • Maybe with @media screen, print Commented Apr 20, 2017 at 1:00
  • I didn't see that in my colleague code... but I will try Commented Apr 20, 2017 at 1:26
  • try to add @media screen, print but it doesn't work Commented Apr 20, 2017 at 6:04
  • Can you add the CSS code in your StyleSheet.css? There may be some specific rules that you miss Commented Apr 20, 2017 at 6:34

1 Answer 1

4

The background color when printing can be controlled by the users browser and it's possible "background color printing" has been turned off.

Also you can force overrides with

body { print-color-adjust: exact; }

Should work in Chrome but have not tested in other browsers.

Combine this with Hackermans answer then you don't need two css files

@media print { \\do stuff }
Sign up to request clarification or add additional context in comments.

2 Comments

i try to add into current CSS file but it doesn't work
print-color-adjust: exact; also, for compatibility

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.