Articles in this section

How to Add a Custom Font in Bold Reports ASP.NET Core Report Viewer

Published:
Updated:

To add a custom font in the Bold Reports ASP.NET Core Report Viewer, store the .ttf file in your application’s wwwroot/fonts directory, declare it with an @font-face rule in your CSS file so it renders in the browser, and load the font stream into PDFOptions.Fonts in your Web API controller so it embeds properly when exporting to PDF.

When to use this

Use this approach when your report uses a corporate or custom font (such as Roboto) that is not installed as a system font on the hosting server. Without these steps, the report may display the custom font in the browser via client-side CSS, but will fall back to a standard font (like Arial or Times New Roman) when exported to PDF.

Prerequisites

  • Bold Reports ASP.NET Core Report Viewer integrated into your application.
  • Processing NuGet package installed (BoldReports.Net.Core).
  • Custom font files in TrueType format (.ttf).
  • Access to the application’s wwwroot folder, CSS file, and the Web API controller implementing OnInitReportOptions.

Steps

1. Store the font files in wwwroot

Create a folder named fonts inside your project’s wwwroot directory (wwwroot/fonts/) and copy your .ttf font files into it (for example, Roboto.ttf and Roboto-Bold.ttf). Placing the folder in wwwroot allows both client-side static file serving and server-side retrieval via _hostingEnvironment.WebRootPath.

2. Declare the custom font in CSS

In your application’s CSS file (such as wwwroot/css/site.css or index.css), define the font using the @font-face rule so the browser renders it in the viewer:

@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto.ttf') format('truetype');
    font-weight: normal;
    font-style: normal;
}
@font-face {
    font-family: 'Roboto';
    src: url('../fonts/Roboto-Bold.ttf') format('truetype');
    font-weight: bold;
    font-style: normal;
}

Note: For each additional weight or style (e.g., Light, Italic), add a corresponding @font-face declaration matching the font file.

3. Reference the CSS file in your layout

Link the stylesheet in the <head> section of your Razor layout page (_Layout.cshtml) or Razor view:

<!-- In ASP.NET Core Razor views/layouts, the tilde (~) resolves to wwwroot -->
<link href="~/css/site.css" rel="stylesheet" />

(Note: If using standard static HTML files instead of Razor views, replace ~/css/site.css with a relative path such as css/site.css.)

4. Load the font stream in the Web API controller

In your Report Viewer Web API controller, configure OnInitReportOptions to attach the font stream to PDFOptions.Fonts. Use cross-platform path resolution with Path.Combine rather than hardcoded backslashes, and preserve any existing PDF configuration:

[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
    // WebRootPath points to the application's wwwroot folder
    string basePath = _hostingEnvironment.WebRootPath;
    reportOption.ReportModel.PDFOptions = new BoldReports.Writer.PDFOptions
    {
        // Load the custom font streams used in the report.
        // Path.Combine builds an OS-correct path, and FileShare.Read allows
        // concurrent exports to read the same file without a lock conflict.
        Fonts = new Dictionary<string, System.IO.Stream>
        {
            { "Roboto", new FileStream(Path.Combine(basePath, "fonts", "Roboto.ttf"), FileMode.Open, FileAccess.Read, FileShare.Read) },
            { "Roboto Bold", new FileStream(Path.Combine(basePath, "fonts", "Roboto-Bold.ttf"), FileMode.Open, FileAccess.Read, FileShare.Read) }
        }
    };
}

Font key naming rules for PDF export

The dictionary key in PDFOptions.Fonts tells the PDF rendering engine which font stream to map to each text element:

  • For regular text, use the exact FontFamily name configured on the report item (for example, "Roboto").
  • For a bold or italic variant, use a separate key that combines the family name with the variant, matching the name used on the report item — for example, "Roboto Bold" for the bold .ttf.
  • The dictionary keys are case-sensitive and must match the case used in your RDL (Report Definition Language) report definitions.

Result

When you run your application, the custom font renders cleanly in the Report Viewer in any web browser. When you click Export > PDF, the PDF writer embeds the provided TrueType font streams directly into the generated PDF file, preserving the original report styling without falling back to a default system font.

Troubleshooting

System.IO.FileNotFoundException: Could not find a part of the path...

This error occurs if the font path is hardcoded using Windows backslashes (\) when running on Linux containers (Docker) or macOS, or if the font file is missing from wwwroot/fonts. Always use Path.Combine(basePath, "fonts", "Roboto.ttf"), and make sure your project’s .csproj file copies the font assets to the output directory if needed.

The custom font renders on screen but not in the exported PDF

The @font-face rule only tells the browser how to render HTML elements. The PDF export runs on the server through the C# report processing engine, which cannot access browser CSS. You must supply the font streams in reportOption.ReportModel.PDFOptions.Fonts inside OnInitReportOptions.

The exported PDF falls back to Arial or Helvetica despite adding the font stream

This happens when the dictionary key does not match the report element’s properties. Verify that:

  1. The font family name matches the RDL element’s FontFamily exactly (casing matters).
  2. Bold and italic elements use a separate key that combines the family name with the variant, such as "Roboto Bold", and that this key matches the one you registered in PDFOptions.Fonts.

Frequently asked questions

Does Bold Reports support font formats other than .ttf for PDF export?

No. Bold Reports PDF export requires TrueType (.ttf) font streams. Web font formats such as .woff or .woff2 are only supported by web browsers for on-screen viewing and cannot be processed by the server-side PDF export engine.

Can I load the font stream from an embedded resource or memory stream instead of a file on disk?

Yes. The PDFOptions.Fonts dictionary accepts any standard System.IO.Stream. You can read the .ttf data from an embedded assembly resource, a byte array (MemoryStream), or a database blob stream instead of reading from a physical path in wwwroot/fonts.

Why must the dictionary key match the exact report font name?

The server-side PDF generator searches the Fonts dictionary using the exact FontFamily string defined on the report item in the RDL file. If the key casing or name does not match exactly, the engine cannot locate the stream and falls back to a standard default font.

Why is FileShare.Read recommended when opening the font stream?

When multiple users export reports concurrently, multiple threads try to read the font file at the same time. FileShare.Read keeps the file from being locked by the first request, avoiding System.IO.IOException errors during simultaneous exports.


Related articles

Was this article useful?
Like
Dislike
Help us improve this page
Please provide feedback or comments
Comments (0)
Access denied
Access denied