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 文件。
IronPDF for Node.js入門指南
1.安裝Node.js :從Node.js官方網站下載並安裝最新版本的Node.js 2.安裝 @ironpdf 軟體包:使用下列終端機指令透過 NPM 安裝IronPDF :
:ProductInstall :ProductInstall3.安裝 IronPDF Engine :安裝適合您作業系統的二進位檔案:
適用於 Windows x64 :
npm install @ironsoftware/ironpdf-engine-windows-x64 npm install @ironsoftware/ironpdf-engine-windows-x64適用於 Windows x86 :
npm install @ironsoftware/ironpdf-engine-windows-x86 npm install @ironsoftware/ironpdf-engine-windows-x86適用於 Linux x64 :
npm install @ironsoftware/ironpdf-engine-linux-x64 npm install @ironsoftware/ironpdf-engine-linux-x64適用於 macOS x64 :
npm install @ironsoftware/ironpdf-engine-macos-x64 npm install @ironsoftware/ironpdf-engine-macos-x64適用於 macOS/ARM :
npm install @ironsoftware/ironpdf-engine-macos-arm64 npm install @ironsoftware/ironpdf-engine-macos-arm64(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);
})();_(如果您收到以下警告,請將 "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;
})();使用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 。
如需更多協助或有任何疑問,請聯絡我們的支援團隊。





