Print and PDF handoff
I wanted to know what Chrome puts into a printed page or PDF when live HTML sits inside a drawable canvas. Press Print (or Cmd/Ctrl+P) to print both cards side by side and test text selection, 800% zoom on the SVG barcode, and the clickable link. Toggle Simulate print handoff to watch moveBefore() pull the right card out into plain DOM and return it without dropping typed text or checkbox state.
Handed off to plain DOM via moveBefore()
Show the code the two cards, the canvas paint effect, the beforeprint handoff, the print CSS
- The two cards. Two copies of one dispatch card inside drawable canvases, with a sibling
#print-hostready for the right card. - Drawing on screen. Sizes the backing store with
device-pixel-content-box, draws crop marks, tilts the card with 5-argumentdrawElementImage(), then adds paper grain and an angled stamp. - The
beforeprintandafterprinthandoff. Moves the right card out withmoveBefore()(with anappendChild()fallback), records state survival, and moves it back withrequestPaint(). - Print styles. Hides site chrome and places both cards side by side in the printed page or PDF.
How it works
Both cards start inside a <canvas content="drawable" layoutsubtree>. On screen, the 2D paint handler renders each card onto a warm drafting mat with corner crop marks, a slight tilt, paper grain and a red "CANVAS 2D" rubber stamp. That makes it obvious on screen when a card is painted by a canvas versus sitting in plain DOM.
When you open Print or Save as PDF, two things happen at once. The left card stays inside #raw-canvas so the PDF captures whatever Chrome's print pipeline emits for a drawable canvas. On the right card, the beforeprint listener (and window.matchMedia('print') as a backup) calls printHost.moveBefore(card, null) to move the live <article> out of #handoff-canvas into #print-host.
Because #print-host is kept rendered rather than display: none, moving the card with moveBefore() preserves the typed text in "Recipient name", the text selection range, the checkbox state, and keyboard focus. When the print dialog closes, afterprint un-hides #handoff-canvas first, calls handoffCanvas.moveBefore(card, null), and calls handoffCanvas.requestPaint() so the styled canvas snaps back.
In a browser without moveBefore(), the handoff falls back to appendChild(). In a browser without HTML-in-canvas, the script moves both cards out of their canvases into plain HTML so neither shows up as an empty box.
Live checks
These run on their own fixtures in the corner of the window. They never touch the cards above and never call window.print().
Findings
Element.prototype.moveBefore()lets a page move a live interactive subtree out of a<canvas layoutsubtree>into a sibling print container and back again without losing typed text, selection range, or checkbox state.- The destination container must not be
display: nonewhenmoveBefore()runs. Keeping#print-hostrendered on screen and un-hiding#handoff-canvasbefore moving the card back avoids dropping focus on the return leg. - After returning the element to the canvas, calling
canvas.requestPaint()repaints the drawable child on the next paint event. - Not verified in a headless browser: what Chrome's native print dialog and Save as PDF output emit for the left card (whether
drawElementImage()text and<a href>links are flattened into a raster bitmap, preserved as vector text, or left blank). Press Print / Save as PDF on a desktop or Android browser to check the saved PDF directly.