Fuentes Personalizadas en HTML a PDF
Para garantizar el renderizado adecuado de los activos que requieren tiempo para cargarse, es esencial especificar un retraso de renderización utilizando la clase waitFor. Esto incluye activos como JavaScript, fuentes, y recursos de red.
Para renderizar contenido HTML con una fuente personalizada de Google Fonts, configure las opciones de renderizado para usar la propiedad waitFor. Establezca el tipo de espera como RenderDelay y especifique un tiempo máximo de espera de 500 milisegundos.
Después de renderizar el HTML, exporte el documento PDF con las capacidades de la Biblioteca de Generación de PDF de IronPDF. Puede verificar el PDF resultante para ver la fuente personalizada perfectamente cargada y mostrada.
// 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!");
}
}




