Polices personnalisées dans HTML vers PDF

Pour garantir le rendu correct des ressources qui nécessitent du temps pour se charger, il est essentiel de spécifier un délai de rendu en utilisant la classe waitFor. Cela inclut des ressources telles que JavaScript, les polices de caractères, et les ressources réseau.

Pour rendre le contenu HTML avec une police personnalisée de Google Fonts, configurez les options de rendu pour utiliser la propriété waitFor. Définissez le type de délai comme RenderDelay et spécifiez un temps d'attente maximum de 500 millisecondes.

Après avoir rendu le HTML, exportez le document PDF avec les fonctionnalités de la Bibliothèque de Génération de PDF d'IronPDF. Vous pouvez vérifier le PDF résultant pour voir la police personnalisée parfaitement chargée et affichée.

// 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

Explorez l'exemple Google Fonts to PDF sur GitHub.

Prêt à commencer?
Version : 2025.11 vient de sortir