IronPDF for Node.js - Create, Edit, and Read PDFs in Node.js Scripts

This article was translated from English: Does it need improvement?
Translated
View the article in English

IronPDF 是一个 PDF 库,它简化了使用 Node.js 编程创建和自定义 PDF 文档所需的工作。

IronPDF 由 Iron Software 开发,该公司维护着一套强大、高性能的文档处理库。

IronPDF is also available for use in .NET (C# and VB.NET), Java PDF Library, and Python PDF Library.

IronPDF for Node.js 的关键特性

  1. 从 HTML、CSS、JavaScript、图像和其他文件类型生成 PDF。
  2. 为 PDF 文档添加页眉、页脚、附件、数字签名、水印和书签。
  3. 使用密码、数字签名、元数据和其他安全设置保护 PDF 防止未经授权的访问。
  4. 完整的多线程和异步支持,以获得对关键任务应用的最佳性能。

IronPDF 拥有超过 50 个用于 PDF 创建和编辑的高级功能,用于创建、格式化和编辑 PDF 文档。

开始使用 IronPDF for Node.js

  1. 安装 Node.js:从官方 Node.js 网站下载并安装最新版 Node.js。
  2. 安装 @ironpdf:使用下面的终端命令通过 NPM 安装 IronPDF:

     npm i @ironsoftware/ironpdf
  3. 安装 IronPDF Engine:为您的操作系统安装合适的二进制文件:

    对于 Windows x64

    npm install @ironsoftware/ironpdf-engine-windows-x64
    npm install @ironsoftware/ironpdf-engine-windows-x64
    SHELL

    对于 Windows x86

    npm install @ironsoftware/ironpdf-engine-windows-x86
    npm install @ironsoftware/ironpdf-engine-windows-x86
    SHELL

    对于 Linux x64

    npm install @ironsoftware/ironpdf-engine-linux-x64
    npm install @ironsoftware/ironpdf-engine-linux-x64
    SHELL

    对于 macOS x64

    npm install @ironsoftware/ironpdf-engine-macos-x64
    npm install @ironsoftware/ironpdf-engine-macos-x64
    SHELL

    对于 macOS/ARM

    npm install @ironsoftware/ironpdf-engine-macos-arm64
    npm install @ironsoftware/ironpdf-engine-macos-arm64
    SHELL

    (IronPDF 会在您的 Node.js 项目首次运行时自动尝试下载并安装正确的二进制文件。但是,在某些情况下,可能会被机器阻止。在这种情况下,您需要使用上述命令安装二进制文件。)

  4. 应用许可证密钥(可选):在您的 Node.js 项目中,设置 IronPdfGlobalConfig.licenseKey 属性为有效的许可证密钥,以使用 IronPDF:

    // Import the necessary module
    import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";
    
    (async () => {
       // Create a configuration object with the license key
       const IronPdfConfig = {
           licenseKey: "IRONPDF-MYLICENSE-KEY-1EF01",
       };
    
       // Apply the configuration to the global settings
       IronPdfGlobalConfig.setConfig(IronPdfConfig);
    })();
    // Import the necessary module
    import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";
    
    (async () => {
       // Create a configuration object with the license key
       const IronPdfConfig = {
           licenseKey: "IRONPDF-MYLICENSE-KEY-1EF01",
       };
    
       // Apply the configuration to the global settings
       IronPdfGlobalConfig.setConfig(IronPdfConfig);
    })();
    JAVASCRIPT

    (如果您收到以下警告,请在 package.json 文件中添加第一层条目 "type": "module"。(node:105376)警告:要加载 ES 模块,请在 package.json 中设置 "type": "module" 或使用 .mjs 扩展名。 (使用 node --trace-warnings ... 来显示创建警告的位置)

  5. 启用调试(可选):在您的 Node.js 项目中,设置 IronPdfGlobalConfig.debugMode 属性为 true 以启用调试。 此操作还将在当前目录中创建一个日志文件:

    // Import the necessary module
    import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";
    
    (async () => {
       // Retrieve the current configuration
       var config = IronPdfGlobalConfig.getConfig();
       // Enable debug mode
       config.debugMode = true;
    })();
    // Import the necessary module
    import { IronPdfGlobalConfig } from "@ironsoftware/ironpdf";
    
    (async () => {
       // Retrieve the current configuration
       var config = IronPdfGlobalConfig.getConfig();
       // Enable debug mode
       config.debugMode = true;
    })();
    JAVASCRIPT

使用 IronPDF for Node.js

将HTML转换为PDF

使用 PdfDocument.fromHtml 将原始 HTML 转换为 PDF。 此方法可以处理包含 HTML 的字符串或 HTML 文档的文件路径。

// Import the needed module
import { PdfDocument } from "@ironsoftware/ironpdf";

/* Convert an HTML String to a PDF */
PdfDocument.fromHtml("<h1>Hello world!</h1><p><small>A PDF brought to you by IronPDF for Node.js!</small></p>")
.then((pdf) => {
    // Save the generated PDF
    pdf.saveAs("./html-string-to-pdf.pdf");
});

/* Convert an HTML File to a PDF */
PdfDocument.fromHtml("./index.html")
.then((pdf) => {
    // Save the generated PDF
    pdf.saveAs("./html-file-to-pdf.pdf");
});
// Import the needed module
import { PdfDocument } from "@ironsoftware/ironpdf";

/* Convert an HTML String to a PDF */
PdfDocument.fromHtml("<h1>Hello world!</h1><p><small>A PDF brought to you by IronPDF for Node.js!</small></p>")
.then((pdf) => {
    // Save the generated PDF
    pdf.saveAs("./html-string-to-pdf.pdf");
});

/* Convert an HTML File to a PDF */
PdfDocument.fromHtml("./index.html")
.then((pdf) => {
    // Save the generated PDF
    pdf.saveAs("./html-file-to-pdf.pdf");
});
JAVASCRIPT

转换 URL 为 PDF

PdfDocument.fromUrl 通过 URL 抓取网页内容并将其转换为 PDF。

// Import the needed module
import { PdfDocument } from "@ironsoftware/ironpdf";

/* Convert a URL to a PDF */
(async () => {
   const pdf = await PdfDocument.fromUrl("https://ironpdf.com/nodejs/");
   // Save the generated PDF
   await pdf.saveAs("./url_to_pdf.pdf");
})();
// Import the needed module
import { PdfDocument } from "@ironsoftware/ironpdf";

/* Convert a URL to a PDF */
(async () => {
   const pdf = await PdfDocument.fromUrl("https://ironpdf.com/nodejs/");
   // Save the generated PDF
   await pdf.saveAs("./url_to_pdf.pdf");
})();
JAVASCRIPT

可用的许可和支持

购买 IronPDF 的许可证密钥 以在生产中使用 IronPDF。 或者,申请IronPDF 免费试用许可证以在购买之前试用 IronPDF。

如需更多支持和咨询,请 联系我们的支持团队

Darrius Serrant
全栈软件工程师(WebOps)

Darrius Serrant 拥有迈阿密大学的计算机科学学士学位,目前在 Iron Software 担任全栈 WebOps 市场工程师。从小就被编码吸引,他认为计算机既神秘又易于接触,使其成为创意和问题解决的理想媒介。

在 Iron Software,Darrius 喜欢创造新事物,并简化复杂概念以使其更易理解。作为我们常驻的开发者之一,他还自愿教授学生,与下一代分享他的专业知识。

对于 Darrius 来说,他的工作令人满意,因为它被重视并产生真正的影响。

准备开始了吗?
版本: 2025.11 刚刚发布