30

I'm really struggeling to print my Twitter Bootstrap views correctly, and was wondering if anyone could point me in the right direction. I've added a custom "print.css" which overrides some of the original bootstrap styling, as well of styling some of my own components.

This is my print dialog: enter image description here

Not sure if you can tell so easily from the image, but the well has lost its background as well as not spanning across the entire page width. Also, the 'columns' or 'spans' seem to be all wonky. Page is built up like this: (Only showing first row to keep it clean)

<div class="container-fluid">
    <div class="row-fluid">
        <div class="span3">
            <div class="row-fluid">
                <div style="height: 150px; width: 300px">
                    <img class="img-rounded" src='data:image/png;base64,@Model.Unit.Image64' />
                </div>
            </div>

            <div class="row-fluid">
                ...
            </div>

        </div>
        <div class="span4">
            ...Pie chart control
        </div>
        <div class="span5">
            ...Bar chart control
        </div>

    </div>
</div>

Here's my print.css:

@media print {
    body {
        margin: 0;
        padding: 0;
        line-height: 1.4em;
        word-spacing: 1px;
        letter-spacing: 0.2px;
        font: 13px Arial, Helvetica,"Lucida Grande", serif;
        color: #000;
    }

    #print-btn #update-btn #nav-left #nav-bar, #selectUnitContainer, .navbar, .sidebar-nav {
        display: none;
    }

    #print-btn, #update-btn, #units {
        display: none;
    }

    #nav-left {
        display: none;
    }

    #report-container {
        visibility: visible;
    }

    .well .span12{
        width: 100%;
        visibility: visible;
    }

    .navbar {
        display: none;
    }

    .sidebar-nav {
        display: none;
    }
}
Nicklas Pouey-Winger
  • 3,023
  • 6
  • 43
  • 75
  • 1
    Were you able to find a decent solution for this? Would be interesting to know, as I myself program a few sites with Bootstrap! =) – MackieeE Sep 26 '13 at 21:02
  • Hi @MackieeE :) I'm sorry, but I havent gotten around to testing this yet, as I'm working on a different issue right now! I'll be sure to update this post once I do, so keep it in your watchlist :) – Nicklas Pouey-Winger Sep 27 '13 at 09:04
  • Also beware of body { height:100% } change to height:initial – user956584 Apr 22 '15 at 09:31

1 Answers1

62

Firstly Bootstrap.css does come with pre-defined print styles such as:

/** 
 *  Use these for toggling content for print.
**/
.visible-print {
    display: block !important;
}

.hidden-print {
    display: none !important;
}

Secondly you can test with page-break css rule to separate the charts onto different pages for cleaner formatting.

/** 
 *  Page-break-inside seems to
 *  be required for Chrome.
**/
div.somechart { 
   page-break-after: always; 
   page-break-inside: avoid;
}

Third test with formatting with the rows to take up the full width on print screen rather than relying on percentages of the span3/col-*-3's and span4/col-*-4's, because it would seem they are behaving correctly in some way when you take into account:

  • Sytem printing page margins
  • Bootstrap's @print defined margins
  • Page gutters
  • span percentages at print screen.

Finally the last small thing that's helpful to note (Yes, despite the custom @print style!):

<link href="/assets/css/bootstrap.min.css" rel="stylesheet" media="screen" />

to:

<link href="/assets/css/bootstrap.min.css" rel="stylesheet" media="all" />
Hakan Fıstık
  • 16,800
  • 14
  • 110
  • 131
MackieeE
  • 11,751
  • 4
  • 39
  • 56
  • 1
    Ok, so I've tried this, and my print looks exactly the same :( No background colors or much of a structure. Just a few borders around it all. By that I mean that I added `visible-print` to a few tags and tested, like so: `
    `
    – Nicklas Pouey-Winger Sep 30 '13 at 12:04
  • Ah! Sorry to hear :( And that's even after changing all the `span4` & `span5`s to `span12`s? I'm supposing the charts are all using pixel widths whilst everything else is in percentages – MackieeE Oct 01 '13 at 08:58
  • 1
    Yeah :( Changed them all. The charts automatically stretches to their parents width, so I don't see how giving them a specific px width would change anything? Then again, I'm no designer, nor do I know css that well, so I may be wrong. Chances are that I am.. ;) I don't have any percentage widths, only the spans. – Nicklas Pouey-Winger Oct 01 '13 at 10:22
  • 1
    @NicklasWinger I had to do a small `.xsl` project a couple of days ago which I kinda ran into the same problem, added a couple of points that were important too :) – MackieeE Oct 27 '13 at 14:30
  • 1
    Thanks alot for the effort @MackieeE :) I'll look into it and let you know :) – Nicklas Pouey-Winger Oct 28 '13 at 06:44
  • @MackieeE is this answer still up-to-date or outdated ? – musafar006 Apr 29 '16 at 17:45
  • Thanks a lot! its working, (@secondly one i have used) – charulatha krishnamoorthy Feb 05 '18 at 07:15