跳過到頁腳內容
使用IRONPDF

HTML到PDF轉換器C#開源(.NET 庫比較)

將 HTML 轉換為 PDF 是許多軟體應用程式中的常見需求,例如產生報告、發票或將網頁儲存為 PDF。 在本文中,我們將探討三個流行的用於在 C# 中進行 HTML 到 PDF 轉換的開源程式庫,回顧它們的優勢和局限性,並討論為什麼IronPDF在許多情況下是一個更好的選擇。

HTML 轉 PDF 轉換器 C# 開源

1. PuppeteerSharp

! HTML 轉 PDF 轉換器 C# 開源版(.NET 函式庫比較):圖 1

PuppeteerSharp是 .NET 封裝器,用於封裝 Puppeteer,Puppeteer 是一個無頭 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!");
        }
    }
}
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 Class
$vbLabelText   $csharpLabel

程式碼解釋

1.下載 Chromium: PuppeteerSharp 會自動下載所需的 Chromium 版本以確保相容性。

2.啟動瀏覽器:使用Puppeteer.LaunchAsync()啟動 Chromium 的無頭實例。

3.設定 HTML 內容:使用page.SetContentAsync()將所需的 HTML 載入到瀏覽器頁面中。

4.產生 PDF:使用page.PdfAsync()方法產生渲染內容的 PDF。

最終產生的是一個高品質的 PDF 檔案( output.pdf ),它準確地複製了 HTML 結構和設計。

優點

-高保真渲染:支援現代網路技術,包括進階 CSS 和 JavaScript。 -自動化功能:除了 PDF 之外,PuppeteerSharp 還可以自動執行網頁瀏覽、測試和資料提取。 -積極開發: PuppeteerSharp 處於積極維護和定期更新狀態。

缺點

-檔案大小較大:需要下載和捆綁 Chromium 瀏覽器,增加部署大小。 -資源密集:執行瀏覽器執行個體會佔用大量系統資源,尤其是對於大型應用程式而言。

  • PDF 特定功能有限: PuppeteerSharp 專注於渲染而不是增強 PDF(例如,添加頁首或頁腳)。

2. PdfSharp

! HTML 轉 PDF 轉換器 C# 開源版(.NET 函式庫比較):圖 2

PdfSharp是一個功能強大的開源程式庫,用於在 C# 中建立和操作 PDF 檔案。 雖然它不直接支援 HTML 渲染,但它非常擅長為開發人員提供以程式設計方式產生和編輯 PDF 文件的工具。

PdfSharp 的主要特點

  1. PDF 建立: PdfSharp 允許開發人員透過定義頁面大小、新增文字、形狀、影像等,從頭開始產生新的 PDF 檔案。

2.修改現有 PDF:您可以修改現有的 PDF 文檔,例如合併、分割或擷取內容。

3.繪圖功能: PdfSharp 提供強大的圖形功能,可使用 XGraphics 類別為 PDF 檔案新增自訂設計。

4.輕量級:它是一個輕量級的庫,非常適合以簡單性和速度為優先的項目。

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 Class
$vbLabelText   $csharpLabel

程式碼解釋

  1. HTML 解析:此範例使用HtmlAgilityPack (一個用於解析和操作 HTML 的開源函式庫)從中擷取文字內容。<h1><p>標籤。

2.繪製內容: PdfSharp 的 XGraphics 類別用於將解析的 HTML 內容渲染為 PDF 頁面上的文字。

3.限制:這種方法適用於簡單的 HTML 結構,但無法處理複雜的佈局、樣式或 JavaScript。

PdfSharp 的優缺點

優點

-輕巧易用: PdfSharp 直覺易用,是 PDF 生成入門開發人員的理想選擇。 -開源且免費:無需支付許可費,原始碼可供自訂。 -自訂繪圖:提供從零開始建立具有自訂設計的 PDF 的出色功能。

缺點

-不支援 HTML 轉 PDF 轉換: PdfSharp 本身不支援將 HTML 渲染為 PDF,需要額外的函式庫來解析 HTML。 -對現代功能的支援有限:不提供互動式 PDF、數位簽章或註解等高級功能。 -效能限制:可能不如針對大型或企業應用程式的專業函式庫那樣最佳化。

3. Pdfium.NET SDK

! HTML 轉 PDF 轉換器 C# 開源版(.NET 函式庫比較):圖 3

Pdfium.NET是一個基於開源 PDFium 專案的綜合庫,旨在用於在 .NET 應用程式中檢視、編輯和操作 PDF 檔案。 它為開發人員提供了強大的工具,用於建立、編輯和提取 PDF 內容,使其適用於各種使用情境。 它本質上是一個免費的HTML轉PDF轉換器庫。

Pdfium.NET SDK 的主要特性

  1. PDF 建立和編輯:
    • 從頭開始或從掃描影像產生 PDF。
    • 透過新增文字、圖像或註釋來編輯現有 PDF。

2.文字和圖像提取:

  • 從PDF文件格式文件中提取文字和圖像,以便進行進一步處理。
  • 在 PDF 文件中搜尋特定文字。
  1. PDF 檢視器控制:
    • 在 WinForms 或 WPF 應用程式中嵌入獨立的 PDF 檢視器。
    • 支援縮放、滾動、書籤和文字搜尋。

4.相容性:

  • 可與 .NET Framework、.NET Core、.NET Standard 和 .NET 6+ 搭配使用。
  • 相容於 Windows 和 macOS 平台。

5.進階功能: 合併和拆分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 Class
$vbLabelText   $csharpLabel

程式碼解釋

  1. SDK 初始化: PdfCommon.Initialize()方法初始化 Pdfium.NET 功能。

2.建立 PDF:使用PdfDocument.CreateNew()建立新的 PDF 文件。

3.新增頁面:將頁面以指定尺寸(例如 A4 尺寸)插入 PDF 中。

4.渲染 HTML 內容:由於 Pdfium.NET SDK 本身不支援 HTML 渲染,因此需要手動解析 HTML 元素並將其渲染為文字、形狀或圖像。

5.儲存 PDF:使用Save()方法將文件儲存到文件路徑。

優點

  • 允許完全控制 PDF 的建立和編輯。
  • 可靈活繪製和添加文字、圖像和形狀。
  • 強大的桌面應用程式檢視和操作 PDF 的功能。

缺點

  • 不直接將 HTML 轉換為 PDF。 手動解析和渲染 HTML 可能既複雜又耗時。
  • 最適合專注於 PDF 編輯和操作而非 HTML 轉換的應用。

介紹 IronPDF。

! HTML 轉 PDF 轉換器 C# 開源版(.NET 函式庫比較):圖 4

IronPDF是一個專業級庫,專為 .NET 開發人員設計,可輕鬆將 HTML 內容轉換為高品質的 PDF。 IronPDF 以其可靠性、高級功能和易用性而聞名,它簡化了開發流程,同時提供了精確的渲染和強大的功能。 以下是IronPDF成為一款傑出解決方案的原因:

主要功能

1.直接將 HTML 轉換為 PDF:使用 IronPDF 直接將 HTML 內容(包括 CSS 和 JavaScript)建立為完全格式化的 PDF 文件。 只需幾行程式碼,開發人員即可從網頁、原始 HTML 字串或本機 HTML 檔案產生 PDF。

2.現代渲染功能: IronPDF 支援最新的 Web 標準,可確保準確渲染複雜的佈局、樣式和互動元素,從而將 HTML 頁面轉換為 PDF。

3.進階 PDF 功能: IronPDF 提供豐富的自訂選項,例如新增頁首、頁尾、浮水印、註解和書籤。 它還支援合併、拆分和編輯現有PDF文件。

4.效能和可擴展性: IronPDF 針對小規模應用程式和企業環境進行了最佳化,可為任何規模的專案提供快速、可靠的效能。

5.易於整合: IronPDF 專為 .NET Framework 和 .NET Core 設計,可與 C# 應用程式無縫集成,為開發人員提供簡單的設定流程和全面的文件。

為什麼選擇 IronPDF?

IronPDF憑藉其豐富的功能、完善的開發者支援和卓越的性能,在眾多解決方案中脫穎而出。 與通常需要大量配置或外部相依性的開源替代方案不同,IronPDF 是一個獨立的解決方案,它簡化了開發過程,同時又不犧牲功能。 無論是產生發票、報告或存檔 Web 內容,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 Class
$vbLabelText   $csharpLabel

程式碼片段解釋

1.許可證密鑰設定:程式首先設定IronPDF 許可證密鑰,這是解鎖庫的全部功能所必需的。

2.建立渲染器:初始化ChromePdfRenderer實例。 此元件負責將 HTML 內容轉換為PDF 文檔,作為原始 HTML 和最終輸出之間的橋樑。

3.定義 HTML 內容:建立字串變數htmlContent來儲存將要轉換為 PDF 的HTML 結構。 在這個例子中,它包含一個簡單的標題。

4.將 HTML 轉換為 PDF :呼叫ChromePdfRenderer實例的RenderHtmlAsPdf()方法,並將 HTML 字串傳遞為輸入。 此功能處理內容並將其轉換為PDF 文件

5.儲存 PDF :最後,使用SaveAs()方法將產生的 PDF 儲存到名為"html2Pdf.pdf"的檔案中,並將其儲存在磁碟上以便將來存取。

輸出 PDF

! HTML 轉 PDF 轉換器 C# 開源版(.NET 函式庫比較):圖 5

授權資訊(提供試用版)

IronPDF 需要有效的許可證金鑰才能使用全部功能。 您可以從官方網站取得試用許可證。使用 IronPDF 庫之前,請如下設定許可證密鑰:

IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
$vbLabelText   $csharpLabel

這樣可以確保圖書館不受限制地運作。

結論

對於需要將 HTML 精確渲染為 PDF 的開發人員來說,PuppeteerSharp 是一個絕佳的選擇,尤其是在處理複雜的網頁時。 然而,對於需要進階 PDF 特定功能、效能最佳化和易於整合的應用來說,像IronPDF這樣的專業工具通常是更好的選擇。

PdfSharp 是一個輕量級、程序化 PDF 創建和操作的絕佳選擇,尤其適合需求簡單的專案。 但是,如果您的應用程式需要將 HTML 轉換為 PDF 或使用進階 PDF 功能, IronPDF可以提供更有效率、更豐富的解決方案。

雖然 Pdfium.NET SDK 是一個強大的 PDF 操作工具,但IronPDF提供了對直接 HTML 到 PDF 轉換的原生支持,包括渲染現代 HTML、CSS 和 JavaScript。 IronPDF 透過內建方法(例如HtmlToPdf.RenderHtmlAsPdf()簡化了工作流程,使開發人員的工作速度更快、更有效率。

無論是產生發票、報告或存檔 Web 內容,IronPDF 都能為開發人員提供所需的工具,幫助他們快速且有效率地獲得專業級的結果。

IronPDF是重視 HTML 轉 PDF 工作流程中的可靠性、可擴充性和易用性的開發人員的實用選擇。

常見問題解答

如何在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字串轉換為 PDF。此外,IronPDF 還支援使用 RenderHtmlFileAsPdf 方法直接將 HTML 檔案轉換為 PDF。

使用 IronPDF 進行 PDF 轉換相較於開放原始碼程式庫有何優勢?

IronPDF 提供 HTML 至 PDF 的直接轉換,支援現代網路標準、先進的 PDF 功能,並可輕鬆與 .NET 應用程式整合。與 PuppeteerSharp、PdfSharp 和 Pdfium.NET SDK 等開放原始碼替代品相比,它提供了專業的解決方案。

IronPDF 能否在 PDF 轉換過程中處理複雜的 HTML、CSS 和 JavaScript?

是的,IronPDF 支援最新的網頁標準,可確保在 HTML 轉換為 PDF 的過程中準確呈現複雜的版面、樣式和互動元素。

使用 IronPDF 進行 HTML 至 PDF 的轉換需要哪些條件?

要使用 IronPDF,需要有效的許可金鑰。開發人員可從官方網站取得試用授權,以解鎖完整功能。

是什麼讓 IronPDF 成為開發人員的實用選擇?

IronPDF 因其可靠性、可擴展性、易用性以及強大的 HTML 至 PDF 轉換功能而非常實用。它是高效生成專業級 PDF 的理想選擇。

使用 PuppeteerSharp 生成 PDF 有哪些限制?

PuppeteerSharp 需要下載和綁定 Chromium 瀏覽器,這會增加檔案大小,並可能耗費大量資源。它的重點在於渲染,而非使用額外的功能來增強 PDF。

在 HTML 到 PDF 的轉換方面,Pdfium.NET SDK 與 IronPDF 有何不同?

Pdfium.NET SDK 本身不支援 HTML 至 PDF 的轉換,需要手動渲染 HTML 元素。相比之下,IronPDF 提供了直接轉換的內建方法,簡化了轉換過程。

PdfSharp適合將複雜的HTML結構渲染為PDF嗎?

PdfSharp 本身不支援 HTML 到 PDF 的轉換,在處理複雜的佈局、樣式或 JavaScript 時可能會有困難,需要額外的函式庫來解析 HTML。

IronPDF 提供哪些 PDF 操作功能?

IronPDF 提供了創建、編輯和從 PDF 中提取內容的工具。它支援 HTML 直接轉換為 PDF、文字/圖片擷取,以及在應用程式中嵌入 PDF 檢視器。

IronPDF 是否與 .NET 10 相容,用於 .NET 10 專案時有哪些優點?

是 - IronPDF 與 .NET 10 完全相容。它支援 .NET 10 專案,不需要特殊的變通方式,並可利用運行時的改進,例如陣列介面方法去虛擬化、增強效能以及降低記憶體使用量。

IronPDF 在 .NET 10 中為 HTML-to-PDF 轉換帶來了哪些新的增強功能?

在 .NET 10 中,IronPDF 在最新版本中提供了 "day zero "支持,与新的运行时完全兼容。由於 .NET 10 渲染和 JIT 引擎的改進,開發人員可以獲得更快的啟動時間、更好的記憶體使用率,以及渲染性能的增強。

Curtis Chau
技術作家

Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。

除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。