Custom Fonts in HTML to 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
Custom Fonts in HTML to PDF

Custom Fonts in HTML to PDF

IronPDF從HTML文件完美呈現PDF文件,也包括自定義網頁字體的準確呈現,這些字體包含由Font Awesome和Bootstrap Components等程式庫提供的字型圖標。

IronPDF支援自定義SVG CSS字體包。 這包括由線上字體鑄造廠如Google Fonts提供的自定義網頁字體。

要正確地將包含一個或多個自定義字體的網頁轉換為PDF,開發者必須在renderDelay屬性設置為適當的值來實現,如下面的範例所示。 在大多數情況下,最小值500毫秒(或半秒)將足以確保所有自定義字體/圖標資產在IronPdf將所有網頁內容轉換為PDF之前完全載入。

請注意,由於安全原因,Azure託管的Windows應用程式不支援網頁字體。

學習使用IronPDF設置PDF 渲染延遲
在 GitHub 查看

準備好開始了嗎?

版本:2026.10剛剛發布