如何解析PDF文件中的資料
將HTML轉換為PDF是許多軟體應用程式中的常見需求,例如生成報告、發票或將網頁保存為PDF。 在本文中,我們將探究三個流行的開源C#庫用於HTML到PDF的轉換,審查其優勢和限制,並討論為什麼IronPDF在許多情況下是更好的替代方案。
HTML到PDF轉換器C#開源
1. PuppeteerSharp

PuppeteerSharp是Puppeteer的.NET包裝,這是一個無頭的Chromium瀏覽器。 它使開發者能夠通過利用Chromium的渲染引擎將HTML文件轉換為PDF。
PuppeteerSharp提供對渲染過程的精確控制。 這裡是一個範例:
using PuppeteerSharp;
using System.Threading.Tasks;
class Program
{
static async Task Main(string[] args)
{
// Download Chromium to ensure compatibility with PuppeteerSharp
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
// Launch a headless instance of Chromium browser
using (var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }))
{
// Open a new browser page
var page = await browser.NewPageAsync();
// Set the HTML content for the page
await page.SetContentAsync("<html><body><h1>Hello, PuppeteerSharp!</h1></body></html>");
// Generate a PDF from the rendered HTML content
await page.PdfAsync("output.pdf");
Console.WriteLine("PDF Generated Successfully!");
}
}
}Imports PuppeteerSharp
Imports System.Threading.Tasks
Friend Class Program
Shared Async Function Main(ByVal args() As String) As Task
' Download Chromium to ensure compatibility with PuppeteerSharp
Await (New BrowserFetcher()).DownloadAsync(BrowserFetcher.DefaultChromiumRevision)
' Launch a headless instance of Chromium browser
Using browser = Await Puppeteer.LaunchAsync(New LaunchOptions With {.Headless = True})
' Open a new browser page
Dim page = Await browser.NewPageAsync()
' Set the HTML content for the page
Await page.SetContentAsync("<html><body><h1>Hello, PuppeteerSharp!</h1></body></html>")
' Generate a PDF from the rendered HTML content
Await page.PdfAsync("output.pdf")
Console.WriteLine("PDF Generated Successfully!")
End Using
End Function
End ClassImports PuppeteerSharp
Imports System.Threading.Tasks
Friend Class Program
Shared Async Function Main(ByVal args() As String) As Task
' Download Chromium to ensure compatibility with PuppeteerSharp
Await (New BrowserFetcher()).DownloadAsync(BrowserFetcher.DefaultChromiumRevision)
' Launch a headless instance of Chromium browser
Using browser = Await Puppeteer.LaunchAsync(New LaunchOptions With {.Headless = True})
' Open a new browser page
Dim page = Await browser.NewPageAsync()
' Set the HTML content for the page
Await page.SetContentAsync("<html><body><h1>Hello, PuppeteerSharp!</h1></body></html>")
' Generate a PDF from the rendered HTML content
Await page.PdfAsync("output.pdf")
Console.WriteLine("PDF Generated Successfully!")
End Using
End Function
End Class程式碼解釋
-
下載Chromium: PuppeteerSharp自動下載所需的Chromium版本以確保相容性。
-
啟動瀏覽器: 使用
Puppeteer.LaunchAsync()啟動Chromium的無頭實例。 -
設定HTML內容: 使用
page.SetContentAsync()將所需的HTML載入到瀏覽器頁面中。 -
生成PDF: 使用
page.PdfAsync()方法生成渲染內容的PDF。
結果是高品質的PDF(output.pdf),準確地複製了HTML的結構和設計。
優點
- 高保真渲染: 支援現代網頁技術,包括先進的CSS和JavaScript。
- 自動化能力: 除了PDF,PuppeteerSharp還可以自動化網頁瀏覽、測試和資料提取。
- 積極開發: PuppeteerSharp積極維護並定期更新。
缺點
- 文件大小大: 需要下載並捆綁Chromium瀏覽器,增加了部署大小。
- 資源密集: 運行瀏覽器實例可能會對系統資源造成負擔,特別是對於大規模應用程式。
- 受限的PDF特定功能: PuppeteerSharp側重於渲染而不是增強PDF(例如新增頁眉或頁腳)。
2. PDFSharp

PDFSharp是一個強大的開源庫,用於在C#中建立和操作PDF文件。 雖然它不直接支援HTML渲染,但它在提供工具生成和程式化編輯PDF文件方面表現優異。
PDFSharp的主要功能
-
PDF建立: PDFSharp允許開發者從頭開始生成新的PDF文件,定義頁面大小,新增文字、圖形、圖像等等。
-
操作現有PDF: 您可以修改現有的PDF文件,例如合併、拆分或提取內容。
-
繪圖能力: PDFSharp提供強大的圖形能力,可使用XGraphics類將自定義設計新增到PDF文件中。
-
輕量級: 它是輕量級庫,非常適合需要簡單性和速度為優先事項的專案。
using PdfSharp.Pdf;
using PdfSharp.Drawing;
using HtmlAgilityPack;
class Program
{
static void Main(string[] args)
{
// Example HTML content
string htmlContent = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>";
// Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument
{
Info = { Title = "HTML to PDF Example" }
};
// Add a new page to the document
PdfPage page = pdfDocument.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont titleFont = new XFont("Arial", 20, XFontStyle.Bold);
XFont textFont = new XFont("Arial", 12, XFontStyle.Regular);
// Draw the parsed HTML content
int yPosition = 50; // Starting Y position
foreach (var node in htmlDoc.DocumentNode.SelectNodes("//h1 | //p"))
{
if (node.Name == "h1")
{
gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
yPosition += 30; // Adjust spacing
}
else if (node.Name == "p")
{
gfx.DrawString(node.InnerText, textFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
yPosition += 20; // Adjust spacing
}
}
// Save the PDF document
string outputFilePath = "HtmlToPdf.pdf";
pdfDocument.Save(outputFilePath);
System.Console.WriteLine($"PDF file created: {outputFilePath}");
}
}using PdfSharp.Pdf;
using PdfSharp.Drawing;
using HtmlAgilityPack;
class Program
{
static void Main(string[] args)
{
// Example HTML content
string htmlContent = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>";
// Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(htmlContent);
// Create a new PDF document
PdfDocument pdfDocument = new PdfDocument
{
Info = { Title = "HTML to PDF Example" }
};
// Add a new page to the document
PdfPage page = pdfDocument.AddPage();
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont titleFont = new XFont("Arial", 20, XFontStyle.Bold);
XFont textFont = new XFont("Arial", 12, XFontStyle.Regular);
// Draw the parsed HTML content
int yPosition = 50; // Starting Y position
foreach (var node in htmlDoc.DocumentNode.SelectNodes("//h1 | //p"))
{
if (node.Name == "h1")
{
gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
yPosition += 30; // Adjust spacing
}
else if (node.Name == "p")
{
gfx.DrawString(node.InnerText, textFont, XBrushes.Black, new XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft);
yPosition += 20; // Adjust spacing
}
}
// Save the PDF document
string outputFilePath = "HtmlToPdf.pdf";
pdfDocument.Save(outputFilePath);
System.Console.WriteLine($"PDF file created: {outputFilePath}");
}
}Imports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports HtmlAgilityPack
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Example HTML content
Dim htmlContent As String = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>"
' Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
Dim htmlDoc = New HtmlDocument()
htmlDoc.LoadHtml(htmlContent)
' Create a new PDF document
Dim pdfDocument As New PdfDocument With {
.Info = { Title = "HTML to PDF Example" }
}
' Add a new page to the document
Dim page As PdfPage = pdfDocument.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim titleFont As New XFont("Arial", 20, XFontStyle.Bold)
Dim textFont As New XFont("Arial", 12, XFontStyle.Regular)
' Draw the parsed HTML content
Dim yPosition As Integer = 50 ' Starting Y position
For Each node In htmlDoc.DocumentNode.SelectNodes("//h1 | //p")
If node.Name = "h1" Then
gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, New XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft)
yPosition += 30 ' Adjust spacing
ElseIf node.Name = "p" Then
gfx.DrawString(node.InnerText, textFont, XBrushes.Black, New XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft)
yPosition += 20 ' Adjust spacing
End If
Next node
' Save the PDF document
Dim outputFilePath As String = "HtmlToPdf.pdf"
pdfDocument.Save(outputFilePath)
System.Console.WriteLine($"PDF file created: {outputFilePath}")
End Sub
End ClassImports PdfSharp.Pdf
Imports PdfSharp.Drawing
Imports HtmlAgilityPack
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Example HTML content
Dim htmlContent As String = "<html><body><h1>Hello, PdfSharp!</h1><p>This is an example of HTML to PDF.</p></body></html>"
' Parse HTML using HtmlAgilityPack (You need to add HtmlAgilityPack via NuGet)
Dim htmlDoc = New HtmlDocument()
htmlDoc.LoadHtml(htmlContent)
' Create a new PDF document
Dim pdfDocument As New PdfDocument With {
.Info = { Title = "HTML to PDF Example" }
}
' Add a new page to the document
Dim page As PdfPage = pdfDocument.AddPage()
Dim gfx As XGraphics = XGraphics.FromPdfPage(page)
Dim titleFont As New XFont("Arial", 20, XFontStyle.Bold)
Dim textFont As New XFont("Arial", 12, XFontStyle.Regular)
' Draw the parsed HTML content
Dim yPosition As Integer = 50 ' Starting Y position
For Each node In htmlDoc.DocumentNode.SelectNodes("//h1 | //p")
If node.Name = "h1" Then
gfx.DrawString(node.InnerText, titleFont, XBrushes.Black, New XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft)
yPosition += 30 ' Adjust spacing
ElseIf node.Name = "p" Then
gfx.DrawString(node.InnerText, textFont, XBrushes.Black, New XRect(50, yPosition, page.Width - 100, page.Height - 100), XStringFormats.TopLeft)
yPosition += 20 ' Adjust spacing
End If
Next node
' Save the PDF document
Dim outputFilePath As String = "HtmlToPdf.pdf"
pdfDocument.Save(outputFilePath)
System.Console.WriteLine($"PDF file created: {outputFilePath}")
End Sub
End Class程式碼解釋
-
HTML解析: 範例使用HtmlAgilityPack(一個解析和操作HTML的開源庫)從
<p>標籤提取文字內容。 -
繪製內容: 使用PDFSharp的XGraphics類將解析的HTML內容作為文字渲染在PDF頁面上。
-
限制: 這種方法適用於簡單的HTML結構,但無法處理複雜的佈局、樣式或JavaScript。
PDFSharp的優缺點
優點
- 輕量且易於使用: PDFSharp直觀明了,非常適合開始使用PDF生成的開發者。
- 開源和免費: 無需授權費,使用者可自定義源程式碼。
- 自定義繪圖: 提供從頭開始建立具有自定義設計的PDF的出色能力。
缺點
- 無HTML到PDF轉換: PDFSharp不支持原生從HTML渲染成PDF,這需要額外的庫進行HTML解析。
- 現代功能支援有限: 不提供如互動式PDF、數位簽章或批註等高級功能。
- 性能約束: 可能不如大規模或企業應用程式的專業庫優化。
3. Pdfium.NET SDK

Pdfium.NET是一個基於開源PDFium項目的綜合庫,設計用於在.NET應用程式中查看、編輯和操作PDF文件。 它為開發者提供強大的工具來建立、編輯和提取PDF中的內容,使其適用於廣泛的使用情況。 基本上是個免費的HTML到PDF轉換庫。
Pdfium.NET SDK的主要功能
-
PDF建立和編輯:
- 從頭生成PDF或從掃描的圖像中生成。
- 通過新增文字、圖像或批註編輯現有的PDF。
-
文字和圖像提取:
- 從PDF文件格式的文件中提取文字和圖像以進行進一步處理。
- 在PDF文件中搜索特定文字。
-
PDF查看器控件:
- 在WinForms或WPF應用程式中嵌入一個獨立的PDF查看器。
- 支持縮放、滾動、書籤和文字搜索。
-
相容性:
- 相容.NET Framework、.NET Core、.NET Standard和.NET 6+。
- 相容Windows和macOS平台。
-
高級功能:
- 合併和拆分PDF文件。
- 將PDF渲染為圖像顯示或列印。
using Pdfium.Net.SDK;
using System;
class Program
{
static void Main(string[] args)
{
// Initialize Pdfium.NET SDK functionalities
PdfCommon.Initialize();
// Create a new PDF document
PdfDocument pdfDocument = PdfDocument.CreateNew();
// Add a page to the document (A4 size in points: 8.27 x 11.69 inches)
var page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842);
// Sample HTML content to be parsed and rendered manually
var htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>";
// Example: Manually render text since Pdfium.NET doesn't render HTML directly
var font = PdfFont.CreateFont(pdfDocument, "Arial");
page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!");
page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.");
// Save the document to a file
string outputFilePath = "HtmlToPdfExample.pdf";
pdfDocument.Save(outputFilePath, SaveFlags.Default);
Console.WriteLine($"PDF created successfully: {outputFilePath}");
}
}using Pdfium.Net.SDK;
using System;
class Program
{
static void Main(string[] args)
{
// Initialize Pdfium.NET SDK functionalities
PdfCommon.Initialize();
// Create a new PDF document
PdfDocument pdfDocument = PdfDocument.CreateNew();
// Add a page to the document (A4 size in points: 8.27 x 11.69 inches)
var page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842);
// Sample HTML content to be parsed and rendered manually
var htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>";
// Example: Manually render text since Pdfium.NET doesn't render HTML directly
var font = PdfFont.CreateFont(pdfDocument, "Arial");
page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!");
page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.");
// Save the document to a file
string outputFilePath = "HtmlToPdfExample.pdf";
pdfDocument.Save(outputFilePath, SaveFlags.Default);
Console.WriteLine($"PDF created successfully: {outputFilePath}");
}
}Imports Pdfium.Net.SDK
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Initialize Pdfium.NET SDK functionalities
PdfCommon.Initialize()
' Create a new PDF document
Dim pdfDocument As PdfDocument = PdfDocument.CreateNew()
' Add a page to the document (A4 size in points: 8.27 x 11.69 inches)
Dim page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842)
' Sample HTML content to be parsed and rendered manually
Dim htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>"
' Example: Manually render text since Pdfium.NET doesn't render HTML directly
Dim font = PdfFont.CreateFont(pdfDocument, "Arial")
page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!")
page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.")
' Save the document to a file
Dim outputFilePath As String = "HtmlToPdfExample.pdf"
pdfDocument.Save(outputFilePath, SaveFlags.Default)
Console.WriteLine($"PDF created successfully: {outputFilePath}")
End Sub
End ClassImports Pdfium.Net.SDK
Imports System
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Initialize Pdfium.NET SDK functionalities
PdfCommon.Initialize()
' Create a new PDF document
Dim pdfDocument As PdfDocument = PdfDocument.CreateNew()
' Add a page to the document (A4 size in points: 8.27 x 11.69 inches)
Dim page = pdfDocument.Pages.InsertPageAt(pdfDocument.Pages.Count, 595, 842)
' Sample HTML content to be parsed and rendered manually
Dim htmlContent = "<h1>Hello, Pdfium.NET SDK!</h1><p>This is an example of HTML to PDF.</p>"
' Example: Manually render text since Pdfium.NET doesn't render HTML directly
Dim font = PdfFont.CreateFont(pdfDocument, "Arial")
page.AddText(72, 750, font, 20, "Hello, Pdfium.NET SDK!")
page.AddText(72, 700, font, 14, "This is an example of HTML to PDF.")
' Save the document to a file
Dim outputFilePath As String = "HtmlToPdfExample.pdf"
pdfDocument.Save(outputFilePath, SaveFlags.Default)
Console.WriteLine($"PDF created successfully: {outputFilePath}")
End Sub
End Class程式碼解釋
-
SDK初始化:
PdfCommon.Initialize()方法初始化Pdfium.NET功能。 -
建立PDF: 使用
PdfDocument.CreateNew()建立新的PDF文件。 -
新增頁面: 按指定尺寸(例如A4尺寸)將頁面插入PDF。
-
渲染HTML內容: Pdfium.NET SDK不支持原生HTML渲染,您需要手動解析並將HTML元素渲染為文字、形狀或圖像。
-
保存PDF: 使用
Save()方法將文件保存到文件路徑。
優點
- 允許完全控制PDF建立和編輯。
- 靈活畫圖和新增文字、圖像和形狀。
- 在桌面應用程式中查看和操作PDF的強大功能。
缺點
- 不直接轉換HTML到PDF。
- 手動解析和渲染HTML可能很復雜且耗時。
- 最適合專注於PDF編輯和操作而非HTML轉換的應用程式。
介紹IronPDF

IronPDF是一個專業級的庫,專為.NET開發者設計,能夠輕鬆地將HTML內容轉換為高質量的PDF。 IronPDF以其可靠性、先進功能和易用性而聞名,簡化了開發過程,同時提供精確渲染和強大功能。 IronPDF成為一個引人注目的解決方案的原因如下:
主要功能
-
直接HTML到PDF轉換: 使用IronPDF直接建立PDF文件,包含HTML內容,包括CSS和JavaScript,轉換成完整格式化的PDF。 只需要幾行程式碼,開發者就能夠從網頁、原始HTML字串或本地HTML文件生成PDF。
-
現代渲染功能: 支持最新的網頁標準,IronPDF確保準確渲染複雜的佈局、樣式和互動元素,將HTML頁面轉換為PDF。
-
先進的PDF功能: IronPDF提供廣泛的自定義選項,例如新增頁眉、頁腳、水印、註釋和書籤。 它還支持合併、拆分和編輯現有PDF。
-
性能和可擴展性: 為小規模項目和企業環境進行了優化,IronPDF提供快速可靠的性能,適用於各種規模的項目。
-
易於整合: 專為.NET Framework和.NET Core設計,IronPDF與C#應用程式流暢整合,為開發者提供簡單的設定過程和全面的文件。
為什麼選擇IronPDF?
IronPDF與其他解決方案相比,因其功能組合、開發者支持和性能而脫穎而出。 與通常需要大量配置或外部依賴的開源替代方案不同,IronPDF是一個內嵌的解決方案,簡化開發過程而不犧牲功能。 無論是生成發票、報告還是存檔網路內容,IronPDF都能為開發者提供即時且高效地實現專業級結果所需的工具。
IronPDF是選擇重視可靠性、可擴展性和易用性的開發者的實用選擇,適用於HTML到PDF的工作流。
如何使用IronPDF將HTML轉換為PDF
using IronPdf;
class Program
{
static void Main()
{
// Specify license key
IronPdf.License.LicenseKey = "Your Key";
// Create a new HtmlToPdf object using ChromePdfRenderer
var Renderer = new ChromePdfRenderer();
// Define the HTML string to be converted
string htmlContent = "<html><body><h1>IronPDF: Better than Open source</h1></body></html>";
// Convert the HTML string to a PDF document
var document = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a file
document.SaveAs("html2Pdf.pdf");
Console.WriteLine("PDF generated and saved successfully!");
}
}using IronPdf;
class Program
{
static void Main()
{
// Specify license key
IronPdf.License.LicenseKey = "Your Key";
// Create a new HtmlToPdf object using ChromePdfRenderer
var Renderer = new ChromePdfRenderer();
// Define the HTML string to be converted
string htmlContent = "<html><body><h1>IronPDF: Better than Open source</h1></body></html>";
// Convert the HTML string to a PDF document
var document = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a file
document.SaveAs("html2Pdf.pdf");
Console.WriteLine("PDF generated and saved successfully!");
}
}Imports IronPdf
Friend Class Program
Shared Sub Main()
' Specify license key
IronPdf.License.LicenseKey = "Your Key"
' Create a new HtmlToPdf object using ChromePdfRenderer
Dim Renderer = New ChromePdfRenderer()
' Define the HTML string to be converted
Dim htmlContent As String = "<html><body><h1>IronPDF: Better than Open source</h1></body></html>"
' Convert the HTML string to a PDF document
Dim document = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF document to a file
document.SaveAs("html2Pdf.pdf")
Console.WriteLine("PDF generated and saved successfully!")
End Sub
End ClassImports IronPdf
Friend Class Program
Shared Sub Main()
' Specify license key
IronPdf.License.LicenseKey = "Your Key"
' Create a new HtmlToPdf object using ChromePdfRenderer
Dim Renderer = New ChromePdfRenderer()
' Define the HTML string to be converted
Dim htmlContent As String = "<html><body><h1>IronPDF: Better than Open source</h1></body></html>"
' Convert the HTML string to a PDF document
Dim document = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF document to a file
document.SaveAs("html2Pdf.pdf")
Console.WriteLine("PDF generated and saved successfully!")
End Sub
End Class程式碼片段解釋
-
授權金鑰設置: 該程式從設定IronPDF授權金鑰開始,這是解鎖庫的全部功能所需的。
-
建立渲染器: 初始化
ChromePdfRenderer的實例。 此元件負責將HTML內容轉換為PDF文件,充當原始HTML和最終輸出之間的橋樑。 -
定義HTML内容: 建立一個字串變數
htmlContent,用於儲存將轉換為PDF的HTML結構。 在這個範例中,它包含一個簡單的標題。 -
將HTML轉換為PDF: 在
RenderHtmlAsPdf()方法,將HTML字串作為輸入傳遞。 此功能處理內容並將其轉換為PDF文件。 -
保存PDF: 最後,生成的PDF通過
SaveAs()方法保存為名為"html2Pdf.pdf"的文件,保存到磁片上以便未來存取。
輸出PDF

授權資訊(可試用)
IronPDF需要有效的授權金鑰才能全功能運行。 您可以從官方網站獲得一個試用授權。在使用IronPDF程式庫之前,請按如下方式設定授權金鑰:
IronPdf.License.LicenseKey = "your key";IronPdf.License.LicenseKey = "your key";IronPdf.License.LicenseKey = "your key"IronPdf.License.LicenseKey = "your key"這確保了該程式庫的無限制運行。
總結
PuppeteerSharp對於需要精確渲染HTML為PDF的開發者來說是非常好的選擇,尤其是在處理複雜的網頁時。 然而,對於需要高級PDF特定功能、性能優化和易於整合的應用程式,像IronPDF這樣的專業工具往往是更好的選擇。
PDFSharp是用於輕量級、程式化的PDF建立和操作的良好選擇,特別是對於要求簡單的項目。 然而,如果您的應用程式需要將HTML轉換為PDF或高級PDF功能,IronPDF提供更高效且功能豐富的解決方案。
雖然Pdfium.NET SDK是PDF操作的強大工具,IronPDF提供原生支持直接將HTML轉換為PDF,包括渲染現代HTML、CSS和JavaScript。 IronPDF通過內建方法如HtmlToPdf.RenderHtmlAsPdf()簡化工作流,使開發者能更快更高效。
無論是生成發票、報告還是存檔網路內容,IronPDF都能為開發者提供即時且高效地實現專業級結果所需的工具。
IronPDF是選擇重視可靠性、可擴展性和易用性的開發者的實用選擇,適用於HTML到PDF的工作流。
Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。