Match Google Chrome Faux Bold in wkhtmltopdf
In this article
Matching Google Chrome's "faux bold" effect in wkhtmltopdf requires manually simulating the synthetic thickening of glyphs that browsers apply when a specific bold font weight is missing. Because wkhtmltopdf uses an older WebKit engine, it does not always trigger the same synthetic bolding algorithms as modern Chromium. To achieve this look, you must use a combination of CSS text-shadow for subtle thickening and -webkit-text-stroke for more aggressive weight simulation, as standard font-weight: bold often fails if the font file doesn't explicitly include a bold face.
What You'll Need
- An installed version of wkhtmltopdf (0.12.x or newer recommended).
- Access to the HTML and CSS files being converted to PDF.
- A text editor or IDE for modifying stylesheets.
- A sample font that lacks a native bold weight (to test the faux effect).
Step-by-Step
- Identify the Target Elements: Determine which text elements require the faux bold effect. Create a specific CSS class, such as
.faux-bold, to avoid applying these styles globally, as synthetic thickening can distort standard text. - Disable Standard Bold: Set the
font-weighttonormalfor these elements. This prevents the engine from attempting to find a native bold font, which might result in a fallback font that doesn't match your design. - Apply a Subtle Text Shadow: To mimic the slight expansion of characters seen in Chrome, apply a very small, blurred text shadow. Use the following CSS:
This adds a fraction of a pixel to both sides of the character, simulating a heavier weight without shifting the baseline..faux-bold { font-weight: normal; text-shadow: 0.3px 0 0 currentColor, -0.3px 0 0 currentColor; } - Implement Text Stroke for Heavier Weights: If the text shadow is too subtle, use the WebKit-specific text stroke property. This is the closest programmatic equivalent to the "smearing" effect of faux bolding:
Adjust the pixel value (e.g., 0.2px to 0.8px) until the thickness matches the Chrome rendering..faux-bold { -webkit-text-stroke: 0.5px currentColor; } - Fine-tune Letter Spacing: Synthetic bolding increases the width of characters, which often causes them to touch or overlap. Add a small amount of
letter-spacingto maintain readability:.faux-bold { letter-spacing: 0.01em; } - Test with PDF Rendering: Run your wkhtmltopdf command. It is critical to check the output PDF at 100% zoom, as the faux bold effect can appear differently depending on the PDF viewer's anti-aliasing settings.
Common Mistakes
- Overusing Text Stroke: Setting the
-webkit-text-stroketoo high (e.g., 1px or more) will make the text look "blobby" and distort the internal holes of letters like 'e' and 'a'. - Ignoring Color: Using a hardcoded color in the shadow or stroke instead of
currentColor. UsingcurrentColorensures that if the text color changes, the faux bold effect remains consistent. - Applying to Small Fonts: Faux bolding on fonts smaller than 10px often leads to illegibility in PDF format due to the way pixels are rasterized.
- Confusing Faux Bold with Bold Weight: Attempting to use
font-weight: 700on a font that only has a 400 weight. In many wkhtmltopdf environments, this simply does nothing or triggers a system font fallback.
Tips
- Use SVG for Critical Headers: If a specific header must look exactly like the Chrome render and CSS simulations fail, convert that specific text to an SVG path.
- Combine Methods: For the most natural look, combine a very light
-webkit-text-stroke(0.2px) with a tinytext-shadow. This creates a softer edge that mimics modern browser rendering. - Check DPI Settings: Ensure your
--dpiflag in wkhtmltopdf is set correctly (usually 96), as this affects how fractional pixels in CSS shadows are interpreted.
Troubleshooting
If the text appears blurry or the faux bold isn't showing up, check the following:
- Font Embedding: Ensure the font is properly embedded. If wkhtmltopdf cannot find the font, it will use a system fallback, and your CSS styles will be applied to the wrong glyph shapes.
- PDF Viewer Rendering: Some PDF viewers (like certain browser plugins) smooth text differently. Open the file in Adobe Acrobat or a dedicated PDF reader to verify the actual thickness.
- CSS Specificity: Ensure that no other
font-weight: bolddeclarations are overriding your.faux-boldclass. Use the browser inspector on the source HTML to verify the computed styles. - Version Mismatch: If
-webkit-text-strokeis ignored, ensure you are using a version of wkhtmltopdf based on a WebKit version that supports it. Most 0.12.x versions do.
Frequently Asked Questions
Why doesn't font-weight: bold work in wkhtmltopdf?
It only works if the font file you are using contains a specific bold variant. If the font only has a "Regular" weight, the engine may not automatically synthesize a bold version, leaving the text looking thin.
Does faux bolding affect PDF file size?
No. Since you are using CSS properties like text-shadow and stroke, you are adding instructions to the PDF drawing commands rather than embedding additional large font files.
Will this method work for all fonts?
It works for most, but fonts with very thin stems or complex scripts may distort more than others. Always test the specific typeface you are using.
Is there a difference between text-shadow and text-stroke?
Yes. Text-shadow adds a layer behind or around the text, while text-stroke adds an outline to the edge of the glyph. Stroke is generally more effective for simulating weight.
Can I use this for italic text as well?
Yes, you can combine font-style: italic with these faux bold techniques to create a synthetic bold-italic effect.