Print media not affected by Bootstrap styling

Below is the code I am using to display a table within my application:


        var elementToPrint = this.mainTable.get(0);

        newWin = window.open("");
        newWin.document.write('<html><head><title>' + elementToPrint.caption + '</title>' +
                            '</head><body class=\'visible-print\'>');
        newWin.document.write(elementToPrint.outerHTML);
        newWin.print();
        newWin.close();
    

The variable `this.mainTable` is a jQuery object.

In my common page (_Layout.cshtml) I have:


        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
            <meta charset="UTF-8" />

            <title>MyApp</title>

            <meta name="viewport" content="width=device-width, initial-scale=1.0">

            <link href="@Url.Content("~/Content/bootstrap.css")" rel="stylesheet" media="all">
            <link href="@Url.Content("~/Content/bootstrap-theme.min.css")" rel="stylesheet" media="all">
        </head>
    

The printing function works fine, but it loses all styles, displaying unformatted text data (side by side). I want to maintain the original bootstrap layout with colors, borders, and stripes. What changes should be made?

Thank you.

Answer №1

Whenever a new window is created, it actually generates a fresh html page that DOES NOT inherit from your main page template. Therefore, the new html page won't include your bootstrap css references.

To address this issue, you should insert these bootstrap css references while utilizing document.write within the code snippet shown below,

newWin.document.write('<html><head><title>' + elementToPrint.caption + '</title>');
newWin.document.write('<link href="Your-Path/bootstrap.min.css" rel="stylesheet" />');
newWin.document.write('<link href="Your-Path/bootstrap-theme.min.css" rel="stylesheet" />');
newWin.document.write('</head><body class=\'visible-print\'>');

UPDATE The standard twitter-bootstrap includes css rules for the Print media type as shown below, which overrides any screen css rules,

@media print {
  * {
    color: #000 !important;
    text-shadow: none !important;
    background: transparent !important;
    box-shadow: none !important;
  }

You can either obtain a custom bootstrap version without "Print media styles" (Recommended) OR manually eliminate these @media print rules.

Take a look at this Plunker demonstration showcasing Printing with Bootstrap CSS styles

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unleash the power of jQuery to leverage two different input methods

Here is the code I'm working with: $('input[type="text"]').each(function(indx){ Right now, this code targets input elements with type "text". But how can I modify it to target both inputs with type "text" and "password"? Any suggestions o ...

Updating the attribute of an ASP object using JQuery

Is there a way to utilize jQuery in order to make Chart1 visible? The code provided below is what I have attempted so far, however, it has not been successful. Any assistance would be greatly appreciated. Thank you. <script type="text/javascript> ...

What is the best way to smoothly transition in an image that is dynamically set using Angular?

I am using Angular to set a background image for my page, but the current loading behavior is not visually appealing as the image loads from top to bottom. I would like to implement a fade-in effect or load the image from a blurry view to enhance the user ...

Uniformly sized text containers and buttons

Seeking assistance to align a text field and a button in a way that they appear as a combined element. The desired look is shown below (screenshot taken from Chrome Linux): However, when viewed in Firefox Linux, the button is slightly taller by two pixels ...

Ways to turn off Bootstrap links hover background color?

I recently added a Bootstrap menu to my website that I'm currently working on. I am curious about how to disable the hover background color for Bootstrap links. Here's the situation: Below is an example of the menu I implemented: Currently, whe ...

Combining two asynchronous requests in AJAX

In the process of customizing the form-edit-account.php template, I have added ajax requests to enhance the functionality of the account settings form. The form allows users to modify their name, surname, age, and other details. While the ajax implementati ...

Developing a Customized Modal with Backbone.js

As a newcomer to Backbone, I have been trying to create a Modal in my application by setting up a base Modal class and then extending it for different templates. However, despite conducting some research, I haven't been able to find a solution that fi ...

Incorporating a unique font into your SAPUI5 application

While experimenting with an expense app design, I decided to incorporate a receipt-like font for a unique look and feel. After discovering the FakeReceipt font, I placed my woff and woff2 files in the same directory as my style.css file, and it worked like ...

What steps should be followed to effectively incorporate Google Fonts into a Material UI custom theme for typography in a React/TypeScript project

Hey there, I'm currently working on incorporating Google fonts into a custom Material UI theme as the global font. However, I'm facing an issue where the font-weight setting is not being applied. It seems to only display the normal weight of 400. ...

Retrieve progress with easing using jQuery's animate() function

At the moment, I'm utilizing this code to create an animation for a bar-graph-inspired element: $('.anim').animate({ right: `${100-(e/max*100)}%`, backgroundColor: colors[0] },{ duration: 1500, easing: 'easeInQuart&apos ...

jQuery's z-index feature is malfunctioning

When I hover over my menu, a box fades in. However, there is a small icon behind this box that I want to move to the front so it can be visible during hover. To see an example of my menu, click here: Navigation I attempted to address this by using jQuer ...

How to create a full-width and full-height background image in a div using CSS

When using a bootstrap template, I encountered an issue with adding an image as a background. Here is the CSS code: background:url(../images/banner.png) no-repeat; background-size: 100%; The problem with these CSS rules is that the image gets clipped and ...

Problem with the purity of :global() CSS-module selectors in NextJS

Currently in the process of migrating an app from CRA to NextJS, I've encountered an issue with the .module.scss files of certain components and pages: Syntax error: Selector ":global(.label-primary)" is not pure (pure selectors must contain ...

Significant contrast in how text is displayed between Chrome and Firefox

Experiencing significant discrepancies in text rendering between Chrome and Firefox. Chrome appears to apply anti-aliasing rules that reduce the text size considerably. Attempts have been made to adjust -webkit-font-smoothing, letter-spacing and word-spac ...

What is the most effective method for verifying a selected item in Jquery UI selectable?

I'm having an issue with my image display div where users can delete selected images. The code functions correctly, but there seems to be unnecessary repetition in certain parts of it. I attempted using `$(".ui-selected").each()` to stop the ...

Setting the size of a box using CSS in HTML pages

I am facing issues with resizing boxes. Below is the code snippet: <!DOCTYPE html> <html lang="en"> <head> <style> .wrapper { text-align: center; } #bubble { display: inline; -moz ...

Bootstrap form validation solution

Utilizing bootstrap validation to validate a jsp page. The folder structure is as follows: WebContent ├── bootstrap-form-validation ├── js └── pages All three folders are under the web content. If I create another folder called teacher ...

The issue with hiding and showing elements using JavaScript during drag and drop functionality

In my code, I have two boxes with IDs box1 and box2, These boxes can be dragged and dropped into the boxleft element, Upon dropping them, the background image is removed and only the name appears in the box, My issue is that when loading values into box ...

Delete a class that is not identified by the DOM

Currently, I'm developing an interactive map that triggers an overlay image to highlight the selected area. I am trying to dynamically add and remove classes from a div based on the highlighted area. Initially, I attempted using the starts with selec ...

Having trouble with the width and alignment of bootstrap columns while setting them up in Angular ng-repeat. How

One issue I'm facing is that the columns created dynamically by my directive do not match the width of statically created bootstrap columns. For example: <my-directive></my-directive> <div class="row"> <div style="background:gr ...