HTML 到 PDF 的自定義字體

為了確保需要時間加載的資產能夠正確呈現,必須使用waitFor類來指定渲染延遲。 這包括JavaScript、字體和網絡資源等資產。

要使用來自Google Fonts的自訂字體來渲染HTML內容,請配置渲染選項以使用waitFor屬性。 將等待類型設置為RenderDelay,並指定最大的等待時間為500毫秒。

在呈現HTML之後,使用IronPDF的PDF生成庫的功能導出PDF文檔。 您可以檢查生成的PDF,以確保自訂字體已完美加載和顯示。

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

在GitHub上探索Google字體轉PDF示例。

準備好開始了嗎?
版本: 2025.11 剛剛發布