Telerik的HTML到PDF生成器與IronPDF
由於 PDF 文件的複雜性,特別是包含圖片、表格、文字、格式和其他功能時,以程式方式產生 PDF 文件可能會很複雜。
主要的挑戰是如何將純文字文件轉換成 PDF 格式。 可以使用許多方法,但選擇一種能夠保持原始文件格式的方法是非常重要的。
在本教程中,我將比較 Progress Software Corporation 的 Telerik PdfProcessing 函式庫與 Iron Software 的 IronPDF,兩者在產生 PDF 文件的能力上有多大的差異。
如何在 Telerik 中將 HTML 轉換為 PDF
- 安裝 C# 函式庫將 HTML 轉換為 PDF
- 在 C# 中利用
Import方法載入現有的 HTML 檔案 - 使用
Export方法將 HTML 轉換為 PDF - 將產生的 PDF 文件匯出至所需位置
- 在 C# 中以一行執行步驟 3 和 4
Telerik PDF處理
Telerik PdfProcessing Documentation 是 Progress 文件處理應用程式組合的一部分,可讓您在不寫任何程式碼的情況下建立 PDF 並將其匯出。 它具有文字區塊、圖片、表單、表格、匯入和匯出等功能。 此外,資料庫提供的功能非常適合流程式編輯。 PdfProcessing 應用程式適用於網頁、桌上型電腦和行動平台。
IronPDF for .NET PDF 函式庫
IronPDF for .NET 是一個 .NET PDF 函式庫,可以在不需要 Adobe Acrobat 或其他第三方軟體的情況下產生 PDF。 這個函式庫可以從頭開始建立 PDF,或是將現有的 .NET 元件 (像是 ASP.NET 官方網站網頁、WPF 使用者介面等) 匯出成 PDF 檔案。
安裝
在本節中,我將介紹我們如何安裝 IronPDF 和 Telerik 文件處理函式庫。
安裝 Telerik 文件處理函式庫
要使用 Telerik Document Processing 套件從 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 Package Manager 安裝這些函式庫。
Telerik 和 Kendo UI 程式庫
安裝 IronPDF C#.NET PDF 函式庫。
您可以使用三種方式安裝 IronPDF:
1.使用 NuGet 套件管理員控制台安裝 2.使用 NuGet Visual Studio GUI 安裝 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)
上述程式碼有些複雜。 您首先需要建立 HtmlFormatProvider 和 RadFlowDocument。 使用 Import 函數匯入 HTML 文件,並使用傳回的 RadFlowDocument 物件產生 PdfFormatProvider。 最後,使用 WriteAllBytes 方法將 PdfFormatProvider 匯出 PDF 檔案到指定位置。
Telerik 產生的輸出效果不佳。 Telerik 沒有保留 HTML 文件的使用者介面,也沒有載入任何圖片。
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")
RenderHtmlFileAsPdf 方法用於從 HTML 檔案產生 PDF。此函數會讀取 HTML 檔案中的所有內容,並載入相關的 CSS 和 JavaScript 檔案。 下面顯示的是 RenderHtmlFileAsPdf 方法的輸出。
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")
RenderUrlAsPdf 函數將網頁的 URL 轉換為 PDF。 它會等待載入所有相關檔案後再進行渲染,產生非凡的效果。 並保留所有顏色、設計和 UI。 您可以在下方看到輸出結果。
URL 到 PDF
您可以在 IronPDF 教程頁面上獲得更多關於 IronPDF 的教程,並觀看它們的實際應用。
比較
由於我們已經看到 IronPDF 和 Telerik 的 ASP.NET 結果的輸出 UI,我們可以說 Telerik 不是 HTML 轉 PDF 的好選擇,因為它的渲染品質不佳。 您可以在下面看到 IronPDF 和 Telerik 輸出的差異。
輸出比較
渲染品質
在上圖中,您可以看到 IronPDF 和 Telerik 標準輸出之間的明顯差異。 讓我們根據功能來比較輸出的結果。
Telerik 的渲染品質不佳。 其渲染的 PDF 格式不佳,無法保留文件的原始樣式。 另一方面,IronPDF 擁有出色的渲染品質,能保留來源文件的每個面向。
CSS 和 JavaScript 支援。
Telerik PdfProcessing 主要是針對基於程式碼的 PDF 產生而設計,本機並不支援外部 CSS 或 JavaScript 檔案的 HTML 轉換。 其重點在於程式化文件的建立,而非 HTML 的呈現。
反之,IronPDF 完全支援內外部 CSS 與 JavaScript 聲明。 IronPDF 可根據需要開啟或關閉 JavaScript 的處理。
Telerik 文件處理的限制
總括而言,以下是 Telerik PdfProcessing 用於 HTML-to-PDF 工作流程的一些額外限制:
1.Telerik PdfProcessing 本身不支援 HTML 轉換的外部 CSS 或 JavaScript 檔案。 2.與基於瀏覽器的 PDF 產生器相比,HTML 渲染能力有限。 3.無內建 URL 至 PDF 轉換功能。 4.專為程式化 PDF 製作而設計,而非 HTML 文件轉換。 5.HTML 呈現的品質可能與原始文件的外觀不符。
IronPDF 的特點
IronPDF 的主要功能如下:
- IronPDF 支援 URL 至 PDF 及 HTML 檔案至 PDF 的轉換。
- IronPDF 支援圖片、CSS 和 JS 檔案等外部檔案。
- IronPDF 可自動載入每個檔案,無需使用任何外部程式庫。
- IronPDF 有大量的文件。
- IronPDF 保留了 UI,並提供完美的渲染品質。
IronPDF 還有許多其他功能。 您可以造訪 IronPDF 功能頁面 以取得最佳資訊。
IronPDF 特性
結論
在這篇文章中,我們比較了 IronPDF 與 Telerik PdfDocument 處理函式庫,發現 IronPDF 在 HTML 至 PDF 的轉換上遠遠優於 Telerik 函式庫。
IronPDF 是所有 PDF 相關作業的絕佳函式庫。 您可以在所有最新的 .NET 和 .NET Core Framework 中建立、編輯和修改 PDF 檔案。 請造訪 IronPDF授權頁面,取得更多關於 IronPDF 產品套件的發行與授權資訊。
常見問題解答
怎樣在 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。

