Custom Fonts in HTML to PDF

To ensure the proper rendering of assets that require time to load, it is essential to specify a render delay using the waitFor class. This includes assets such as JavaScript, fonts, and network resources.

To render HTML content with a custom font from Google Fonts, configure the rendering options to use the waitFor property. Set the wait for type as RenderDelay and specify a maximum wait time of 500 milliseconds.

After rendering the HTML, export the PDF document with the capabilities of IronPDF's PDF Generation Library. You can check the resulting PDF to see the custom font perfectly loaded and displayed.

// C# Example Code for Rendering HTML with custom font using IronPDF

using IronPdf;

class Program
{
    static void Main()
    {
        // Step 1: Initialize the Renderer
        var Renderer = new HtmlToPdf();

        // Configure rendering options to wait for specific assets to load
        Renderer.PrintOptions.WaitFor = WebBrowserEvent.RenderDelay;
        Renderer.PrintOptions.RenderDelay = 500; // Maximum wait time set to 500 milliseconds

        // Step 2: Define HTML with a custom Google Font link
        string htmlContent = @"
        <!DOCTYPE html>
        <html lang='en'>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap' rel='stylesheet'>
            <style>
                body {
                    font-family: 'Roboto', sans-serif;
                }
                h1 {
                    font-weight: 700;
                }
            </style>
            <title>Sample PDF</title>
        </head>
        <body>
            <h1>Hello, World!</h1>
            <p>This is a paragraph with a custom Google font.</p>
        </body>
        </html>";

        // Step 3: Render the HTML to a PDF document
        var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);

        // Step 4: Export the PDF document to a file
        pdfDocument.SaveAs("SampleWithFonts.pdf");

        // Output completion message
        Console.WriteLine("PDF document created successfully with custom Google font!");
    }
}
// C# Example Code for Rendering HTML with custom font using IronPDF

using IronPdf;

class Program
{
    static void Main()
    {
        // Step 1: Initialize the Renderer
        var Renderer = new HtmlToPdf();

        // Configure rendering options to wait for specific assets to load
        Renderer.PrintOptions.WaitFor = WebBrowserEvent.RenderDelay;
        Renderer.PrintOptions.RenderDelay = 500; // Maximum wait time set to 500 milliseconds

        // Step 2: Define HTML with a custom Google Font link
        string htmlContent = @"
        <!DOCTYPE html>
        <html lang='en'>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap' rel='stylesheet'>
            <style>
                body {
                    font-family: 'Roboto', sans-serif;
                }
                h1 {
                    font-weight: 700;
                }
            </style>
            <title>Sample PDF</title>
        </head>
        <body>
            <h1>Hello, World!</h1>
            <p>This is a paragraph with a custom Google font.</p>
        </body>
        </html>";

        // Step 3: Render the HTML to a PDF document
        var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);

        // Step 4: Export the PDF document to a file
        pdfDocument.SaveAs("SampleWithFonts.pdf");

        // Output completion message
        Console.WriteLine("PDF document created successfully with custom Google font!");
    }
}
' C# Example Code for Rendering HTML with custom font using IronPDF

Imports IronPdf

Friend Class Program
	Shared Sub Main()
		' Step 1: Initialize the Renderer
		Dim Renderer = New HtmlToPdf()

		' Configure rendering options to wait for specific assets to load
		Renderer.PrintOptions.WaitFor = WebBrowserEvent.RenderDelay
		Renderer.PrintOptions.RenderDelay = 500 ' Maximum wait time set to 500 milliseconds

		' Step 2: Define HTML with a custom Google Font link
		Dim htmlContent As String = "
        <!DOCTYPE html>
        <html lang='en'>
        <head>
            <meta charset='UTF-8'>
            <meta name='viewport' content='width=device-width, initial-scale=1.0'>
            <link href='https://fonts.googleapis.com/css2?family=Roboto:wght@400;700&display=swap' rel='stylesheet'>
            <style>
                body {
                    font-family: 'Roboto', sans-serif;
                }
                h1 {
                    font-weight: 700;
                }
            </style>
            <title>Sample PDF</title>
        </head>
        <body>
            <h1>Hello, World!</h1>
            <p>This is a paragraph with a custom Google font.</p>
        </body>
        </html>"

		' Step 3: Render the HTML to a PDF document
		Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)

		' Step 4: Export the PDF document to a file
		pdfDocument.SaveAs("SampleWithFonts.pdf")

		' Output completion message
		Console.WriteLine("PDF document created successfully with custom Google font!")
	End Sub
End Class
$vbLabelText   $csharpLabel