跳過到頁腳內容
產品比較

Telerik的HTML到PDF生成器與IronPDF

程式化生成 PDF 文件可能很複雜,尤其是當需要包含圖片、表格、文本、格式及其他功能時。

主要的挑戰是了解如何將純文本文件轉換成 PDF 格式。 可以使用多種方法,但選擇一種能保持原文件格式的是至關重要的。

在此教程中,我將比較 Progress 軟體公司的 Telerik PdfProcessing 庫與 Iron Software 的 IronPDF 庫在生成 PDF 文件方面的表現。

class="hsg-featured-snippet">

如何在 Telerik 中將 HTML 轉換為 PDF

  1. 安裝 C# 庫以將 HTML 轉換為 PDF
  2. 利用 Import 方法在 C# 中加載現有的 HTML 文件
  3. Export 方法將 HTML 轉換為 PDF
  4. 將生成的 PDF 文件導出到所需位置
  5. 在 C# 中用一行代碼完成步驟 3 和 4

Telerik PdfProcessing

Telerik PdfProcessing 文檔,它是 Progress 文檔處理應用程式組合的一部分,允許您創建 PDF 並在不編寫任何代碼的情況下導出它們。 它有文本區塊、圖片、表單、表格、導入和導出等功能。 此外,此庫提供了適合流內容編輯的功能。 PdfProcessing 應用程式在網頁、桌面和移動平台上皆可使用。

IronPDF .NET PDF 庫

IronPDF 是一個 .NET PDF 庫,可以生成 PDF,而無需 Adobe Acrobat 或其他第三方軟體。 該庫可從零開始創建 PDF,或將現有的 .NET 組件(如ASP.NET 官方網站網頁、WPF 用戶界面等)導出為 PDF 文件。

安裝

在本節中,我將介紹如何安裝 IronPDF 和 Telerik 文檔處理庫。

安裝 of Telerik Document Processing Libraries

要使用 Telerik 文檔處理套件從 HTML 創建 PDF 文檔,我們需要安裝三個庫:

  1. Telerik.Documents.Core.Trial
  2. Telerik.Documents.Flow.FormatProviders.Doc.Trial
  3. Telerik.Documents.Flow.FormatProviders.Pdf.Trial
  4. Telerik.Documents.Flow.Trial

您可以使用 NuGet 包管理器安裝這些庫。

class="content-img-align-center"> Telerik HTML 到 PDF PDF 生成器對比 IronPDF - 圖 1: Telerik 和 Kendo UI 庫

class="content__image-caption">Telerik 和 Kendo UI 庫

安裝 of IronPDF C#.NET PDF Library

您可以通過以下三種方式安裝 IronPDF:

  1. 使用 NuGet 包管理器控制台安裝
  2. 使用 NuGet Visual Studio 圖形用戶界面安裝
  3. 下載IronPDF DLL 文件以進行手動安裝

使用包管理器控制台安裝時,您需要在控制台中輸入以下命令。

Install-Package IronPdf

這個命令將在項目中安裝最新版本的 IronPDF 庫。 當然,您可以隨時在IronPDF 的 NuGet 頁面查看 IronPDF 的最新版本。

使用 Telerik 生成 PDF

Telerik 支持使用 RadFlowDocument 庫附加組件將 HTML 轉換為 PDF。 它可以將包含 HTML 字符串的 HTML 文檔轉換成 PDF 文檔。

您可以使用以下代碼來使用 Telerik 將 HTML 轉換為 PDF。

using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using Telerik.Windows.Documents.Flow.Model;

// Create an HTML format provider for importing HTML files.
HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();

// Create a document instance from the content of an HTML file.
RadFlowDocument document = htmlProvider.Import(File.ReadAllText(@"C:\HTML Website\website\index.html"));

// Create a PDF format provider for exporting the document.
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

// Export the document to a byte array.
byte[] pdfBytes = pdfProvider.Export(document);

// Save the PDF byte array to a file.
File.WriteAllBytes(@"C:/test.pdf", pdfBytes);
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using Telerik.Windows.Documents.Flow.Model;

// Create an HTML format provider for importing HTML files.
HtmlFormatProvider htmlProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider();

// Create a document instance from the content of an HTML file.
RadFlowDocument document = htmlProvider.Import(File.ReadAllText(@"C:\HTML Website\website\index.html"));

// Create a PDF format provider for exporting the document.
Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

// Export the document to a byte array.
byte[] pdfBytes = pdfProvider.Export(document);

// Save the PDF byte array to a file.
File.WriteAllBytes(@"C:/test.pdf", pdfBytes);
Imports Telerik.Windows.Documents.Flow.FormatProviders.Html
Imports Telerik.Windows.Documents.Flow.Model

' Create an HTML format provider for importing HTML files.
Private htmlProvider As HtmlFormatProvider = New Telerik.Windows.Documents.Flow.FormatProviders.Html.HtmlFormatProvider()

' Create a document instance from the content of an HTML file.
Private document As RadFlowDocument = htmlProvider.Import(File.ReadAllText("C:\HTML Website\website\index.html"))

' Create a PDF format provider for exporting the document.
Private pdfProvider As New Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider()

' Export the document to a byte array.
Private pdfBytes() As Byte = pdfProvider.Export(document)

' Save the PDF byte array to a file.
File.WriteAllBytes("C:/test.pdf", pdfBytes)
$vbLabelText   $csharpLabel

上面的代碼有些複雜。 您首先需要創建一個 HtmlFormatProvider 和一個 RadFlowDocument。 使用 HtmlFormatProviderImport 函數導入 HTML 文件,然後使用返回的 RadFlowDocument 對象生成 PdfFormatProvider。 最後,使用 PdfFormatProviderWriteAllBytes 方法將 PDF 文件導出到特定位置。

Telerik 生成的輸出不好。 Telerik 未能保留 HTML 文檔的 UI,也未加載任何圖片。

class="content-img-align-center"> Telerik HTML 到 PDF PDF 生成器對比 IronPDF - 圖 2: Telerik 輸出

class="content__image-caption">Telerik 輸出

使用 IronPDF 生成 PDF

IronPDF 可以使用 HTML 文件、HTML 字符串和 URL 生成 PDF。

HTML 到 PDF

使用以下代碼來使用 HTML 文件創建 PDF 文檔。

using IronPdf;

// Create an instance of ChromePdfRenderer
var IronRenderer = new ChromePdfRenderer();

// Set the renderer's options to fit to the specified paper mode.
IronRenderer.RenderingOptions.FitToPaperMode = IronPdf.Engines.Chrome.FitToPaperModes.FixedPixelWidth;

// Render the HTML file as a PDF document.
var pdfFromHtmlFile = IronRenderer.RenderHtmlFileAsPdf(@"C:\HTML Website\website\index.html");

// Save the rendered PDF document to a file.
pdfFromHtmlFile.SaveAs(@"C:/IronPDF Test.pdf");
using IronPdf;

// Create an instance of ChromePdfRenderer
var IronRenderer = new ChromePdfRenderer();

// Set the renderer's options to fit to the specified paper mode.
IronRenderer.RenderingOptions.FitToPaperMode = IronPdf.Engines.Chrome.FitToPaperModes.FixedPixelWidth;

// Render the HTML file as a PDF document.
var pdfFromHtmlFile = IronRenderer.RenderHtmlFileAsPdf(@"C:\HTML Website\website\index.html");

// Save the rendered PDF document to a file.
pdfFromHtmlFile.SaveAs(@"C:/IronPDF Test.pdf");
Imports IronPdf

' Create an instance of ChromePdfRenderer
Private IronRenderer = New ChromePdfRenderer()

' Set the renderer's options to fit to the specified paper mode.
IronRenderer.RenderingOptions.FitToPaperMode = IronPdf.Engines.Chrome.FitToPaperModes.FixedPixelWidth

' Render the HTML file as a PDF document.
Dim pdfFromHtmlFile = IronRenderer.RenderHtmlFileAsPdf("C:\HTML Website\website\index.html")

' Save the rendered PDF document to a file.
pdfFromHtmlFile.SaveAs("C:/IronPDF Test.pdf")
$vbLabelText   $csharpLabel

RenderHtmlFileAsPdf 方法用於從 HTML 文件生成 PDF。此功能讀取 HTML 文件中的所有內容,並加載相關的 CSS 和 JavaScript 文件。 RenderHtmlFileAsPdf 方法的輸出如下所示。

class="content-img-align-center"> Telerik HTML 到 PDF PDF 生成器對比 IronPDF - 圖 3: IronPDF HTML 到 PDF

class="content__image-caption">IronPDF HTML 到 PDF

IronPDF 使用 HTML 生成 PDF 非常美觀。 這結果與 Telerik 生成的 PDF 不同且更好。

URL 到 PDF

您可以使用以下代碼從 URL 生成 PDF。

using IronPdf.Rendering;
using IronPdf;

// Create an instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Set the paper size for rendering the PDF.
renderer.RenderingOptions.PaperSize = PdfPaperSize.A2;

// Render the specified URL as a PDF document.
PdfDocument myPdf = renderer.RenderUrlAsPdf("https://dotnet.microsoft.com/en-us/");

// Save the rendered PDF document to a file.
myPdf.SaveAs(@"C:/dotnet.pdf");
using IronPdf.Rendering;
using IronPdf;

// Create an instance of ChromePdfRenderer
ChromePdfRenderer renderer = new ChromePdfRenderer();

// Set the paper size for rendering the PDF.
renderer.RenderingOptions.PaperSize = PdfPaperSize.A2;

// Render the specified URL as a PDF document.
PdfDocument myPdf = renderer.RenderUrlAsPdf("https://dotnet.microsoft.com/en-us/");

// Save the rendered PDF document to a file.
myPdf.SaveAs(@"C:/dotnet.pdf");
Imports IronPdf.Rendering
Imports IronPdf

' Create an instance of ChromePdfRenderer
Private renderer As New ChromePdfRenderer()

' Set the paper size for rendering the PDF.
renderer.RenderingOptions.PaperSize = PdfPaperSize.A2

' Render the specified URL as a PDF document.
Dim myPdf As PdfDocument = renderer.RenderUrlAsPdf("https://dotnet.microsoft.com/en-us/")

' Save the rendered PDF document to a file.
myPdf.SaveAs("C:/dotnet.pdf")
$vbLabelText   $csharpLabel

RenderUrlAsPdf 函數將網頁的 URL 轉換為 PDF。 它在生成之前會等待加載所有相關的文件,從而產生卓越的結果。 它保留了所有顏色、設計和 UI。 您可以在下面看到輸出。

class="content-img-align-center"> Telerik HTML 到 PDF PDF 生成器對比 IronPDF - 圖 4: IronPDF URL 到 PDF

class="content__image-caption">URL 到 PDF

您可以在 IronPDF 教程頁面 查看更多關於 IronPDF 的教程並觀看他們的實際運行。

比較

正如我們看到的,IronPDF 和 Telerik 的 ASP.NET 結果的 UI,讓我們得出結論:Telerik 用於 HTML 到 PDF 轉換不是個好選擇,因其渲染質量不佳。 您可以在下面看到 IronPDF 和 Telerik 的輸出差異。

class="content-img-align-center"> Telerik HTML 到 PDF PDF 生成器對比 IronPDF - 圖 5: 輸出比較

class="content__image-caption">輸出比較

渲染質量

在上圖中,您可以看到 IronPDF 和 Telerik 的標準輸出之間的明顯差異。 讓我們以功能為基準來比較輸出。

Telerik 的渲染質量很差。 它生成的 PDF 格式不良,無法保持原始文檔的樣式。 另一方面,IronPDF 的渲染質量優秀,保持了原始文檔的每一個方面。

CSS 和 JavaScript 支持

Telerik PdfProcessing 是主要面向基於代碼的 PDF 生成設計的,不支持外部 CSS 或 JavaScript 文件的 HTML 轉換。 它側重於程序化文檔創建而非 HTML 渲染。

相反,IronPDF 完全支持內部和外部 CSS 和 JavaScript 聲明。 JavaScript 的處理可以根據需要在 IronPDF 中開啟或關閉。

Telerik 文檔處理的限制

總結來說,Telerik PdfProcessing 用於 HTML 到 PDF 工作流程的限制有以下幾點:

  1. Telerik PdfProcessing 不支持外部 CSS 或 JavaScript 文件的 HTML 轉換。
  2. 與基於瀏覽器的 PDF 生成器相比,HTML 渲染能力有限。
  3. 沒有內置的 URL 到 PDF 轉換功能。
  4. 設計用于程式化 PDF 的創建,而非 HTML 文檔轉換。
  5. HTML 渲染質量可能與源文檔的外觀不符。

IronPDF 的特點

IronPDF 主要特點包括:

  • IronPDF 支持 URL 到 PDF 和 HTML 文件到 PDF 的轉換。
  • IronPDF 支持外部文件如圖像、CSS 和 JS 文件。
  • IronPDF 自動加載每個文件,無需使用任何外部庫。
  • IronPDF 擁有廣泛的文檔支持。
  • IronPDF 保留了用戶界面並提供完美的渲染質量。

IronPDF 還有許多其他功能。 您可以訪問 IronPDF 功能頁面獲取最佳信息。

class="content-img-align-center"> Telerik HTML 到 PDF PDF 生成器對比 IronPDF - 圖 6: IronPDF 特點

class="content__image-caption">IronPDF 特點

結論

在本文中,我們比較了 IronPDF 與 Telerik PdfDocument 處理庫,並發現 IronPDF 比 Telerik 庫的 HTML 到 PDF 轉換效果要好得多。

IronPDF 是所有 PDF 相關操作的優秀庫。 您可以在所有最新的 .NET 和 .NET 核心框架中創建、編輯和修改 PDF 文件。 訪問 IronPDF 許可頁面獲取有關 IronPDF 產品套裝分發和許可的更多信息。

請注意Progress Software Corporation 和 Telerik 是個別所属公司的註冊商標。 本網站與 Progress Software Corporation 或 Telerik 無關,亦未受到任何支持或贊助。 所有產品名稱、徽標和品牌均為其各自所有者的財產。 比較僅供信息參考,並反映撰寫時公開可用的信息。

常見問題解答

怎樣在 C# 中將 HTML 轉換為 PDF?

您可以使用 IronPDF 的 RenderHtmlAsPdf 方法將 HTML 字符串轉換為 PDF。您還可以使用 RenderHtmlFileAsPdf 將 HTML 文件轉換為 PDF。

使用 IronPDF 進行 PDF 生成比使用 Telerik 有何好處?

IronPDF 提供優越的渲染質量,支持外部 CSS 和 JavaScript 文件,並允許將 URL 轉換為 PDF。它還比 Telerik PdfProcessing 更好地保留了原始文檔的樣式和 UI。

我可以使用 IronPDF 處理外部 CSS 和 JavaScript 文件嗎?

是的,IronPDF 支持外部 CSS 和 JavaScript 文件的包含,從而確保 HTML 文檔的準確渲染。

IronPDF 有哪些安裝方法?

IronPDF 可以使用 NuGet 軟件包管理器控制台安裝,也可以通過 NuGet Visual Studio GUI 或下載 IronPDF DLL 文件進行手動安裝。

為什麼 Telerik PdfProcessing 的渲染質量可能有限?

Telerik PdfProcessing 不支持外部 CSS、JavaScript 或 URL 到 PDF 轉換,導致渲染質量差並且文檔功能不完整。

IronPDF 有哪些主要功能?

IronPDF 支持 URL 到 PDF 和 HTML 文件到 PDF 的轉換,處理像圖像、CSS 和 JS 等外部文件,並提供出色的渲染質量。它還包括全面的文檔。

可以使用 IronPDF 將 URL 轉換為 PDF 嗎?

是的,IronPDF 允許 URL 到 PDF 的轉換,使用其多功能渲染能力保持原始樣式和內容。

如何使用軟件包管理器控制台安裝 IronPDF?

要通過軟件包管理器控制台安裝 IronPDF,請使用命令Install-Package IronPdf

Telerik PdfProcessing 在處理 PDF 中的圖像時面臨哪些挑戰?

由於不支持外部 CSS 和 JavaScript,Telerik PdfProcessing 在 PDF 中渲染圖像時遇到困難,可能會影響文檔的整體質量和完整性。

IronPDF 如何確保比 Telerik 更好的 PDF 文檔質量?

IronPDF 通過支持外部 CSS 和 JavaScript,提供全面的文檔,以及提供強大的渲染能力來保留原始文檔的樣式和 UI,確保更高的質量。

Curtis Chau
技術作家

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

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