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:

   :ProductInstall
   :ProductInstall
SHELL

3.安裝 IronPDF 引擎:針對您的作業系統安裝適當的二進位檔:

適用於 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

(If you are getting the following warning, please add the line "type":"module" 作為您的 package.json 檔案中的第一級項目。(node:105376) 警告:若要載入 ES 模組,請在 "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 剛剛發布