What is the best way to ensure proper printing using bootstrap?

Hello, I am having difficulty printing a page while using Bootstrap to style the content. The issue arises when attempting to print the page as Bootstrap adds extra space that cannot be removed or adjusted. How can I resolve this? Thank you.

The dotted lines represent the corners of the page.

Styles:

@media print {
    .page {
        width: 100%; 
        float: none;
        padding: 0 !important;
        margin: 0 !important;
    }

    body {
        line-height: 1.3;
        color: #000;
        width: 100%; 
    }
}

body {
    background: rgb(255,255,255); 
    color: var(--darkgray) !important;
    font-family: 'Roboto Condensed', sans-serif !important;
    font-weight: 100 !important;
    line-height: 1.3;
}

page {
    background: white;
    display: block;
    margin: 0 auto;
    margin-bottom: 0.5cm;
    box-shadow: 0 0 0.5cm rgba(0,0,0,0.5);
    width: 21cm;
    height: 29.7cm; 
    float: none;
    page-break-after: always;
    border: dotted 2px;
}

Page:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
            <link rel="stylesheet" type="text/css" href="styles.css">
            <script src="https://kit.fontawesome.com/25f01b61a0.js" crossorigin="anonymous"></script>
        </head>

        <body>
        <page>

        </page>
        <page>

        </page>
    </body>
</html>

The issue arises when including Bootstrap:

<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">

Result with Bootstrap:

https://i.sstatic.net/fg9kl.png

When removing the Bootstrap link:

https://i.sstatic.net/qgMMi.png

I aim to print the page entirely without any issues.

How can I address this complication?

Thank you.

Answer №1

It's quite a challenging situation. My response does not provide a direct solution.

In the context of my code snippet below, the issue arises with scaling. If my .page div was embedded in an <iframe> and then scaled using CSS transform, it would simulate the appearance of an A4 single-page document (similar to a PDF). This means that as you scale down for mobile viewing, the .page would shrink like a previewed A4 document.

However, there are other considerations to take into account such as multiple-page overflow, print margins, web responsiveness, which can make things more complex.

The most reliable approach is to simply utilize Bootstrap's standard .container class on a div and construct your page content within it. By doing this, you can handle overflow onto multiple pages effortlessly. Just leverage Bootstrap's basic CSS classes for text, tables, and other elements to achieve the desired outcome.

Bootstrap is well-equipped to manage these requirements effectively. Additionally, you can incorporate an additional @media print style to eliminate padding on .container so that your content fills the printed page appropriately.

Below is an experimental piece of code I came up with, but I must emphasize that it may not be the optimal solution depending on your specific needs.

Practicality should be your guiding principle in this scenario.

BODY {
  background: darkgray;
  min-height: 100%;
  padding: 30px;
}

.page {
  height: 0;
  padding-top: calc(100% / 8.27 * 11.65);
  overflow: hidden;
  background: #fff;
  box-shadow: 0px 5px 15px 0px rgba(0, 0, 0, 0.3);
  position: relative;
}

.page-inner {
  position: absolute;
  top: calc(100% / 11.65);
  right: calc(100% / 8.27);
  bottom: calc(100% / 11.65);
  left: calc(100% / 8.27);
}

@media print {
  BODY {
    background: none;
    padding: 0;
  }
  .container {
    width: 100%;
    padding: 0;
  }
  .page {
    background: none;
    box-shadow: none;
  }
  .page-inner {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
  }
}
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" />

<div class="container">
  <div class="page">
    <div class="page-inner">

      <h1 class="display-4">Display test title for print</h1>

      <p class="lead">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec faucibus aliquet turpis, vel luctus sapien ullamcorper sed. Donec sit amet arcu ut urna tempus consectetur cursus porttitor justo. Vivamus vitae mollis nunc. Donec blandit euismod tristique.
        Nam at sem non urna vehicula hendrerit non ac dui. Sed imperdiet lorem at nisi lacinia, in vehicula eros feugiat. Nulla vitae vulputate mauris, malesuada vulputate erat. Donec eleifend augue eros, at scelerisque ex suscipit in.</p>

      <p>Donec bibendum purus nec fringilla hendrerit. Etiam commodo viverra lorem a hendrerit. Etiam venenatis ultrices ante venenatis facilisis. Nunc purus massa, tincidunt a sollicitudin quis, euismod nec ligula. Donec feugiat enim ac sapien sodales,
        id vehicula diam pharetra. Fusce auctor quis risus pellentesque sagittis. Sed fermentum orci odio, et consectetur velit maximus at. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Phasellus congue
        ac erat eu vestibulum.</p>

      <hr/>

      <h2>Sub test title for print</h2>

      <p>Mauris id turpis a nisi tempus ullamcorper. Mauris id pharetra ante, id consectetur erat. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas diam leo, facilisis tempus mauris nec, porttitor ultrices leo. Vestibulum vehicula ullamcorper
        consequat. Maecenas viverra sollicitudin libero, ut faucibus metus tristique sed. Etiam purus magna, luctus non iaculis luctus, molestie nec justo. In molestie, lectus eget feugiat vehicula, nisi libero pulvinar justo, vitae hendrerit mi odio
        a erat. In malesuada eros eu libero varius, sed convallis turpis rutrum. In eu tortor sed elit bibendum volutpat blandit eget ipsum. In varius ex fermentum nisi mattis, id vehicula purus efficitur.</p>

    </div>
  </div>
</div>

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

A quick and easy way to locate the object tag along with the param, and insert the embed tag within an HTML document is

Struggling with understanding the simple HTML DOM of PHP? You're not alone. As a newcomer to the programming industry, following instructions can be challenging. I've been searching for how to locate the object tag and embed, but haven't fou ...

The fixed navigation bar is causing the content to shift downward

I am currently working on designing a two-layered navbar structure. The top layer contains essential company information such as phone number, email address, social media links, and more. The second layer serves as the main navbar, designed to be sticky a ...

Using the 'onended' audio event emitter in Angular 2 along with a local member of the Component

I'm looking for assistance on how to utilize audio.onended() in order to play the next song in a playlist. I can successfully add songs to the playlist and play them using the above method with an audioObject. However, when audio.onended triggers, I ...

The width of a CSS border determines its height, not its width

When I try to use border-width: 100px; to set the horizontal width to 100px, it ends up setting the height to 100px instead. Any help would be greatly appreciated. .border-top-green { border-top: 1px solid #4C9962; border-width: 100px; padding-t ...

Extract hidden form variables using a function in PHP

Here is the JavaScript function that I have written: function GetCellValues() { var rows = document.getElementsByTagName('tr'); var str = ''; for (var c = 1 ; c < rows.length ; c++) { str += '\n&apo ...

Unable to resize the div to fit the content

My container div is not adjusting its height based on the content inside. It expands according to the header and navigation divs, but not the sidebar which is nested inside another div. Here is the HTML structure: <div id="container"> <div ...

Bottom space gap issue in PhoneGap for Windows Phone

I am using phonegap for development and I am facing an issue with the styling. My main concern is to position a footer at the bottom of the page. This is how my index.html looks like <!DOCTYPE html> <html> <head> <meta http- ...

Create dynamic div animations triggered by changes in size of another div

Struggling to get this animation to function correctly. I have a vertical scrolling page with various sized div elements that change based on the content. The problem is that when these size changes occur, the other elements are moving abruptly. I want to ...

Using bootstrap-vue sticky columns without specifying a column variant is easier than you think

Let's cut to the chase with an example I created using bootstrap-vue's documentation to help clarify the issue: <template> <div> <div class="mb-2"> <b-form-checkbox v-model="stickyHeader" inline>Sticky header& ...

Discovering mistakes in PHP MySQL

Is there an error in this code? Here's the code I'm working with. Thank you in advance for your help! else if(isset($_POST['UPDATECSSGOLD'])) { $stmt = $mysqli->prepare("UPDATE css set STYLE_STATUS = 'allnull'"); $stmt-& ...

Issues with stationary navigation bars

Having trouble with the fixed navigation bar, as it appears slightly lower on section links. I want the navigation bar to be at the top so that the title text in sections is clearly visible. I attempted to add margin-top to the section, but the gaps were t ...

Error Message: Angular Typescript: x function is not defined

UPDATE: Huge shoutout to everyone who caught my mistake with the code location... Haha I've been wrestling with this issue all day long - the button should trigger the menu to open and switch to "close menu" after displaying a list of 3 items. The co ...

Setting focus on an input box using programmatic methods triggered by the ItemInvoked event in a ListView in a Windows 8 Metro HTML5 environment

Note: If you have not worked on Windows 8 before, please refrain from answering this question. You are advised not to vote or read it unless you are familiar with Windows 8 METRO HTML5/js applications that run in the Windows Runtime environment. Query: I ...

Tips for opening a new browser tab

I'm having trouble getting my code to open in a new browser tab. Can someone help me figure out what's wrong? function getWaterMeterList() { // alert("ON"); var BillingPeriod = $('#BillingPeriod').val(); $.ajax({ ur ...

Tips for aligning an input field alongside a button using CSS and HTML

.wrapper { display: flex; justify-content: center; align-items: center; width: 100vw; height: 100vh; background-color: blueviolet; } .welcome_text { width: 500px; } .welcome_form input, .welcome_form button { display: block; width: 100% ...

Incorporating a custom background image for Google Maps

Can a live Google map achieve the same overlay effect as a static image? Discover more here Many thanks, Greg Update: Check out this Fiddle that was created based on another Fiddle by @SinistraD ...

Is there a way to conceal :before content in the parent element by hovering over the child element?

Here is the HTML code I currently have: <li *ngFor="let menu of menus" class="{{ menu.attributes.class }} menu-items" [attr.data-title]="menu.label"> <a [routerLink]="[menu.url||'/']" [queryParams]="menu.refParameter3 ? menu.refPara ...

Tips for shutting down the camera within a modal by utilizing Html5 QrCode Scanner in ASP.NET MVC

Hey there! I'm currently working with the Html5 QrCode library to implement a scanner, and I've integrated it into a modal. However, I'm facing an issue where the camera continues running even after I close the modal. I'm wondering if t ...

The concept of Nested DIVs and their impact on Element Height

Trying to divide this page into blocks with checkbox options in a 4x3 layout. The issue is that setting the "columns" to 100% height makes it 100% of the parent's parent, not the parent div. Any suggestions? <div id="left" style="width:50%;height: ...

:hover functionality can only be activated within the Inspector

The element top_fixed_right:hover on my homepage is functioning perfectly. However, on a different page, the same div only responds to the :hover state when forced using Chrome's developer tools. Normal mouse hovering does not trigger it. I tried op ...