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

_(如果您收到以下警告,請將 "type": "module" 行作為 package.json 文件中的第一級條目添加。(node:105376) 警告:若要載入 ES 模組,請在 "type": "module" 中設定 @CO-DE-470 @CO@DE470@CO@DE1-470。 (使用 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 來說,工作令人滿意因為它被重視且有實際影響。

準備好開始了嗎?
版本: 2026.3 剛剛發布
Still Scrolling Icon

還在捲動嗎?

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