如何在 PDF 中设置字体
网页字体是专为网站使用而设计的字体。 这些字体托管在网络服务器上,并由网页浏览器下载,以确保网站上的文本呈现一致且视觉上吸引人,不受用户本地字体可用性的影响。 此外,图标字体使用符号和字形,常用于网页设计中,以创建可缩放、可自定义的图标,并通过CSS操作保持视觉上的一致性用户界面。
CSS包括网络字体,允许您指定在访问您的网站时下载的字体文件。 IronPDF支持从HTML加载字体并渲染到PDF。
如何在 PDF 中设置字体
- 下载用于在PDF中设置字体的C#库IronPDF
- 利用 HTML 从外部来源导入或请求字体
- 延迟渲染过程,确保正确加载字体
- 像往常一样将 HTML 转换为 PDF
- 在 CSS 样式中使用 @font-face 规则导入字体文件
开始使用IronPDF
立即在您的项目中开始使用IronPDF,并享受免费试用。
使用 WebFonts 和图标示例
IronPDF 支持WebFonts(如 Google Fonts 和 Adobe 网站字体 API)以及图标字体,例如 Bootstrap 和FontAwesome使用的字体。
字体在渲染时常常需要一定的延迟以正确加载。 当字体未正确加载时,可能会导致页面上没有任何文本的空白页。 您可以使用WaitFor.AllFontsLoaded
方法,通过为其分配最大等待时间来等待字体加载。 默认的最大等待时间为500毫秒。
这是一个关于如何在项目中使用名为Lobster的Web字体的小示例。
: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")
在 IronPDF WaitFor 类文档中探索更多 WaitFor 选项,例如用于字体、JavaScript、HTML 元素和网络空闲的选项。
导入字体文件示例
要使用现有的字体文件,在CSS样式中应用@font-face规则。 它也适用于使用 @font-face 规则和嵌入 base64 编码的 woff 文件的组合。 在下面的例子中,我将使用Pixelify Sans 字体。
: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")
限制在Azure PDF上
Azure 托管平台不支持服务器在其较低的共享 Web 应用程序层加载 SVG 字体。 然而,Azure的VPS和Web Role没有以相同方式进行沙盒处理,且支持网页字体渲染。