HTML 轉 PDF 中的自訂字型
為確保需耗時載入的資源能正確呈現,務必使用 waitFor 類別指定渲染延遲。 這包括 JavaScript、字型及網路資源等資產。
import {PdfDocument, WaitForType} from "@ironsoftware/ironpdf";
(async () => {
// Define the HTML content with a custom font from Google Fonts
const htmlWithFont = `
<h1>Google Font</h1>
<link href="https://fonts.googleapis.com/css?family=Lobster" rel="stylesheet">
<p style="font-family: 'Lobster', serif; font-size:30px;">Hello Google Fonts</p>
`;
// Configure render options
const options = {
// Delay render to finish font loading
waitFor: {
type: WaitForType.RenderDelay,
maxWaitTime: 500,
},
};
// Render HTML content with the custom font to a PDF
const doc = await PdfDocument.fromHtml(htmlWithFont, { renderOptions: options });
// Save the PDF
await doc.saveAs("font.pdf");
})();為確保需耗時載入的資源能正確呈現,務必使用 waitFor 類別指定渲染延遲。 這包括 JavaScript、字型及網路資源等資產。