自訂 PDF 尺寸
import {PdfDocument, PaperSizeUnit} from "@ironsoftware/ironpdf";

(async () => {
    // Define the HTML content
    const html = `<p>Lorem ipsum dolor sit amet...</p>`;

    // Configure render options
    const options = {
        // Set the paper size to custom (5x5 inches)
        paperSize: {
            unit: PaperSizeUnit.Inch,
            height: 5,
            width: 5,
        },
    };

    // Render HTML content to a PDF
    const pdf = await PdfDocument.fromHtml(html, { renderOptions: options });

    // Save the PDF with the custom paper size
    await pdf.saveAs("customPaperSize.pdf");
})();
npm i @ironsoftware/ironpdf
自訂 PDF 尺寸

自訂 PDF 尺寸

此程式碼範例說明如何在將HTML內容轉換為PDF文件時定義自訂紙張大小。

使用paperSize屬性在渲染選項中自訂紙張大小。 options物件用於指定自訂的紙張大小,將尺寸設定為自訂的5x5英寸。 您可以根據需要調整width屬性以符合您想要的紙張大小。

利用IronPDF程式庫中的PdfDocument.fromHtml方法,將HTML內容轉換為PDF文件,並應用指定的渲染選項。

渲染後,使用saveAs方法將生成的PDF文件以自訂紙張大小儲存。

  • outputPath(生成的PDF將儲存的檔案路徑)。

  • 使用ChromePdfRenderer處理HTML內容的渲染過程。

  • 建立一個PdfPaperSize物件來指定5x5英寸的自訂紙張大小。 這是通過將PdfPaperSize constructor實現的。

  • renderer被設置為使用自訂紙張大小。

  • RenderHtmlAsPdf方法根據預定義的設定將給定的HTML內容轉換為PDF文件。

  • 最後,outputPath處儲存PDF文件。

在GitHub上探索自訂PDF頁面大小程式碼範例
在 GitHub 上查看

準備好開始了嗎?

版本:2026.11剛剛發布