如何在Node.js中建立PDF檔案 Copy for LLMsCopy for LLMs Copy page as Markdown for LLMs
# 如何在Node.js中建立PDF檔案
在Node.js中程式生成PDF檔案需要一個能夠準確處理HTML渲染、支援現代CSS並與Node的異步模式清晰整合的程式庫。 [IronPDF](https://ironpdf.com/nodejs/docs/) 使用基於Chromium的渲染引擎將HTML內容轉換為與Chrome列印輸出匹配的PDF,支援完整的CSS、內聯JavaScript和開箱即用的響應式佈局。
本指南涵蓋了完整的工作流程:安裝、從HTML字串、HTML檔案和URLs生成PDF、配置輸出選項、以及應用企業功能如標題、頁腳、數位簽名和加密。
*as-heading:2(快速入門:在Node.js中建立PDF)*
1. 通過npm安裝IronPDF:`npm install @ironsoftware/ironpdf`
2. 從`PdfDocument`
3. 使用您的HTML內容呼叫`PdfDocument.fromHtml()`
4. 呼叫`.saveAs()`將PDF檔案寫入磁碟
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
const pdf = await PdfDocument.fromHtml("<h1>Hello, PDF!</h1><p>Generated with IronPDF.</p>");
await pdf.saveAs("output.pdf");
```
在生產環境運行前為您的平台配置`IronPdfEngine`。 查看適用於Windows、Linux、macOS和Docker的[IronPdfEngine設置指南](https://ironpdf.com/nodejs/get-started/use-ironpdfengine/)。
<div class="hsg-featured-snippet">
<h3>最小化工作流程(5步)</h3>
<ol>
<li>安裝IronPDF:<code>npm install @ironsoftware/ironpdf</code></li>
<li>從<code>@ironsoftware/ironpdf</code>中導入<code>PdfDocument</code></li>
<li>使用<code>PdfDocument.fromHtml()</code>、<code>PdfDocument.fromFile()</code>或<code>PdfDocument.fromUrl()</code>生成PDF</li>
<li>應用可選設置:紙張尺寸、邊距、標頭、頁腳</li>
<li>使用<code>pdf.saveAs("output.pdf")</code>保存輸出</li>
</ol>
</div>
## 如何安裝IronPDF for Node.js?
使用[npm](https://www.npmjs.com/package/@ironsoftware/ironpdf)安裝`@ironsoftware/ironpdf`包。 該包適用於[Node.js](https://nodejs.org/) 12.0或更高版本,同時支援ESM和CommonJS模組格式。
```bash
//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/install.sh
npm install @ironsoftware/ironpdf
```
IronPDF for Node.js依賴於IronPDF引擎二進制檔案,其負責處理基於Chromium的渲染。 引擎在首次運行時自動下載,但您可以預先安裝平台特定的包以避免運行時下載。 這在受限網路環境或基於Docker的CI管線中非常有用:
<table class="content__data-table" data-content-table>
<caption>IronPDF引擎平台包可在離線或受限環境中預先安裝</caption>
<thead>
<tr><th>平台</th><th>包</th></tr>
</thead>
<tbody>
<tr><td>Windows x64</td><td><code>@ironsoftware/ironpdf-engine-windows-x64</code></td></tr>
<tr><td>Linux x64</td><td><code>@ironsoftware/ironpdf-engine-linux-x64</code></td></tr>
<tr><td>macOS x64</td><td><code>@ironsoftware/ironpdf-engine-macos-x64</code></td></tr>
<tr><td>macOS ARM</td><td><code>@ironsoftware/ironpdf-engine-macos-arm64</code></td></tr>
</tbody>
</table>
[[i:(有效的授權金鑰可移除生成的PDF上的浮水印。 在第一次`IronPdfGlobalConfig.setConfig({ licenseKey: "YOUR-KEY" })`來應用它。)]} 請遵循完整的[授權金鑰設置說明](https://ironpdf.com/nodejs/get-started/license-keys/)。
)]]
### IronPDF的系統要求是什麼?
IronPDF for Node.js在Windows、Linux、macOS和Docker環境的Node.js 12.0或更高版本上運行。 該程式庫支援x64和ARM64架構,使其適用於本地開發和容器化部署。 對於雲環境,IronPDF還支援連接到[遠端IronPDF引擎](https://ironpdf.com/nodejs/get-started/use-ironpdfengine/),而不是本地運行引擎。
<hr />
## 如何在Node.js中從HTML建立PDF?
使用`PdfDocument.fromHtml()`從HTML字串生成PDF。 該方法接受任何有效的HTML,包括內聯`<style>`區塊、通過CDN載入的外部字型和佈局系統如CSS Grid或Flexbox。 Chromium渲染器在生成PDF之前解析所有資源。
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.header { background: #2b4c8c; color: white; padding: 24px 32px; }
.header h1 { margin: 0; font-size: 22px; }
.body { padding: 32px; }
table { width: 100%; border-collapse: collapse; margin-top: 16px; }
th { background: #f0f4fb; text-align: left; padding: 8px 12px; }
td { padding: 8px 12px; border-bottom: 1px solid #e0e0e0; }
</style>
</head>
<body>
<div class="header"><h1>Invoice #INV-2025-0042</h1></div>
<div class="body">
<p>Date: ${new Date().toLocaleDateString()}</p>
<table>
<tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Total</th></tr>
<tr><td>IronPDF Enterprise License</td><td>1</td><td>$499.00</td><td>$499.00</td></tr>
<tr><td>Priority Support (1 year)</td><td>1</td><td>$199.00</td><td>$199.00</td></tr>
</table>
<p style="text-align:right; margin-top:16px;"><strong>Total: $698.00</strong></p>
</div>
</body>
</html>`;
// Generate the PDF from the HTML string
const pdf = await PdfDocument.fromHtml(htmlContent);
await pdf.saveAs("invoice.pdf");
```
`PdfDocument`實例。 使用您期望的輸出路徑呼叫`.then()`處理器。 Chromium引擎在捕獲輸出之前處理字體載入、外部樣式表和資產解析。 有關更多模式,請查看[HTML字串轉PDF範例](https://ironpdf.com/nodejs/examples/using-html-to-create-a-pdf/)。
### 如何從HTML檔案建立PDF?
使用`.html`檔案。 此方法對於模板文件特別有效,其中HTML與應用程式邏輯分開維護。
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
// Load an HTML file and render it as a PDF
const pdf = await PdfDocument.fromFile("./templates/report-template.html");
await pdf.saveAs("./output/report.pdf");
```
渲染器從HTML檔案的目錄解析相對資產路徑,因此本地CSS和圖像引用無需額外配置即可運行。 請參閱[HTML檔案轉PDF範例](https://ironpdf.com/nodejs/examples/file-to-pdf/)以獲得其他選項,包括如何覆寫資產解析的基準URL。
### 如何從URL生成PDF?
使用`PdfDocument.fromUrl()`將現有的網頁渲染為PDF。 渲染器在捕獲輸出之前,以完整的瀏覽器會話的方式載入頁面,執行JavaScript、應用CSS,並等待動態內容。
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
// Render a live webpage to PDF
const pdf = await PdfDocument.fromUrl("https://ironpdf.com/nodejs/");
await pdf.saveAs("ironpdf-homepage.pdf");
```
對於通過JavaScript異步載入內容的頁面,將此與`renderDelay`選項(以毫秒為單位)結合使用,以允許在捕獲之前有額外時間。 [URL轉PDF範例](https://ironpdf.com/nodejs/examples/converting-a-url-to-a-pdf/)展示了延遲配置和身份驗證標頭。
<hr />
## 如何配置PDF輸出設置?
將配置物件作為第二個參數傳遞給`PdfDocument.fromUrl()`。 配置物件接受紙張尺寸、邊距、方向和渲染選項。
```nodejs
import { PdfDocument, PdfPaperSize } from "@ironsoftware/ironpdf";
const config = {
paperSize: PdfPaperSize.A4,
marginTop: 25,
marginBottom: 25,
marginLeft: 20,
marginRight: 20,
landscape: false,
printBackground: true,
};
const pdf = await PdfDocument.fromHtml("<h1>Configured PDF</h1>", config);
await pdf.saveAs("configured-output.pdf");
```
主要配置選項包括:
- `PdfPaperSize`枚舉值(A4、Letter、Legal和[自定義尺寸](https://ironpdf.com/nodejs/examples/custom-pdf-paper-size/))
- `landscape`:為橫向[頁面方向](https://ironpdf.com/nodejs/examples/pdf-page-orientation/)設置`true`
- `printBackground`:包含CSS背景顏色和圖像
- `marginTop/Bottom/Left/Right`:以毫米為單位的頁面邊距
- `renderDelay`:用於JavaScript大量頁面捕捉前的等待毫秒數
### 如何將頁眉和頁腳新增到PDF?
通過配置物件中的`htmlFooter`屬性新增標頭和頁腳。 兩者都接受完整的HTML字串,從而能夠將樣式內容與頁碼、日期和動態文字結合。
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
const config = {
htmlHeader: {
htmlFragment: "<div style='text-align:center; font-size:12px; color:#666;'>Quarterly Report - Confidential</div>",
dividerLine: true,
},
htmlFooter: {
htmlFragment: "<div style='text-align:right; font-size:10px;'>Page {page} of {total-pages}</div>",
dividerLine: true,
},
};
const pdf = await PdfDocument.fromHtml("<h1>Q3 Financial Summary</h1><p>See attached tables.</p>", config);
await pdf.saveAs("report-with-headers.pdf");
```
使用`{total-pages}`標記在頁腳HTML中; IronPDF在渲染時間替換正確的值。有關更多標頭和頁腳模式,包括[HTML標頭和頁腳](https://ironpdf.com/nodejs/examples/html-headers-and-footers/)與[進階標頭技巧](https://ironpdf.com/nodejs/examples/adding-headers-and-footers-advanced/),請參閱連結的範例。
<hr />
## 如何為PDF新增安全和元資料?
使用`PdfDocument`安全方法將[密碼保護和加密](https://ironpdf.com/nodejs/examples/security-and-metadata/)應用到生成的PDF。 該程式庫支援擁有者和使用者密碼,提供列印、複製和編輯的詳細權限控制。
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
const pdf = await PdfDocument.fromHtml("<h1>Confidential Document</h1>");
// Apply password protection with granular permissions
await pdf.securePdf({
userPassword: "view-password",
ownerPassword: "admin-password",
allowUserAnnotations: false,
allowUserPrinting: true,
allowUserCopyPasteContent: false,
});
await pdf.saveAs("secured-document.pdf");
```
[加密和解密](https://ironpdf.com/nodejs/examples/encryption-and-decryption/)在應用密碼時預設使用AES 256位加密。 對於文件身份驗證工作流程,IronPDF還支援使用X.509證書的[數位簽名](https://ironpdf.com/nodejs/examples/digitally-sign-a-pdf/)。
[[t:(對於需要長期檔案遵從性的文件,請使用`PdfDocument.fromHtml()`,然後使用PDF/A轉換方法。 IronPDF支援[PDF/A合規](https://ironpdf.com/nodejs/examples/pdfa/)和[PDF/UA可存取性標準](https://ironpdf.com/nodejs/examples/pdfua/)。)]]
<hr />
## 如何合併和操作現有的PDF?
`PdfDocument`提供合併、分割和修改現有PDF文件的方法。 使用`PdfDocument.fromFile()`載入現有的PDF,並使用操作方法新增或移除頁面、印章內容或替換文字。
```nodejs
import { PdfDocument } from "@ironsoftware/ironpdf";
// Merge two PDF files into one
const merged = await PdfDocument.mergePdf([
await PdfDocument.fromFile("./report-part1.pdf"),
await PdfDocument.fromFile("./report-part2.pdf"),
]);
await merged.saveAs("./complete-report.pdf");
```
`PdfDocument`實例的陣列並返回一個單一的合併文件。 頁面順序遵循陣列順序,因此輸出保留輸入文件的順序。
其他常見的操作操作:
- 使用`removePages()`從PDF中[移除頁面](https://ironpdf.com/nodejs/examples/remove-page-from-pdf/)
- 將[HTML內容印章](https://ironpdf.com/nodejs/examples/stamping-new-content/)到現有文件上
- 在所有頁面[查找和替換文字](https://ironpdf.com/nodejs/examples/replace-text-in-pdf/)
- 從PDF中[提取文字](https://ironpdf.com/nodejs/examples/reading-pdf-text/)以進行索引或處理
對於高容量場景,[多執行緒PDF生成](https://ironpdf.com/nodejs/examples/threading/)可讓您並行處理多個文件。 [PDF壓縮](https://ironpdf.com/nodejs/examples/pdf-compression/)可減少檔案大小以進行儲存和傳輸。
<hr />
## 如何為複雜內容生成PDF,如圖表和Angular頁面?
IronPDF使用完全Chromium瀏覽器會話渲染頁面,這意味著生成圖表、圖表或資料視覺化的JavaScript庫正確渲染,包括[JavaScript圖表庫](https://ironpdf.com/nodejs/examples/js-charts-to-pdf/)。 使用像Angular這樣的框架構建的單頁應用程式也可以使用[Angular轉PDF範例](https://ironpdf.com/nodejs/examples/angular-to-pdf/)乾淨轉換。
對於Unicode密集型文件或多語言輸出,IronPDF處理[Unicode和國際字元集](https://ironpdf.com/nodejs/examples/unicode/)無需額外配置。 [Google字型整合範例](https://ironpdf.com/nodejs/examples/google-fonts-htmltopdf/)顯示如何在渲染的HTML中載入網頁字形,以確保輸出PDF的字型一致性。
[[i:(IronPDF的Chromium渲染器在捕捉PDF之前執行JavaScript。 如果您的圖表庫或SPA需要在DOM載入後額外時間來渲染資料,請以毫秒為單位設置`renderDelay`以給予頁面完全渲染的時間。)]]
<hr />
## Node.js中建立PDF的下一步是什麼?
本指南涵蓋了IronPDF的核心工作流程:安裝程式庫、從HTML字串、檔案和URL生成PDF、配置輸出設置、新增頁眉和頁腳、應用安全措施以及操作現有PDF。
繼續了解完整的[HTML到PDF教學](https://ironpdf.com/nodejs/tutorials/html-to-pdf/)以更深入了解渲染選項,或瀏覽[API參考](https://ironpdf.com/nodejs/object-reference/api/)以獲得完整的`PdfDocument`方法和配置屬性列表。
[開始您的免費試用](trial-license)以生成無浮水印的PDF,或[查看授權選項](licensing)以進行生產部署。
Ask ChatGPT about this page
Ask Gemini about this page
Ask Perplexity about this page
在Node.js中程式生成PDF檔案需要一個能夠準確處理HTML渲染、支援現代CSS並與Node的異步模式清晰整合的程式庫。 IronPDF 使用基於Chromium的渲染引擎將HTML內容轉換為與Chrome列印輸出匹配的PDF,支援完整的CSS、內聯JavaScript和開箱即用的響應式佈局。
本指南涵蓋了完整的工作流程:安裝、從HTML字串、HTML檔案和URLs生成PDF、配置輸出選項、以及應用企業功能如標題、頁腳、數位簽名和加密。
通過npm安裝IronPDF:npm install @ironsoftware/ironpdf
從PdfDocument
使用您的HTML內容呼叫PdfDocument.fromHtml()
呼叫.saveAs()將PDF檔案寫入磁碟
import { PdfDocument } from "@ironsoftware/ironpdf" ;
const pdf = await PdfDocument .fromHtml( "<h1>Hello, PDF!</h1><p>Generated with IronPDF.</p>" );
await pdf.saveAs( "output.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
const pdf = await PdfDocument.fromHtml("<h1>Hello, PDF!</h1><p>Generated with IronPDF.</p>");
await pdf.saveAs("output.pdf");
JavaScript
在生產環境運行前為您的平台配置IronPdfEngine。 查看適用於Windows、Linux、macOS和Docker的IronPdfEngine設置指南 。
最小化工作流程(5步) 安裝IronPDF:npm install @ironsoftware/ironpdf 從@ironsoftware/ironpdf中導入PdfDocument 使用PdfDocument.fromHtml()、PdfDocument.fromFile()或PdfDocument.fromUrl()生成PDF 應用可選設置:紙張尺寸、邊距、標頭、頁腳 使用pdf.saveAs("output.pdf")保存輸出
如何安裝IronPDF for Node.js?
使用npm 安裝@ironsoftware/ironpdf包。 該包適用於Node.js 12.0或更高版本,同時支援ESM和CommonJS模組格式。
//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/install.sh
npm install @ironsoftware/ironpdf
//:path=/static-assets/pdf/content-code-examples/tutorials/nodejs-create-pdf/install.sh
npm install @ironsoftware/ironpdf
SHELL
IronPDF for Node.js依賴於IronPDF引擎二進制檔案,其負責處理基於Chromium的渲染。 引擎在首次運行時自動下載,但您可以預先安裝平台特定的包以避免運行時下載。 這在受限網路環境或基於Docker的CI管線中非常有用:
有效的授權金鑰可移除生成的PDF上的浮水印。 在第一次IronPdfGlobalConfig.setConfig({ licenseKey: "YOUR-KEY" })來應用它。)]} 請遵循完整的授權金鑰設置說明 。
IronPDF的系統要求是什麼?
IronPDF for Node.js在Windows、Linux、macOS和Docker環境的Node.js 12.0或更高版本上運行。 該程式庫支援x64和ARM64架構,使其適用於本地開發和容器化部署。 對於雲環境,IronPDF還支援連接到遠端IronPDF引擎 ,而不是本地運行引擎。
如何在Node.js中從HTML建立PDF?
使用PdfDocument.fromHtml()從HTML字串生成PDF。 該方法接受任何有效的HTML,包括內聯<style>區塊、通過CDN載入的外部字型和佈局系統如CSS Grid或Flexbox。 Chromium渲染器在生成PDF之前解析所有資源。
import { PdfDocument } from "@ironsoftware/ironpdf" ;
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.header { background: #2b4c8c; color: white; padding: 24px 32px; }
.header h1 { margin: 0; font-size: 22px; }
.body { padding: 32px; }
table { width: 100%; border-collapse: collapse; margin-top: 16px; }
th { background: #f0f4fb; text-align: left; padding: 8px 12px; }
td { padding: 8px 12px; border-bottom: 1px solid #e0e0e0; }
</style>
</head>
<body>
<div class="header"><h1>Invoice #INV-2025-0042</h1></div>
<div class="body">
<p>Date: ${ new Date() . toLocaleDateString() } </p>
<table>
<tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Total</th></tr>
<tr><td>IronPDF Enterprise License</td><td>1</td><td>$499.00</td><td>$499.00</td></tr>
<tr><td>Priority Support (1 year)</td><td>1</td><td>$199.00</td><td>$199.00</td></tr>
</table>
<p style="text-align:right; margin-top:16px;"><strong>Total: $698.00</strong></p>
</div>
</body>
</html>` ;
// Generate the PDF from the HTML string
const pdf = await PdfDocument .fromHtml(htmlContent);
await pdf.saveAs( "invoice.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial, sans-serif; margin: 0; padding: 0; }
.header { background: #2b4c8c; color: white; padding: 24px 32px; }
.header h1 { margin: 0; font-size: 22px; }
.body { padding: 32px; }
table { width: 100%; border-collapse: collapse; margin-top: 16px; }
th { background: #f0f4fb; text-align: left; padding: 8px 12px; }
td { padding: 8px 12px; border-bottom: 1px solid #e0e0e0; }
</style>
</head>
<body>
<div class="header"><h1>Invoice #INV-2025-0042</h1></div>
<div class="body">
<p>Date: ${new Date().toLocaleDateString()}</p>
<table>
<tr><th>Item</th><th>Qty</th><th>Unit Price</th><th>Total</th></tr>
<tr><td>IronPDF Enterprise License</td><td>1</td><td>$499.00</td><td>$499.00</td></tr>
<tr><td>Priority Support (1 year)</td><td>1</td><td>$199.00</td><td>$199.00</td></tr>
</table>
<p style="text-align:right; margin-top:16px;"><strong>Total: $698.00</strong></p>
</div>
</body>
</html>`;
// Generate the PDF from the HTML string
const pdf = await PdfDocument.fromHtml(htmlContent);
await pdf.saveAs("invoice.pdf");
JavaScript
PdfDocument實例。 使用您期望的輸出路徑呼叫.then()處理器。 Chromium引擎在捕獲輸出之前處理字體載入、外部樣式表和資產解析。 有關更多模式,請查看HTML字串轉PDF範例 。
如何從HTML檔案建立PDF?
使用.html檔案。 此方法對於模板文件特別有效,其中HTML與應用程式邏輯分開維護。
import { PdfDocument } from "@ironsoftware/ironpdf" ;
// Load an HTML file and render it as a PDF
const pdf = await PdfDocument .fromFile( "./templates/report-template.html" );
await pdf.saveAs( "./output/report.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
// Load an HTML file and render it as a PDF
const pdf = await PdfDocument.fromFile("./templates/report-template.html");
await pdf.saveAs("./output/report.pdf");
JavaScript
渲染器從HTML檔案的目錄解析相對資產路徑,因此本地CSS和圖像引用無需額外配置即可運行。 請參閱HTML檔案轉PDF範例 以獲得其他選項,包括如何覆寫資產解析的基準URL。
如何從URL生成PDF?
使用PdfDocument.fromUrl()將現有的網頁渲染為PDF。 渲染器在捕獲輸出之前,以完整的瀏覽器會話的方式載入頁面,執行JavaScript、應用CSS,並等待動態內容。
import { PdfDocument } from "@ironsoftware/ironpdf" ;
// Render a live webpage to PDF
const pdf = await PdfDocument .fromUrl( "https://ironpdf.com/nodejs/" );
await pdf.saveAs( "ironpdf-homepage.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
// Render a live webpage to PDF
const pdf = await PdfDocument.fromUrl("https://ironpdf.com/nodejs/");
await pdf.saveAs("ironpdf-homepage.pdf");
JavaScript
對於通過JavaScript異步載入內容的頁面,將此與renderDelay選項(以毫秒為單位)結合使用,以允許在捕獲之前有額外時間。 URL轉PDF範例 展示了延遲配置和身份驗證標頭。
如何配置PDF輸出設置?
將配置物件作為第二個參數傳遞給PdfDocument.fromUrl()。 配置物件接受紙張尺寸、邊距、方向和渲染選項。
import { PdfDocument , PdfPaperSize } from "@ironsoftware/ironpdf" ;
const config = {
paperSize: PdfPaperSize . A4 ,
marginTop: 25 ,
marginBottom: 25 ,
marginLeft: 20 ,
marginRight: 20 ,
landscape: false ,
printBackground: true ,
};
const pdf = await PdfDocument .fromHtml( "<h1>Configured PDF</h1>" , config);
await pdf.saveAs( "configured-output.pdf" );
import { PdfDocument, PdfPaperSize } from "@ironsoftware/ironpdf";
const config = {
paperSize: PdfPaperSize.A4,
marginTop: 25,
marginBottom: 25,
marginLeft: 20,
marginRight: 20,
landscape: false,
printBackground: true,
};
const pdf = await PdfDocument.fromHtml("<h1>Configured PDF</h1>", config);
await pdf.saveAs("configured-output.pdf");
JavaScript
主要配置選項包括:
PdfPaperSize枚舉值(A4、Letter、Legal和自定義尺寸 )
landscape:為橫向頁面方向 設置true
printBackground:包含CSS背景顏色和圖像
marginTop/Bottom/Left/Right:以毫米為單位的頁面邊距
renderDelay:用於JavaScript大量頁面捕捉前的等待毫秒數
如何將頁眉和頁腳新增到PDF?
通過配置物件中的htmlFooter屬性新增標頭和頁腳。 兩者都接受完整的HTML字串,從而能夠將樣式內容與頁碼、日期和動態文字結合。
import { PdfDocument } from "@ironsoftware/ironpdf" ;
const config = {
htmlHeader: {
htmlFragment: "<div style='text-align:center; font-size:12px; color:#666;'>Quarterly Report - Confidential</div>" ,
dividerLine: true ,
},
htmlFooter: {
htmlFragment: "<div style='text-align:right; font-size:10px;'>Page {page} of {total-pages}</div>" ,
dividerLine: true ,
},
};
const pdf = await PdfDocument .fromHtml( "<h1>Q3 Financial Summary</h1><p>See attached tables.</p>" , config);
await pdf.saveAs( "report-with-headers.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
const config = {
htmlHeader: {
htmlFragment: "<div style='text-align:center; font-size:12px; color:#666;'>Quarterly Report - Confidential</div>",
dividerLine: true,
},
htmlFooter: {
htmlFragment: "<div style='text-align:right; font-size:10px;'>Page {page} of {total-pages}</div>",
dividerLine: true,
},
};
const pdf = await PdfDocument.fromHtml("<h1>Q3 Financial Summary</h1><p>See attached tables.</p>", config);
await pdf.saveAs("report-with-headers.pdf");
JavaScript
使用{total-pages}標記在頁腳HTML中; IronPDF在渲染時間替換正確的值。有關更多標頭和頁腳模式,包括HTML標頭和頁腳 與進階標頭技巧 ,請參閱連結的範例。
如何為PDF新增安全和元資料?
使用PdfDocument安全方法將密碼保護和加密 應用到生成的PDF。 該程式庫支援擁有者和使用者密碼,提供列印、複製和編輯的詳細權限控制。
import { PdfDocument } from "@ironsoftware/ironpdf" ;
const pdf = await PdfDocument .fromHtml( "<h1>Confidential Document</h1>" );
// Apply password protection with granular permissions
await pdf.securePdf({
userPassword: "view-password" ,
ownerPassword: "admin-password" ,
allowUserAnnotations: false ,
allowUserPrinting: true ,
allowUserCopyPasteContent: false ,
});
await pdf.saveAs( "secured-document.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
const pdf = await PdfDocument.fromHtml("<h1>Confidential Document</h1>");
// Apply password protection with granular permissions
await pdf.securePdf({
userPassword: "view-password",
ownerPassword: "admin-password",
allowUserAnnotations: false,
allowUserPrinting: true,
allowUserCopyPasteContent: false,
});
await pdf.saveAs("secured-document.pdf");
JavaScript
加密和解密 在應用密碼時預設使用AES 256位加密。 對於文件身份驗證工作流程,IronPDF還支援使用X.509證書的數位簽名 。
對於需要長期檔案遵從性的文件,請使用PdfDocument.fromHtml(),然後使用PDF/A轉換方法。 IronPDF支援PDF/A合規 和PDF/UA可存取性標準 。
如何合併和操作現有的PDF?
PdfDocument提供合併、分割和修改現有PDF文件的方法。 使用PdfDocument.fromFile()載入現有的PDF,並使用操作方法新增或移除頁面、印章內容或替換文字。
import { PdfDocument } from "@ironsoftware/ironpdf" ;
// Merge two PDF files into one
const merged = await PdfDocument .mergePdf([
await PdfDocument .fromFile( "./report-part1.pdf" ),
await PdfDocument .fromFile( "./report-part2.pdf" ),
]);
await merged.saveAs( "./complete-report.pdf" );
import { PdfDocument } from "@ironsoftware/ironpdf";
// Merge two PDF files into one
const merged = await PdfDocument.mergePdf([
await PdfDocument.fromFile("./report-part1.pdf"),
await PdfDocument.fromFile("./report-part2.pdf"),
]);
await merged.saveAs("./complete-report.pdf");
JavaScript
PdfDocument實例的陣列並返回一個單一的合併文件。 頁面順序遵循陣列順序,因此輸出保留輸入文件的順序。
其他常見的操作操作:
對於高容量場景,多執行緒PDF生成 可讓您並行處理多個文件。 PDF壓縮 可減少檔案大小以進行儲存和傳輸。
如何為複雜內容生成PDF,如圖表和Angular頁面?
IronPDF使用完全Chromium瀏覽器會話渲染頁面,這意味著生成圖表、圖表或資料視覺化的JavaScript庫正確渲染,包括JavaScript圖表庫 。 使用像Angular這樣的框架構建的單頁應用程式也可以使用Angular轉PDF範例 乾淨轉換。
對於Unicode密集型文件或多語言輸出,IronPDF處理Unicode和國際字元集 無需額外配置。 Google字型整合範例 顯示如何在渲染的HTML中載入網頁字形,以確保輸出PDF的字型一致性。
IronPDF的Chromium渲染器在捕捉PDF之前執行JavaScript。 如果您的圖表庫或SPA需要在DOM載入後額外時間來渲染資料,請以毫秒為單位設置renderDelay以給予頁面完全渲染的時間。
Node.js中建立PDF的下一步是什麼?
本指南涵蓋了IronPDF的核心工作流程:安裝程式庫、從HTML字串、檔案和URL生成PDF、配置輸出設置、新增頁眉和頁腳、應用安全措施以及操作現有PDF。
繼續了解完整的HTML到PDF教學 以更深入了解渲染選項,或瀏覽API參考 以獲得完整的PdfDocument方法和配置屬性列表。
開始您的免費試用 以生成無浮水印的PDF,或查看授權選項 以進行生產部署。
常見問題 使用npm install @ironsoftware/ironpdf安裝IronPDF,然後使用您的HTML字串調用PdfDocument.fromHtml(),並使用saveAs()保存結果。該程式庫會自動處理所有渲染。
在您的專案目錄中運行npm install @ironsoftware/ironpdf。IronPDF引擎的二進制檔案在第一次使用時會自動下載。對於離線或CI環境,單獨安裝平台特定的引擎包,例如@ironsoftware/ironpdf-engine-linux-x64。
可以。IronPDF使用基於Chromium的渲染引擎,在將HTML轉換為PDF時保留所有CSS樣式、外部字體和JavaScript執行。輸出效果與Chrome的列印預覽行為一致。
IronPDF支持數位簽名、AES 256位加密、具有細粒度權限的密碼保護、自定義標頭和頁尾與頁碼標記、PDF合併、頁面移除、HTML標記、文字提取、PDF/A合規性和多執行緒生成。
使用PdfDocument.fromFile('./path/to/template.html')。渲染器會從HTML文件的目錄解析相對資產路徑,因此本地CSS和圖像參考無需額外配置即可運行。
可以。使用PdfDocument.fromFile()載入現有PDF,然後使用文字提取方法從任何頁面讀取內容。IronPDF支持建立新PDF和處理現有文件。
可以。Chromium渲染器在捕捉PDF之前會執行JavaScript。對於異步載入內容的頁面,將renderDelay選項設定為毫秒,讓頁面在捕捉前有更多時間。
Yes, IronPDF supports applying digital signatures to PDFs using X.509 certificates. This feature is useful for document authentication and secure workflows.
IronPDF uses a full Chromium browser session to render pages, which means JavaScript chart libraries and frameworks like Angular are fully supported. This allows charts, graphs, and other complex visualizations to render correctly in the output PDFs.
IronPDF requires Node.js 12.0 or higher and runs on Windows, Linux, macOS, and Docker environments. It supports both x64 and ARM64 architectures, making it suitable for various deployment scenarios including local development and cloud environments.
技術作家
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。
...
閱讀更多