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!");
}
}




