在Linux Docker容器中嵌入自訂字型到IronPDF中
This article was translated from English: Does it need improvement?
Translated
View the article in English
在Windows上生成PDF時,自訂字型能正確嵌入,但在Linux Docker容器中卻無法嵌入。 渲染器未使用預定的字型,而是使用DejaVu Sans作為後備字型。
在Linux系統和Docker容器中,字型必須被明確地安裝或引用。 如果Chrome渲染引擎無法使用某個字型,IronPDF會改用系統預設的後備字型,導致視覺差異和嵌入失敗。
解決方案
選項1:安裝Microsoft核心字型,例如Arial及相似字型
新增到您的Dockerfile中:
RUN apt update && apt install -y ttf-mscorefonts-installer
選項2:通過@font-face嵌入自訂字型
對於非系統字型,如Poppins或Roboto,在HTML中使用.ttf文件的路徑:
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: 'Poppins';
src: url('fonts/Poppins-Regular.ttf');
}
p {
font-family: 'Poppins';
font-size: 16px;
}
</style>
</head>
<body>
<p>Custom font text</p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
@font-face {
font-family: 'Poppins';
src: url('fonts/Poppins-Regular.ttf');
}
p {
font-family: 'Poppins';
font-size: 16px;
}
</style>
</head>
<body>
<p>Custom font text</p>
</body>
</html>
HTML
選項3:使用Print CSS媒體型別
設置@font-face聲明:
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print;
renderer.RenderingOptions.CssMediaType = IronPdf.Rendering.PdfCssMediaType.Print
$vbLabelText
$csharpLabel
完整的Linux Docker設定請參見IronPDF Docker指南。
準備開始了嗎?
Nuget 下載 20,088,359 | 版本: 2026.7 剛剛發布

