Print Guard

Before printing, finds what the paper will lose (dropped backgrounds, collapsed sections, cut-off tables) and fixes it for print only.

StopsPrintout doesn't match the screen

Installation

pnpm dlx shadcn@latest add https://bollard-ui.vercel.app/r/print-guard.json

The files land in your project. They're yours to edit.

Usage

print-guard-usage.tsx
import { PrintGuard } from "@/components/ui/print-guard"

<PrintGuard target={doc} checkOnMount />

What it catches

  • Light text on dark backgrounds the browser won't print
  • Collapsed sections that would print closed
  • Tables wider than the printable width
  • Scroll boxes that print only the visible part
  • Links printed without their address

Props

PrintGuard props
PropTypeDefault
target

What gets printed. Defaults to the whole page.

React.RefObject<HTMLElement | null>—
pageWidth

Printable width in CSS px. Default: A4 with 1 cm margins (718). Letter is about 739.

numberA4_CONTENT_WIDTH
autoFix

Ctrl+P / Cmd+P can't be stopped, so apply every fix to those printouts too, then offer a review.

booleantrue
checkOnMount

Check as soon as it mounts and show what would be lost, without waiting for a click.

booleanfalse
describeIssue

Wording per issue; return `problem` and `fix`.

(issue: PrintIssue) => { problem: string; fix: string }describePrintIssue
printLabel

Print button content.

React.ReactNode"Print"
title

Panel heading.

string"This won't print the way it looks"
showLabel

Button text to show an affected element.

string"Show"
printFixedLabel

Button text to print with fixes.

string"Print with fixes"
printAsIsLabel

Button text to print without fixes.

string"Print as is"
cancelLabel

Cancel button text.

string"Cancel"
autoFixedText

`{count}` is filled in.

string"Fixed {count} for that printout."
reviewLabel

Button text to review an auto-fixed printout.

string"Review"

* required. Also accepts the native props of <div> (className, aria-*, data-*…), passed through to it.

Accessibility

  • Toggle uses aria-expanded; the panel is a labelled region, inert when closed
  • Status uses role=status; focus moves into the panel and back to Print
  • Highlight motion is skipped under reduced motion

Only what the source does. Test with your own screen reader and content before shipping.

Limitations

  • Default page width is A4 with 1 cm margins; set pageWidth for Letter or other sizes
  • Can't change the browser's own print settings (backgrounds, scale)
  • Checks the live DOM at print time; content loaded later is missed

What it stops

Printout doesn't match the screen

Also known as

print · ctrl+p · print css · invoice print · pdf · receipt · print preview · @media print

Dependencies

Pairs well with