Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print exact measurements in Chrome

When measurements are specified in cm or inches for an element, it is printed at exactly that size from Firefox and Internet Explorer. Chrome on the other hand makes the elements bigger.

Is there anyway to make Chrome print things at exactly the specified size, or is it something I'll just have to live with?

E.g.

<!doctype html>
<html>
  <head>
    <style type="text/css">
        div.box {
          border: 1px solid black;
          width: 5cm;
        }
    </style>
  </head>
  <body>
    <div class="box">box</div>
  </body>
</html>

The above code prints an exact 5cm (on my printer) in both Firefox and IE, but prints at about 5.5cm from Chrome.

like image 746
Vincent McNabb Avatar asked Jan 08 '13 20:01

Vincent McNabb


2 Answers

I have found this issue too.

After playing with MANY wasted sheets of paper, I've found that Chrome tries to scale the HTML.

For example, add a full width div to your sample below and it'll resize the box correctly, because you're asking Chrome to make the box 100% of the page and thus forcing a 1:1 scale of the page.

<!doctype html>
<html>
  <head>
    <style type="text/css">
        div.box {
          border: 1px solid black;
          width: 5cm;
        }
        div.forcer {
          width: 100%;
          height: 1px;
          border: 1px dotted green;
        }
    </style>
  </head>
  <body>
    <div class="box">box</div>
    <div class="forcer"></div>
  </body>
</html>

Unfortunately, when I tried this, it didn't fix the height issue, but also I couldn't make the box 0px without it losing correct scaling.

Incidentally, take a look at the following to show how it affects the sizes when printed.

<!doctype html>
<html>
  <head>
    <style type="text/css">
        div.box {
          border: 1px solid black;
          width: 5cm;
        }
        div.forcer {
          width: 200%;
          height: 1px;
          border: 1px dotted green;
        }
    </style>
  </head>
  <body>
    <div class="box">box</div>
    <div class="forcer"></div>
  </body>
</html>

In a nutshell: Chrome's printing capabilities are shocking!

Firefox works far better for printing, but runs much slower.

like image 167
wkdmarty Avatar answered Oct 18 '22 20:10

wkdmarty


For Chrome, just set the print margins to something, and set the body to the width of the paper, minus the margins.

E.g. For an A4 page, the width is 210mm

So for 1 inch margins (roughly 2.5cm) you can do the following

@media print
{
    @page
    {
        margin-left: 25mm;
        margin-right: 25mm;
    }

    body
    {
        width: 160mm;
    }
}

The left, right, and width of the body should add up to 210mm.

For letter you'd use 1 inch margins, and a 6.5 inch width on the body.

like image 32
Vincent McNabb Avatar answered Oct 18 '22 22:10

Vincent McNabb