sodcalculator

10 Tech How-To

Fix Playwright Screenshots Hindi Fonts Ubuntu

Guide 10-49732 · Updated · Step by step

If your Playwright screenshots on Ubuntu show boxes, squares, or missing glyphs instead of Hindi text, the system likely lacks the fonts needed to render Devanagari script. This guide walks through installing the required fonts, configuring fontconfig, and verifying that Playwright’s bundled Chromium can render Hindi correctly.

What You’ll Need

  • Ubuntu (20.04 or newer) with sudo access
  • A Playwright project already set up (Node.js or Python)
  • Internet connection to install font packages

Step-by-Step

  1. Check current font status. Run fc-list | grep -i devanagari in a terminal. If nothing appears, no Devanagari fonts are installed.
  2. Install the fonts-noto package. This includes Noto Sans Devanagari and Noto Serif Devanagari, which cover the full Hindi character set. Run: sudo apt update && sudo apt install fonts-noto
  3. Install additional Devanagari fonts (optional). For broader coverage, also install fonts-lohit-deva and fonts-gargi: sudo apt install fonts-lohit-deva fonts-gargi
  4. Rebuild the font cache. After installation, run sudo fc-cache -fv to refresh the font information database.
  5. Verify fonts are now visible. Run fc-list | grep -i devanagari again. You should see multiple font files listed.
  6. Set a fallback font in fontconfig. Create a configuration file to ensure Chromium uses a Devanagari font when needed. Create /etc/fonts/local.conf (or edit it) and add:
    <?xml version="1.0"?>
    <!DOCTYPE fontconfig SYSTEM "fonts.dtd">
    <fontconfig>
      <match target="pattern">
        <test name="lang" compare="contains"><string>hi</string></test>
        <edit name="family" mode="prepend" binding="strong">
          <string>Noto Sans Devanagari</string>
        </edit>
      </match>
    </fontconfig>
  7. Rebuild font cache again. Run sudo fc-cache -fv to apply the config change.
  8. Restart any running Playwright browsers. Close and relaunch your Playwright script or browser instance so it picks up the new font configuration.
  9. Test with a simple screenshot. Write a minimal Playwright script that navigates to a page with Hindi text (e.g., a Wikipedia article in Hindi) and takes a screenshot. Check the output image for proper rendering.
  10. If still broken, install Playwright’s system dependencies. Run npx playwright install-deps (or python -m playwright install-deps for Python). This installs libraries that Chromium needs for font rendering.

Common Mistakes

  • Forgetting to rebuild the font cache after installing fonts.
  • Only installing a single font that doesn’t cover all conjuncts (e.g., installing only a Latin font).
  • Not restarting the browser process – Playwright caches fontconfig at launch.
  • Using a headless environment without the necessary system libraries (missing libnss3, libatk, etc.).
  • Assuming the problem is in the code when it’s purely a system font issue.

Tips

  • Always use a font that supports the full Devanagari Unicode block – Noto Sans Devanagari is the safest choice.
  • If you’re deploying to a Docker container, include the font installation steps in your Dockerfile.
  • For consistent results across environments, consider bundling the font files with your project and loading them via page.addStyleTag or a custom font-face.
  • Test with a known complex Hindi string like “हिन्दी विकिपीडिया” to ensure conjuncts and matras render correctly.
  • Use fc-match 'Devanagari' to see which font fontconfig would select for Devanagari text.

Troubleshooting

  • Still seeing boxes after installing fonts? Check that the font files are actually present with fc-list | grep -i noto. If not, try reinstalling: sudo apt install --reinstall fonts-noto.
  • Playwright uses its own Chromium – do system fonts apply? Yes, Chromium reads system fontconfig. If it still fails, try launching Playwright with --no-sandbox or in headed mode to see if there’s a different error.
  • Headless mode shows different results than headed. Sometimes headless has fewer default fonts. Ensure you’ve installed the full fonts-noto package, not just a subset.
  • Error: ‘fontconfig’ not found? Install fontconfig: sudo apt install fontconfig.
  • If using Docker, the container may not have the same fonts. Rebuild the image after installing fonts. Also ensure you copy the fontconfig config into the container.
  • If the issue persists, check the page’s CSS. Some websites explicitly set a font-family that doesn’t exist on your system. Override it in your script by injecting a style: page.addStyleTag({ content: '* { font-family: "Noto Sans Devanagari", sans-serif !important; }' })

Frequently Asked Questions

Why do I see boxes instead of Hindi characters?

Boxes (or tofu) mean the system has no font that contains the Devanagari glyphs. Installing a font like Noto Sans Devanagari fixes this.

Do I need to install fonts for each user?

No, system-wide installation via apt makes fonts available to all users and processes, including Playwright’s Chromium.

Will this affect other languages?

No, it only adds Devanagari support. Other scripts remain unchanged.

Can I use a custom Hindi font instead of Noto?

Yes, you can install any TTF/OTF Devanagari font and reference it in fontconfig or via CSS in your page.

Does this work for other Indic scripts like Tamil or Bengali?

Yes, the same approach works – install the appropriate Noto fonts (e.g., fonts-noto-tamil, fonts-noto-bengali) and ensure fontconfig recognizes them.

Why does my screenshot look fine in a browser but not in Playwright?

Playwright’s Chromium may run in a sandbox or headless environment that lacks the same font configuration. Installing fonts system-wide and restarting the browser usually resolves this.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →