How to Set Fonts in PDFs

A webfont is a specialized font designed for use on websites. These fonts are hosted on web servers and downloaded by web browsers to ensure consistent and visually appealing text rendering on websites, regardless of the user's local font availability. Additionally, icon fonts, which use symbols and glyphs, are often used in web design to create scalable, customizable icons and maintain a visually consistent user interface through CSS manipulation.

CSS includes web fonts, enabling you to specify font files for download when your website is accessed. IronPDF supports font loading and rendering to PDF from HTML.

Get started with IronPDF

Start using IronPDF in your project today with a free trial.

First Step:
green arrow pointer

Use WebFonts and Icons Example

IronPDF supports WebFonts (such as Google Fonts and the Adobe web font API) and icon fonts, such as those used by Bootstrap and FontAwesome.

Fonts often require a delay in rendering to load properly. When a font is not loaded correctly, it can lead to a blank page without any text. You can use the WaitFor.AllFontsLoaded method to wait until all fonts are loaded by assigning a maximum wait time to it. The default maximum wait time is 500ms.

Here is a small example of how to use a WebFont named Lobster in your project.

:path=/static-assets/pdf/content-code-examples/how-to/webfonts-webicons-render-webfont.cs
using IronPdf;

// HTML contains webfont
var html = @"<link href=""https://fonts.googleapis.com/css?family=Lobster"" rel=""stylesheet"">
<p style=""font-family: 'Lobster', serif; font-size:30px;"" > Hello Google Fonts</p>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Wait for font to load
renderer.RenderingOptions.WaitFor.AllFontsLoaded(2000);

// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Export the PDF
pdf.SaveAs("font-test.pdf");
Imports IronPdf

' HTML contains webfont
Private html = "<link href=""https://fonts.googleapis.com/css?family=Lobster"" rel=""stylesheet"">
<p style=""font-family: 'Lobster', serif; font-size:30px;"" > Hello Google Fonts</p>"

Private renderer As New ChromePdfRenderer()

' Wait for font to load
renderer.RenderingOptions.WaitFor.AllFontsLoaded(2000)

' Render HTML to PDF
Dim pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Export the PDF
pdf.SaveAs("font-test.pdf")
$vbLabelText   $csharpLabel

Explore more WaitFor options, such as those for fonts, JavaScript, HTML elements, and network idle on the 'IronPDF WaitFor Class Documentation.'


Import Font File Example

To use an existing font file, apply the @font-face rule in CSS styling. It also works when using a combination of the @font-face rule and embedding base64-encoded woff files. In the following example, I will be using Pixelify Sans Font.

:path=/static-assets/pdf/content-code-examples/how-to/webfonts-webicons-custom-font.cs
using IronPdf;

// Import custom font
string html = @"<!DOCTYPE html>
<html>
<head>
<style>
@font-face {font-family: 'Pixelify';
src: url('fonts\PixelifySans-VariableFont_wght.ttf');
}
p {
    font-family: 'Pixelify';
    font-size: 70px;
}
</style>
</head>
<body>
<p>Custom font</p>
</body>
</html>";

ChromePdfRenderer renderer = new ChromePdfRenderer();

// Render HTML to PDF
PdfDocument pdf = renderer.RenderHtmlAsPdf(html);

// Export the PDF
pdf.SaveAs("customFont.pdf");
Imports IronPdf

' Import custom font
Private html As String = "<!DOCTYPE html>
<html>
<head>
<style>
@font-face {font-family: 'Pixelify';
src: url('fonts\PixelifySans-VariableFont_wght.ttf');
}
p {
    font-family: 'Pixelify';
    font-size: 70px;
}
</style>
</head>
<body>
<p>Custom font</p>
</body>
</html>"

Private renderer As New ChromePdfRenderer()

' Render HTML to PDF
Private pdf As PdfDocument = renderer.RenderHtmlAsPdf(html)

' Export the PDF
pdf.SaveAs("customFont.pdf")
$vbLabelText   $csharpLabel

Limitations on Azure PDF

The Azure hosting platform does not support servers loading SVG fonts in their lower shared web app tiers. However, Azure's VPS and Web Role are not sandboxed in the same way and do support web font rendering.

Frequently Asked Questions

How do you set fonts in a PDF using a .NET library?

You can set fonts in a PDF using IronPDF by downloading the IronPDF library, using HTML to import or request fonts, delaying the rendering process until fonts are loaded, and applying the @font-face rule for custom fonts in CSS.

What is the role of the WaitFor.AllFontsLoaded method in PDF rendering?

The WaitFor.AllFontsLoaded method in IronPDF ensures that all fonts are fully loaded before rendering the PDF, preventing issues like blank pages caused by unloaded fonts.

Can you use Google Fonts in a .NET application for PDF creation?

Yes, you can use Google Fonts in a .NET application for PDF creation with IronPDF by importing the fonts in your HTML and specifying them in your CSS before rendering the HTML to PDF.

How can icon fonts like FontAwesome be used in PDF documents?

IronPDF supports icon fonts such as FontAwesome, allowing you to incorporate scalable, customizable icons into your PDF documents by including the icon font in your HTML and CSS.

What are the Azure hosting limitations for PDF font rendering?

On the Azure hosting platform, the lower shared web app tiers do not support SVG font loading, but Azure's VPS and Web Role do support full web font rendering without such limitations.

How can I troubleshoot if my custom fonts are not appearing in the PDF?

If custom fonts are not appearing in the PDF, ensure that the font files are correctly linked in your HTML and increase the wait time using the WaitFor.AllFontsLoaded method to allow fonts to load properly before rendering.

What is the process to use the @font-face rule in CSS for PDF conversion?

To use the @font-face rule in CSS for PDF conversion, define the custom font and its source in your CSS, and IronPDF will apply these styles when converting HTML to PDF, allowing custom fonts to be used.

Are there any specific settings needed for using WebFonts in PDF conversion?

For using WebFonts in PDF conversion with IronPDF, ensure that the font URLs are accessible, include the fonts in your HTML via link or import, and specify them in your CSS. Use the WaitFor.AllFontsLoaded method to manage font loading.

Chaknith Bin
Software Engineer
Chaknith works on IronXL and IronBarcode. He has deep expertise in C# and .NET, helping improve the software and support customers. His insights from user interactions contribute to better products, documentation, and overall experience.