Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

100vh div has half page height when printing pdf

Tags:

html

css

pdf

I set the div to be 100vh but when printing it's only half of the page. I tried adding media print but without changes. I've also tried to set height to 100% instead. In HTML works but not on pdf.

html,
body {
  height: 100%;
  margin: 0;
  position: relative;
}

.page100vh {
  background-color: red;
  height: 1000vh;
  width: 100vw;
}

@media print {
  .page100vh {
    height: 100vh;
  }
}
<div class="page100vh"></div>

enter image description here

like image 736
Filippo Avatar asked Dec 07 '25 16:12

Filippo


1 Answers

When defining objects in a PDF the units should be similar for X & Y (PDF is technically unitless). Thus best to use px for HTML. however we can use inches or mm for the media ratio such as 210 x 297 for A4.

The division to fully cover that page may be described as 100% of width thus in height that should be 141.421% same width units and for 2 pages it will be 282.842% of width. vh is not tied to vw for PDF /MediaBox viewport scale is only useful for a HTML screen.

@media print {
@page { size: A4;
  margin: 0;
}
}
html,
body {

  margin: 0;
  position: relative;
}

.pageDiv {
  background-color: red;
  height: 141.421vw;
  width: 100vw;
}
<div class="pageDiv">Hello World!</div>

enter image description here enter image description here

like image 187
K J Avatar answered Dec 11 '25 08:12

K J



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!