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 Fonts到PDF示例。 相关文档链接 在Github上查看 相关教程 相关操作指南 类文档 获取语言包 下载IronPDF DLL 在此页面上报告问题 准备开始了吗? 版本: 2025.11 刚刚发布 免费npm安装 查看许可证