NReco.PdfGenerator與IronPDF:技術比較指南
NReco.PdfGenerator概述
NReco.PdfGenerator是一個C#程式庫,通過利用wkhtmltopdf命令行工具將HTML文件轉換為PDF。 通過其HtmlToPdfConverter類提供了一個簡單的API,調用wkhtmltopdf進行渲染。
wkhtmltopdf工具使用WebKit Qt作為其渲染引擎,這是大約從2012年的一個版本。雖然廣泛使用,但wkhtmltopdf的開發已在2020年停止,沒有安全更新或新功能。 這對於有現代CSS和JavaScript需求的團隊開發現代應用程式帶來了挑戰。
NReco.PdfGenerator的免費版本在生成的PDF上包含浮水印,生產使用需要商業授權。 獲取這些授權的價格需要聯繫銷售部門,這可能使採購過程變得複雜。
IronPDF簡介
IronPDF是一個.NET程式庫,使用現代的基於Chromium的渲染引擎將HTML、CSS和JavaScript轉換成PDF文件。 ChromePdfRenderer類作為主要的HTML轉PDF轉換介面,通過RenderingOptions屬性提供豐富的配置選項。
與wkhtmltopdf包裝器不同,IronPDF的渲染引擎定期接收安全和相容性的更新。 該程式庫是自包含的,無需在不同平台間管理外部二進位依賴。
IronPDF提供了一個功能齊全的試用期(無水印),允許團隊在購買前評估能力。 商業授權使用公佈的透明定價。
渲染引擎比較
這些程式庫之間的主要區別在於其渲染引擎,這會影響到安全性和CSS相容性。
| 方面 | NReco.PdfGenerator | IronPDF |
|---|---|---|
| 渲染引擎 | WebKit Qt (2012) | Chromium(當前) |
| 安全性 | 20多個CVE,已停用 | 活動安全更新 |
| CSS 支援 | CSS2.1,有限的CSS3 | 完整的CSS3,格柵,彈性盒 |
| JavaScript | 基本的ES5 | 完整的ES6+ |
| 相依性 | 外部wkhtmltopdf二進位 | 自包含 |
| Async支持 | 僅同步 | 完全異步/等待 |
| Web字型 | 有限 | 完整的Google字體,@font-face |
| 免費試用 | 帶水印 | 完整功能 |
| 定價透明 | 不透明,聯繫銷售 | 公布的定價 |
NReco.PdfGenerator繼承了wkhtmltopdf的所有安全漏洞,包括已記錄的伺服器端請求偽造、本地文件讀取漏洞以及潛在的遠程代碼執行。 由於wkhtmltopdf自2020年以來被棄用,這些問題沒有修補程式可用。
IronPDF的Chromium引擎提供了當前的Web標準支持,支持現代CSS功能,如格柵和彈性盒佈局、CSS變量和自定義屬性。 JavaScript執行支持包括async/await模式的ES6+語法。
基本 HTML 到 PDF 轉換
兩個程式庫都能夠處理基本的HTML轉PDF轉換,儘管使用不同的API模式。
NReco.PdfGenerator方法:
// NuGet: Install-Package NReco.PdfGenerator
using NReco.PdfGenerator;
using System.IO;
class Program
{
static void Main()
{
var htmlToPdf = new HtmlToPdfConverter();
var htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>";
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
File.WriteAllBytes("output.pdf", pdfBytes);
}
}// NuGet: Install-Package NReco.PdfGenerator
using NReco.PdfGenerator;
using System.IO;
class Program
{
static void Main()
{
var htmlToPdf = new HtmlToPdfConverter();
var htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>";
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
File.WriteAllBytes("output.pdf", pdfBytes);
}
}Imports NReco.PdfGenerator
Imports System.IO
Class Program
Shared Sub Main()
Dim htmlToPdf = New HtmlToPdfConverter()
Dim htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>"
Dim pdfBytes = htmlToPdf.GeneratePdf(htmlContent)
File.WriteAllBytes("output.pdf", pdfBytes)
End Sub
End ClassIronPDF方法:
// NuGet: Install-Package IronPdf
using IronPdf;
using System.IO;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using System.IO;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("output.pdf");
}
}Imports IronPdf
Imports System.IO
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
Dim htmlContent = "<html><body><h1>Hello World</h1><p>This is a PDF document.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("output.pdf")
End Sub
End Class兩個程式庫都需要最少的代碼來進行基本轉換。 NReco.PdfGenerator返回一個SaveAs()。 IronPDF對象還提供對Stream以進行基於流的操作。
對於熟悉HTML到PDF轉換流程的開發者來說,IronPDF的API會感覺直觀,同時在基本轉換之外提供額外的功能。
URL到PDF轉換
將網頁轉換為PDF文件揭示了方法命名和語義上的API差異。
NReco.PdfGenerator URL轉換:
// NuGet: Install-Package NReco.PdfGenerator
using NReco.PdfGenerator;
using System.IO;
class Program
{
static void Main()
{
var htmlToPdf = new HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdfFromFile("https://www.example.com", null);
File.WriteAllBytes("webpage.pdf", pdfBytes);
}
}// NuGet: Install-Package NReco.PdfGenerator
using NReco.PdfGenerator;
using System.IO;
class Program
{
static void Main()
{
var htmlToPdf = new HtmlToPdfConverter();
var pdfBytes = htmlToPdf.GeneratePdfFromFile("https://www.example.com", null);
File.WriteAllBytes("webpage.pdf", pdfBytes);
}
}Imports NReco.PdfGenerator
Imports System.IO
Class Program
Shared Sub Main()
Dim htmlToPdf As New HtmlToPdfConverter()
Dim pdfBytes As Byte() = htmlToPdf.GeneratePdfFromFile("https://www.example.com", Nothing)
File.WriteAllBytes("webpage.pdf", pdfBytes)
End Sub
End ClassIronPDF URL轉換:
// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
var pdf = renderer.RenderUrlAsPdf("https://www.example.com");
pdf.SaveAs("webpage.pdf");
}
}Imports IronPdf
Class Program
Shared Sub Main()
Dim renderer As New ChromePdfRenderer()
Dim pdf = renderer.RenderUrlAsPdf("https://www.example.com")
pdf.SaveAs("webpage.pdf")
End Sub
End ClassNReco.PdfGenerator使用GeneratePdfFromFile()處理文件路徑和URL,這可能在語義上造成混淆。 IronPDF提供了一個專用的RenderUrlAsPdf方法,明確表示正在執行的操作。
自定義頁面大小和邊距
專業文件通常需要具體的頁面尺寸和邊距配置。 這兩個程式庫都支持這些自定義配置,但使用不同的配置模式。
NReco.PdfGenerator頁面配置:
// NuGet: Install-Package NReco.PdfGenerator
using NReco.PdfGenerator;
using System.IO;
class Program
{
static void Main()
{
var htmlToPdf = new HtmlToPdfConverter();
htmlToPdf.PageWidth = 210;
htmlToPdf.PageHeight = 297;
htmlToPdf.Margins = new PageMargins { Top = 10, Bottom = 10, Left = 10, Right = 10 };
var htmlContent = "<html><body><h1>Custom Page Size</h1><p>A4 size document with margins.</p></body></html>";
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
File.WriteAllBytes("custom-size.pdf", pdfBytes);
}
}// NuGet: Install-Package NReco.PdfGenerator
using NReco.PdfGenerator;
using System.IO;
class Program
{
static void Main()
{
var htmlToPdf = new HtmlToPdfConverter();
htmlToPdf.PageWidth = 210;
htmlToPdf.PageHeight = 297;
htmlToPdf.Margins = new PageMargins { Top = 10, Bottom = 10, Left = 10, Right = 10 };
var htmlContent = "<html><body><h1>Custom Page Size</h1><p>A4 size document with margins.</p></body></html>";
var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);
File.WriteAllBytes("custom-size.pdf", pdfBytes);
}
}Imports NReco.PdfGenerator
Imports System.IO
Class Program
Shared Sub Main()
Dim htmlToPdf = New HtmlToPdfConverter()
htmlToPdf.PageWidth = 210
htmlToPdf.PageHeight = 297
htmlToPdf.Margins = New PageMargins With {.Top = 10, .Bottom = 10, .Left = 10, .Right = 10}
Dim htmlContent = "<html><body><h1>Custom Page Size</h1><p>A4 size document with margins.</p></body></html>"
Dim pdfBytes = htmlToPdf.GeneratePdf(htmlContent)
File.WriteAllBytes("custom-size.pdf", pdfBytes)
End Sub
End ClassIronPDF頁面配置:
// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.MarginLeft = 10;
renderer.RenderingOptions.MarginRight = 10;
var htmlContent = "<html><body><h1>Custom Page Size</h1><p>A4 size document with margins.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("custom-size.pdf");
}
}// NuGet: Install-Package IronPdf
using IronPdf;
using IronPdf.Rendering;
class Program
{
static void Main()
{
var renderer = new ChromePdfRenderer();
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4;
renderer.RenderingOptions.MarginTop = 10;
renderer.RenderingOptions.MarginBottom = 10;
renderer.RenderingOptions.MarginLeft = 10;
renderer.RenderingOptions.MarginRight = 10;
var htmlContent = "<html><body><h1>Custom Page Size</h1><p>A4 size document with margins.</p></body></html>";
var pdf = renderer.RenderHtmlAsPdf(htmlContent);
pdf.SaveAs("custom-size.pdf");
}
}Imports IronPdf
Imports IronPdf.Rendering
Class Program
Shared Sub Main()
Dim renderer = New ChromePdfRenderer()
renderer.RenderingOptions.PaperSize = PdfPaperSize.A4
renderer.RenderingOptions.MarginTop = 10
renderer.RenderingOptions.MarginBottom = 10
renderer.RenderingOptions.MarginLeft = 10
renderer.RenderingOptions.MarginRight = 10
Dim htmlContent = "<html><body><h1>Custom Page Size</h1><p>A4 size document with margins.</p></body></html>"
Dim pdf = renderer.RenderHtmlAsPdf(htmlContent)
pdf.SaveAs("custom-size.pdf")
End Sub
End ClassNReco.PdfGenerator需要用毫米指定頁面尺寸(A4為210×297)並使用PageMargins對象。 IronPDF使用RenderingOptions提供獨立的邊距屬性。 RenderingOptions類將所有頁面配置集成在一起,使配置可通過IDE自動補全發現。
API對映參考
對於考慮從NReco.PdfGenerator遷移到IronPDF的團隊而言,理解API對映有助於估計工作量和計劃轉換。
核心方法對映
| NReco.PdfGenerator | IronPDF |
|---|---|
new HtmlToPdfConverter() | new ChromePdfRenderer() |
GeneratePdf(html) | RenderHtmlAsPdf(html) |
GeneratePdfFromFile(url, output) | RenderUrlAsPdf(url) |
GeneratePdfFromFile(path, output) | RenderHtmlFileAsPdf(path) |
| (不支持) | RenderHtmlAsPdfAsync(html) |
配置屬性對映
| NReco.PdfGenerator | IronPDF |
|---|---|
Orientation = PageOrientation.Landscape | RenderingOptions.PaperOrientation = PdfPaperOrientation.Landscape |
Size = PageSize.A4 | RenderingOptions.PaperSize = PdfPaperSize.A4 |
Margins.Top = 10 | RenderingOptions.MarginTop = 10 |
Zoom = 0.9f | RenderingOptions.Zoom = 90 |
PageHeaderHtml = "..." | RenderingOptions.HtmlHeader |
PageFooterHtml = "..." | RenderingOptions.HtmlFooter |
佔位符語法差異
頁眉和頁尾經常包含動態內容,如頁碼。 兩個程式庫的佔位符語法有所不同:
| NReco.PdfGenerator | IronPDF | 目的 |
|---|---|---|
[page] | {page} | 當前頁碼 |
[topage] | {total-pages} | 總頁數 |
[date] | {date} | 當前日期 |
[time] | {time} | 當前時間 |
[title] | {html-title} | 文件標題 |
[webpage] | {url} | 來源URL |
從NReco.PdfGenerator遷移的團隊需要用新的佔位符語法更新所有頁眉和頁尾模板。
縮放值轉換
NReco.PdfGenerator使用浮點值(0.0-2.0)進行縮放,而IronPDF使用百分值:
// NReco: Zoom = 0.75f means 75%
// IronPDF: Zoom = 75 means 75%
int ironPdfZoom = (int)(nrecoZoom * 100);// NReco: Zoom = 0.75f means 75%
// IronPDF: Zoom = 75 means 75%
int ironPdfZoom = (int)(nrecoZoom * 100);' NReco: Zoom = 0.75F means 75%
' IronPDF: Zoom = 75 means 75%
Dim ironPdfZoom As Integer = CInt(nrecoZoom * 100)異步支援比較
現代Web應用從不阻塞線程的異步操作中受益。 這在處理並發請求的ASP.NET Core應用中特別重要。
NReco.PdfGenerator僅提供同步操作,在生成PDF時可能會阻塞Web伺服器線程:
// NReco.PdfGenerator - synchronous only
var pdfBytes = converter.GeneratePdf(html); // Blocks thread// NReco.PdfGenerator - synchronous only
var pdfBytes = converter.GeneratePdf(html); // Blocks threadIronPDF全面支持async/await模式:
//IronPDF- async support
var pdf = await renderer.RenderHtmlAsPdfAsync(html); // Non-blocking
await pdf.SaveAsAsync("output.pdf");//IronPDF- async support
var pdf = await renderer.RenderHtmlAsPdfAsync(html); // Non-blocking
await pdf.SaveAsAsync("output.pdf");Imports System.Threading.Tasks
' IronPDF - async support
Dim pdf = Await renderer.RenderHtmlAsPdfAsync(html) ' Non-blocking
Await pdf.SaveAsAsync("output.pdf")對於響應HTTP請求而生成PDF的應用而言,IronPDF的異步支持通過在渲染過程中釋放線程來提高可擴展性。
依賴項和部署差異
NReco.PdfGenerator需要在系統中提供wkhtmltopdf二進位。 這帶來了部署難題:
- 必須管理特定平台的二進位(Windows .exe,Linux .so,macOS .dylib)
- Docker映像需要安裝wkhtmltopdf
- CI/CD管道需要二進位配置步驟
- 安全掃描器標記已知的CVE
IronPDF本身是作為NuGet包的自包含:
#IronPDFinstallation - complete
dotnet add package IronPdf
# NReco.PdfGenerator installation - plus binary management
dotnet add package NReco.PdfGenerator
# Plus: install wkhtmltopdf per platform#IronPDFinstallation - complete
dotnet add package IronPdf
# NReco.PdfGenerator installation - plus binary management
dotnet add package NReco.PdfGenerator
# Plus: install wkhtmltopdf per platform從NReco.PdfGenerator遷移的團隊可以移除wkhtmltopdf二進位,簡化其部署過程。
團隊考慮從NReco.PdfGenerator轉向IronPDF時
有幾個因素促使團隊將IronPDF作為NReco.PdfGenerator的替代品:
安全合規:當漏洞掃描器識別出wkhtmltopdf的CVE時,具有安全需求的組織將面臨挑戰。 由於wkhtmltopdf被棄用,這些漏洞在NReco.PdfGenerator中沒有修復途徑。 IronPDF的Chromium引擎活躍維護,接收安全更新。
現代CSS需求:需要CSS Grid、Flexbox、CSS變量或其他現代CSS功能的項目無法使用wkhtmltopdf的2012年代WebKit引擎。網頁設計師經常製作在NReco.PdfGenerator中錯位或根本不渲染的佈局。
JavaScript相容性:需要使用JavaScript渲染內容(圖表、動態表格、計算值)生成PDF的應用需要現代JavaScript支持。 wkhtmltopdf的ES5限制阻止了使用當代JavaScript庫。
異步應用架構:ASP.NET Core應用從異步PDF生成中受益,以維持線程效率。 NReco.PdfGenerator的同步API可能會造成可縮放性瓶頸。
簡化部署:跨開發、測試和生產環境管理特定平台的wkhtmltopdf二進位增加了操作複雜性。 團隊尋求自包含的解決方案以簡化CI/CD管道。
定價清晰:預算規劃需要可預測的成本。 NReco.PdfGenerator的不透明定價(聯繫銷售)使採購複雜化,而IronPDF的公佈定價使預算安排變得簡單。
安裝比較
NReco.PdfGenerator安裝:
Install-Package NReco.PdfGeneratorInstall-Package NReco.PdfGenerator加上特定平台wkhtmltopdf二進位安裝和管理。
IronPDF 安裝:
Install-Package IronPdfInstall-Package IronPdfIronPDF 在應用程式啟動時需要授權金鑰配置:
IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY";IronPdf.License.LicenseKey = "YOUR-LICENSE-KEY"兩個程式庫支持.NET Framework 4.6.2+和.NET Core/.NET 5+,使其可與針對.NET 10和C# 14的最新.NET開發相容。
效能考量
IronPDF的Chromium引擎在首次使用時具有初始化成本(大約1.5秒的Chromium啟動時間)。 隨後的渲染速度更快。 對於需要低延遲啟動的應用程序,在應用初始化時預熱渲染引擎可防止此延遲影響面向用戶的操作:
// Warm up at startup
new ChromePdfRenderer().RenderHtmlAsPdf("<html></html>");// Warm up at startup
new ChromePdfRenderer().RenderHtmlAsPdf("<html></html>");Dim renderer As New ChromePdfRenderer()
renderer.RenderHtmlAsPdf("<html></html>")NReco.PdfGenerator在啟動wkhtmltopdf進程時也有初始化開銷,此外還有外部進程管理的持續內存開銷。
做出決定
在NReco.PdfGenerator和IronPDF之間的選擇取決於您的具體需求:
考慮NReco.PdfGenerator如果:您已經有正確運作的基於wkhtmltopdf的工作流程,您的環境中可以接受安全掃描器的發現,您不需要現代的CSS或JavaScript功能,並且能夠管理特定平台的二進位部署。
考慮IronPDF如果:安全合規要求解決wkhtmltopdf的CVE,您的設計使用現代CSS功能如網格或彈性盒,您需要用於Web應用的async/await支持,您希望通過消除外部二進位來簡化部署,或您偏好透明的授權和定價。
對於在2025年和計劃於2026年建設現代.NET應用程式的團隊,IronPDF活躍維護的Chromium引擎為當前和未來的Web標準相容性提供了基礎。
開始使用IronPDF
要評估IronPDF是否滿足您的PDF生成需求:
- 安裝IronPDF NuGet包:
Install-Package IronPdf - 查看HTML 到 PDF 教學以了解基本轉換模式
- 探索URL to PDF轉換以捕捉網頁
- 配置專業文件的頁眉和頁腳
最終想法
NReco.PdfGenerator和IronPDF代表著不同時代的PDF生成技術。 NReco.PdfGenerator包裝了wkhtmltopdf的2012年代WebKit引擎,繼承了它熟悉的API、安全漏洞和渲染限制。 IronPDF使用了一個現代的Chromium引擎,具有活躍的維護、當前的Web標準支持以及異步能力。
對於關注安全合規、現代CSS/JavaScript需求或部署複雜性的團隊來說,IronPDF提供了一個向前的道路,無需棄用依賴的技術債務。 轉換需要更新API調用和佔位符語法,但消除了管理外部二進位的需求,並解決了已記錄的安全漏洞。
根據您對安全性、渲染保真度、異步支持和部署簡潔性的具體需求,評估這兩個選項。 理解在本比較中概述的架構差異將幫助您做出符合您的PDF生成需求和現代化目標的明智決定。
