Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[css-page][css-env] Expose unprintable areas via CSS #11395

Open
mstensho opened this issue Dec 19, 2024 · 3 comments
Open

[css-page][css-env] Expose unprintable areas via CSS #11395

mstensho opened this issue Dec 19, 2024 · 3 comments

Comments

@mstensho
Copy link

Most printers have a small region along each edge of the page sheet which is unprintable, due to the printer's paper handling mechanism. See https://drafts.csswg.org/css-page-3/#page-terms

The size of these unprintable areas are available to applications (such as browsers) in most OSes, but currently it's not web-exposed.

This means that authors have no means of confidently setting page margins to prevent content (inside a page margin box, or even inside the page area) from getting clipped.

Maybe we could use env() variables for this? https://drafts.csswg.org/css-env-1/#safe-area-insets seems to be exactly what we need, in a way, although the spec currently says that "[f]or rectangular displays, these must all be zero". Furthermore, some printers have non-uniform unprintable area widths. For instance, my old HP deskjet had an unprintable area of about 1.5cm at the bottom (which is huge, and more than a typical margin), but more reasonable widths on the other three sides (3 millimeters or less). The problem with this non-uniformity is that the values are relative to the direction the sheet of paper travels through the printer, as far as I understand, and the application has no way of telling whether the printer is going to rotate what it receives from the application. E.g. if the page has landscape orientation (e.g. @page { size: landscape; }, and the printer is fed with the short edge first (aka portrait), it will have to rotate it. But will it be 90 or 270 degrees? Up to the printer. If the printer can print on both sides, it may also be rotated.

So the only safe thing seems to be: use the larger of all those unprintable area widths - i.e. just one env() variable for this?

Let's pretend it's called env(unprintable-area-width) for now:

<!DOCTYPE html>
<style>
  @page {
    margin: calc(env(unprintable-area-width) + 50px);
    margin-bottom: calc(env(unprintable-area-width) + 50px);
    margin-left: calc(env(unprintable-area-width) + 50px);
    margin-right: calc(env(unprintable-area-width) + 50px);

    @top-center {
      content: "";
      border: solid;
      margin-top: env(unprintable-area-width);
    }
    @bottom-center {
      content: "";
      border: solid;
      margin-bottom: env(unprintable-area-width);
    }
    @right-middle {
      content: "";
      border: solid;
      margin-right: env(unprintable-area-width);
    }
    @left-middle {
      content: "";
      border: solid;
      margin-left: env(unprintable-area-width);
    }
    @top-left-corner {
      content: "";
      width: 30px;
      height: 30px;
      border: solid;
      margin-bottom: auto;
      margin-right: auto;
    }
    @top-right-corner {
      content: "";
      width: 30px;
      height: 30px;
      border: solid;
      margin-bottom: auto;
      margin-left: auto;
    }
    @bottom-right-corner {
      content: "";
      width: 30px;
      height: 30px;
      border: solid;
      margin-top: auto;
      margin-left: auto;
    }
    @bottom-left-corner {
      content: "";
      width: 30px;
      height: 30px;
      border: solid;
      margin-top: auto;
      margin-right: auto;
    }
  }
</style>

Content.

If there's no unprintable area (when creating a PDF, for example), it would look like this:

image

If the unprintable area width is 16px, on the other hand, it would result in a print layout like this:

image

On paper it would look like this (if the printer doesn't lie too much about its capabilities):

image

Thoughts? @fantasai @tabatkins @rachelandrew

One concern is printers with large unprintable areas (such as my old HP deskjet with 1.5cm at the bottom), resulting in unreasonably large page margins if they are affected by env(unprintable-area-width).

@rachelandrew
Copy link
Contributor

One variable representing the largest makes sense, I did a quick search but didn't find anything immediately, but I wonder if we can get the most used sizes from anywhere to see if your printer was an outlier or not.

@bfgeek
Copy link

bfgeek commented Dec 19, 2024

An insightful common on why a single size makes sense here:
https://issues.chromium.org/issues/41495735#comment20

@mstensho
Copy link
Author

@rachelandrew It may very well be an outlier. I mean, no browser was able to print default UA footers on that printer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants