IronPDF for Node.js - 在 Node.js 腳本中建立、編輯及讀取 PDF 檔案
IronPDF 是一款 PDF 函式庫,可簡化使用 Node.js 透過程式碼建立及自訂 PDF 文件所需的作業流程。
IronPDF 由 Iron Software 開發,該公司持續維護並擴充一系列功能強大且高效能的文件處理函式庫。
IronPDF 亦可應用於 .NET(C# 和 VB.NET)、Java PDF 函式庫及 Python PDF 函式庫。
IronPDF for Node.js 的主要功能
- 從 HTML、CSS、JavaScript、圖片及其他檔案類型產生 PDF 檔案。
- 向 PDF 文件添加頁首、頁尾、附件、數位簽名、浮水印和書籤。
- 透過密碼、數位簽章、元資料及其他安全性設定,保護 PDF 檔案免於未經授權的存取。
- 全面支援多執行緒與非同步處理,為關鍵任務應用程式提供最佳效能。
IronPDF 具備超過 50 項用於 PDF 建立與編輯的高階功能,可協助您建立、格式化及編輯 PDF 文件。
IronPDF for Node.js 入門指南
- 安裝 Node.js:從官方 Node.js 網站下載並安裝最新版本的 Node.js。
安裝
@ironpdf套件:請使用以下終端機指令透過 NPM 安裝 IronPDF:npm i @ironsoftware/ironpdf
安裝
IronPDF Engine:請安裝適用於您作業系統的二進位檔:適用於 Windows x64:
npm install @ironsoftware/ironpdf-engine-windows-x64npm install @ironsoftware/ironpdf-engine-windows-x64SHELL適用於 Windows x86:
npm install @ironsoftware/ironpdf-engine-windows-x86npm install @ironsoftware/ironpdf-engine-windows-x86SHELL適用於 Linux x64:
npm install @ironsoftware/ironpdf-engine-linux-x64npm install @ironsoftware/ironpdf-engine-linux-x64SHELL適用於 macOS x64:
npm install @ironsoftware/ironpdf-engine-macos-x64npm install @ironsoftware/ironpdf-engine-macos-x64SHELL適用於 macOS/ARM:
npm install @ironsoftware/ironpdf-engine-macos-arm64npm install @ironsoftware/ironpdf-engine-macos-arm64SHELL(當您的 Node.js 專案首次執行時,IronPDF 會嘗試自動下載並安裝適合您系統的正確二進位檔。然而,在某些情況下,此操作可能會遭到系統阻擋。若發生此情況,您將需要使用上述提供的指令來安裝二進位檔。)
套用授權金鑰(可選):在您的 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 ...標示警告產生之處))啟用除錯 (選用):在您的 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");
});將 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");
})();提供授權與技術支援
購買 IronPDF 授權金鑰,即可在生產環境中使用 IronPDF。 您亦可申請 IronPDF 的免費試用授權,在購買前先體驗 IronPDF。
如需更多支援或有任何疑問,請聯絡我們的支援團隊。





