Skip to content

[Bug]: default font families are lost after any screenshot captured beyond the viewport (Chromium headless shell) #42962

Closed

Description

Version

1.63.0 (bundled Chromium headless shell 153.0.8010.12)

Steps to reproduce

const { chromium } = require('playwright');

const lines = Array.from({ length: 40 }, (_, i) => `<p>The quick brown fox ${i}</p>`).join('');
const html = family => `<body style="margin:0;font:22px ${family}"><h1>Title</h1>${lines}<p id="last">last</p>`;

async function probe(page) {
  const cdp = await page.context().newCDPSession(page);
  await cdp.send('DOM.enable');
  await cdp.send('CSS.enable');
  const { root } = await cdp.send('DOM.getDocument');
  const { nodeId } = await cdp.send('DOM.querySelector', { nodeId: root.nodeId, selector: 'h1' });
  const { fonts } = await cdp.send('CSS.getPlatformFontsForNode', { nodeId });
  await cdp.detach();
  const y = await page.evaluate(() => Math.round(document.querySelector('#last').getBoundingClientRect().top));
  return `${fonts[0].postScriptName} (last line at ${y}px)`;
}

(async () => {
  const browser = await chromium.launch();
  console.log(process.platform, browser.version());
  for (const family of ['sans-serif', 'serif', 'monospace', 'cursive', 'fantasy']) {
    const page = await browser.newPage({ viewport: { width: 1280, height: 720 } });
    await page.setContent(html(family));
    console.log(`\n${family}`);
    console.log('  initial:', await probe(page));
    await page.screenshot();
    console.log('  after page.screenshot():      ', await probe(page));
    await page.screenshot({ fullPage: true });
    console.log('  after screenshot({fullPage}): ', await probe(page));
    await page.goto('data:text/html,' + encodeURIComponent(html(family)));
    console.log('  after goto:                   ', await probe(page));
    await page.close();
  }
  await browser.close();
})();

Expected behavior

The font resolved for a generic family (sans-serif, serif, monospace, cursive, fantasy) stays the one Playwright sets with Page.setFontFamilies (defaultFontFamilies.ts) for the whole life of the page. Taking a screenshot does not lay the page out again.

Actual behavior

After the first screenshot captured beyond the viewport, the generic families resolve to other fonts and the page is laid out again. A viewport-only screenshot does not do it. A navigation brings Playwright's families back, and the next such screenshot drops them again.

macOS, arm64:

sans-serif
 initial: Helvetica-Bold (last line at 1960px)
 after page.screenshot(): Helvetica-Bold (last line at 1960px)
 after screenshot({fullPage}): Arial-BoldMT (last line at 1999px)
 after goto: Helvetica-Bold (last line at 1960px)
serif: Times-Bold -> TimesNewRomanPS-BoldMT
monospace: Courier-Bold -> Menlo-Bold
cursive: Apple-Chancery (2378px) -> TimesNewRomanPS-BoldMT (2000px)
fantasy: Papyrus (2337px) -> Impact (2042px)

Linux (Ubuntu 24.04 in Docker, arm64): only monospace changes, the family where Playwright's default (Monospace) and Chromium's differ.

monospace: WenQuanYiZenHeiMono (2168px) -> LiberationMono-Bold (1959px)

Additional context

Same root cause as #42607, for another setting.

Page.captureScreenshot with captureBeyondViewport: true pushes WebPreferences twice (page_handler.cc). WebView::ApplyWebPreferences applies every font family map of those preferences (web_view_impl.cc, ApplyFontsFromMap), in the same function that sets max_touch_points. The families set through Page.setFontFamilies at page initialization (_setDefaultFontFamilies, headless only) are overwritten.

The trigger is captureBeyondViewport alone. Over raw CDP, on the same page:

  • Page.captureScreenshot with a 1x1 clip and captureBeyondViewport: true changes the fonts;
  • the same command without captureBeyondViewport does not;
  • resizing the viewport and restoring it, without any screenshot, does not.

It does not reproduce with channel: 'chromium' on macOS, except for monospace (Courier -> Menlo, same metrics).

#42617 re-sends Emulation.setTouchEmulationEnabled after such a capture; the font families are not re-sent.

Likely related: #29968, #11177.

Environment

System:
  OS: macOS
  CPU: arm64
Binaries:
  Node: 22.17.0
npmPackages:
  playwright: 1.63.0

Activity

  1. donmangudata-ops commented on Oct 6, 2026

    @donmangudata-ops

    I can reproduce this on Linux (Playwright Python 1.56.0, Chromium 141 headless shell). After page.screenshot(full_page=True), monospace resolves to Liberation Mono instead of DejaVu Sans Mono. A normal screenshot does not change it.

    A workaround that avoids captureBeyondViewport: resize the viewport to the page height, then take a normal screenshot. In my test the fonts stayed the same, also after restoring the viewport.

    vp = page.viewport_size
    h = page.evaluate("document.documentElement.scrollHeight")
    page.set_viewport_size({"width": vp["width"], "height": h})
    page.screenshot(path="full.png")
    page.set_viewport_size(vp)

    Resizing changes layout for vh units and media queries. I did not test pages that use them.

    Disclosure: I build commercial data tools in this area (details in my GitHub profile).

  2. rgrzywinski commented on Oct 9, 2026

    @rgrzywinski

    Same on macOS arm64, Playwright 1.63.0 / Chromium headless shell 153.0.8010.12: monospace goes Courier-Bold → Menlo-Bold after a beyond-viewport capture, and inline code lines grow 1px. Two additions:

    1. The switch can also go back. In a parallel suite (6 workers), a single toHaveScreenshot loop on a ~30,000px page alternated heights 30049 → 30083 → 30049 across consecutive captures until it timed out on "Failed to take two consecutive stable screenshots". In isolation, the switch is one-way and stays. We can't trigger the reverse on demand.
    2. Workaround that holds across navigations: send Page.setFontFamilies with the family you want, and keep that CDP session attached for the page's life. An override sent on a session that is then detached is lost at the next navigation; one on a retained session survives it. We pin only fixed: 'Menlo', sent once per page before the first navigation, and all subsequent beyond-viewport captures stay byte-identical.
  3. dgozman commented on Oct 16, 2026

    @dgozman
    Collaborator

    Upstream issue: https://issues.chromium.org/u/1/issues/574315013
    The fix should be available with the Chromium 158 roll.

  4. pavelfeldman commented on Oct 31, 2026

    @pavelfeldman
    Member

    I'll close it as fixed, it should be ok in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions