HTML到PDF中使用自定义字体

import com.ironsoftware.ironpdf.PdfDocument;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;

import java.io.IOException;
import java.nio.file.Paths;

String htmlWithFont = "<link href=\"https://fonts.googleapis.com/css?family=Lobster\"rel=\"stylesheet\"><p style=\"font-family: 'Lobster', serif; font-size:30px;\"> Hello Google Fonts</p>";
ChromePdfRenderOptions renderOptions = new ChromePdfRenderOptions();
renderOptions.setWaitFor(new WaitFor(500));

PdfDocument doc = PdfDocument.renderHtmlAsPdf(htmlWithFont, renderOptions);

// Save the PDF
try {
    doc.saveAs(Paths.get("assets/font.pdf"));
} catch (IOException exception) {
    String.format("Failed to save the PDF to the filesystem: %s", exception.getMessage());
    exception.printStackTrace();
}
mvn install
HTML到PDF中使用自定义字体

HTML到PDF中使用自定义字体

IronPDF将HTML文档完美渲染为PDF文档,同时还包括准确的自定义Web字体渲染,其中包括由Font Awesome和Bootstrap Components等库提供的字体图标。

IronPDF支持自定义WOFF和SVG CSS字体包。 这包括由Google Fonts等在线字体公司提供的自定义Web字体。

为了正确地将包含一个或多个自定义字体的网页转换成PDF,开发者必须在IronPdf的HTML到PDF渲染引擎上设置PDF渲染延迟。这可以通过使用setRenderDelay方法,将ChromePdfRenderOptions实例的renderDelay属性设置为适当的值来实现,具体示例见下文。 最低500毫秒(或半秒)的值在大多数情况下就足以确保在IronPdf将所有网页内容转换为PDF之前,所有自定义字体/图标资源都已完全加载。

但请注意,由于安全原因,网页字体在Azure托管的Windows应用程序上不起作用。

学习使用 IronPDF 设置 PDF 渲染延迟
在GitHub上查看

准备开始了吗?

版本:2026.10刚刚发布