自定义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文档,同时应用指定的渲染选项。

渲染后,生成的PDF文档使用saveAs方法以自定义纸张大小保存。

  • outputPath(生成的PDF的保存路径)。

  • ChromePdfRenderer用于处理HTML内容的渲染过程。

  • 创建PdfPaperSize对象来指定自定义5x5英寸纸张大小。 这是通过将PdfPaperSize constructor来实现的。

  • RenderingOptions属性被设置为使用自定义纸张大小。

  • RenderHtmlAsPdf方法根据预定义的设置将给定的HTML内容转换为PDF文档。

  • 最终,outputPath。

在 GitHub 上探索自定义 PDF 页面大小代码示例
在 GitHub 上查看

准备开始了吗?

版本:2026.11刚刚发布