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 建立與編輯的高階功能,可協助您建立、格式化及編輯 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

    (當您的 Node.js 專案首次執行時,IronPDF 會嘗試自動下載並安裝適合您系統的正確二進位檔。然而,在某些情況下,此操作可能會遭到系統阻擋。若發生此情況,您將需要使用上述提供的指令來安裝二進位檔。)

  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
技術撰稿人

Curtis Chau 擁有卡爾頓大學(Carleton University)的電腦科學學士學位,專精於前端開發,並精通 Node.js、TypeScript、JavaScript 及 React。他熱衷於打造直觀且美觀的用戶介面,喜歡運用現代框架,並創建結構完善、視覺上吸引人的手冊。

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

準備開始了嗎?
版本: 2026.5 just released
Still Scrolling Icon

還在捲動嗎?

想要快速證明?
執行範例 觀看您的 HTML 變成 PDF。