IronPDF for Node.js - 在 Node.js 腳本中建立、編輯和讀取 PDF 文件

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 也可用於.NET(C# 和 VB.NET)Java PDF 函式庫Python PDF 函式庫

IronPDF for Node.js 的主要特性

  1. 從 HTML、CSS、JavaScript、圖像和其他文件類型產生 PDF。
  2. 在 PDF 文件中新增頁首、頁尾、附件、數位簽章、浮水印和書籤。
  3. 使用密碼、數位簽章、元資料和其他安全設定保護 PDF 檔案免受未經授權的存取。
  4. 完全支援多執行緒和非同步,為關鍵任務應用程式提供最佳效能。

IronPDF 擁有50 多項進階功能,用於建立、格式化和編輯 PDF 文件。

IronPDF for Node.js 入門指南

1.安裝 Node.js :從Node.js 官方網站下載並安裝最新版本的 Node.js。 2.安裝@ironpdf套件:使用下列終端機指令透過 NPM 安裝 IronPDF:

   :ProductInstall
   :ProductInstall
SHELL

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。

如需更多協助或有任何疑問,請聯絡我們的支援團隊

柯蒂斯·週
技術撰稿人

Curtis Chau擁有卡爾頓大學電腦科學學士學位,專長於前端開發,精通Node.js、TypeScript、JavaScript和React。他熱衷於打造直覺美觀的使用者介面,喜歡使用現代框架,並擅長撰寫結構清晰、視覺效果出色的使用者手冊。

除了開發工作之外,柯蒂斯對物聯網 (IoT) 也抱有濃厚的興趣,致力於探索硬體和軟體整合的創新方法。閒暇時,他喜歡玩遊戲和製作 Discord 機器人,將他對科技的熱愛與創造力結合。

準備好開始了嗎?
版本: 2025.12 剛剛發布