Telerik HTML到PDF生成器与IronPDF
生成 PDF 文档因其复杂性而难,尤其是包含图像、表格及其他功能时.
主要挑战是弄清楚如何将普通文本文档转换为PDF格式。 有多种方法可以使用,但选择一种能保持原始文档格式的方法是至关重要的。
在本教程中,我将比较Progress Software Corporation的Telerik PdfProcessing库与Iron Software的IronPDF,看它们在生成PDF文档方面的表现如何。
如何在 Telerik 中将 HTML 转换为 PDF
- 安装 C# 库将 HTML 转换为 PDF
- 利用
Import方法在 C# 中加载现有 HTML 文件 - 使用
Export方法将 HTML 转换为 PDF - 将生成的 PDF 文档导出到所需位置
- 用 C# 中的一行执行步骤 3 和 4
Telerik PdfProcessing
Telerik PdfProcessing文档是Progress文档处理应用套件的一部分,让您无需编写任何代码即可创建和导出PDF。 它具有文本块、图像、表单、表格、导入和导出等功能。 此外,该库提供的功能非常适合于类似流的编辑。 PdfProcessing应用程序可用于网络、桌面和移动平台。
IronPDF for .NET PDF库
IronPDF是一个.NET PDF库,可以在不需要Adobe Acrobat或其他第三方软件的情况下生成PDF。 该库可以从零开始创建PDF或将现有的.NET组件(如ASP.NET官方网站网页、WPF用户界面等)导出为PDF文件。
安装
在这一部分中,我将介绍我们如何安装IronPDF和Telerik文档处理库。
Telerik文档处理库的安装
要使用Telerik文档处理套件从HTML创建一个PDF文档,我们必须安装三个库:
- Telerik.Documents.Core.Trial
- Telerik.Documents.Flow.FormatProviders.Doc.Trial
- Telerik.Documents.Flow.FormatProviders.Pdf.Trial
- Telerik.Documents.Flow.Trial
您可以使用NuGet包管理器安装这些库。

Telerik 和 Kendo UI 库
安装IronPDF C#.NET PDF库
您可以通过三种方式安装IronPDF:
- 使用NuGet包管理器控制台进行安装
- 使用NuGet Visual Studio GUI进行安装
- 下载IronPDF DLL文件进行手动安装
对于使用包管理器控制台的安装,您需要在控制台中编写以下命令。
Install-Package IronPdf
此命令将安装项目中最新版本的IronPDF库。 当然,您可以随时查看IronPDF的NuGet页面上的最新版本。
使用Telerik生成PDF
Telerik支持使用RadFlowDocument库插件将HTML转换为PDF。 它可以将包含HTML字符串的HTML文档转换为PDF文档。
您可以使用以下代码将HTML转换为PDF,使用Telerik。
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);上述代码有些复杂。 您首先需要创建一个HtmlFormatProvider和一个RadFlowDocument。 使用HtmlFormatProvider的Import函数导入HTML文件,并使用返回的RadFlowDocument对象生成一个PdfFormatProvider。 最后,使用PdfFormatProvider的WriteAllBytes方法将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");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");RenderUrlAsPdf函数将网页的URL转换为PDF。 它在呈现之前会等待加载所有相关文件,产生卓越的结果。 它保留了所有颜色、设计和用户界面。 您可以看到下面的输出。

URL 转 PDF
您可以在IronPDF教程页面上获取更多关于IronPDF的教程并查看它们的实际效果。
比较
正如我们在IronPDF和Telerik的ASP.NET结果的用户界面输出中看到的,我们可以说Telerik不是HTML到PDF转换的好选择,因为其渲染质量不佳。 您可以在下文中看到IronPDF和Telerik输出的区别。

输出比较
渲染质量
在上图中,您可以看到IronPDF和Telerik标准输出之间的明显差异。 让我们根据功能进行输出比较。
Telerik的渲染质量很差。 它渲染的PDF格式不好,无法保留文档的原始样式。 另一方面,IronPDF拥有卓越的渲染质量,保留了源文档的每个方面。
CSS和JavaScript支持
Telerik PdfProcessing主要用于基于代码的PDF生成,不原生支持外部CSS或JavaScript文件进行HTML转换。 其重点是程序化文档创建而非HTML渲染。
相反,IronPDF对内部和外部CSS以及JavaScript声明提供了充分支持。 JavaScript的处理可以根据需要在IronPDF中打开或关闭。
Telerik文档处理的限制
综上所述,以下是Telerik PdfProcessing在HTML到PDF工作流程中的一些其他限制:
- Telerik PdfProcessing不原生支持外部CSS或JavaScript文件进行HTML转换。
- 相较于基于浏览器的PDF生成器,HTML渲染功能有限。
- 没有内置的URL到PDF转换功能。
- 设计用于程序化PDF创建而非HTML文档转换。
- HTML渲染质量可能无法匹配源文档的外观。
IronPDF的功能
IronPDF的主要功能包括:
- IronPDF支持URL到PDF和HTML文件到PDF转换。
- IronPDF支持外部文件,如图像、CSS和JS文件。
- IronPDF自动加载每个文件,不使用任何外部库。
- IronPDF有详细的文档。
- IronPDF保留用户界面并提供完美的渲染质量。
IronPDF还有许多其他功能。 您可以访问IronPDF功能页面以获取最佳信息。

IronPDF 功能
结论
在本文中,我们比较了IronPDF和Telerik PdfDocument处理库,发现IronPDF在HTML到PDF转换方面远胜于Telerik库。
IronPDF是进行所有PDF相关操作的优秀库。 您可以在所有最新的.NET和.NET Core框架中创建、编辑和修改PDF文件。 访问IronPDF许可页面以获取有关IronPDF产品包的分发和许可的更多信息。
常见问题解答
如何在C#中将HTML转换为PDF?
你可以使用IronPDF的RenderHtmlAsPdf方法将HTML字符串转换为PDF。你还可以使用RenderHtmlFileAsPdf将HTML文件转换为PDF。
使用 IronPDF 进行 PDF 生成相对于 Telerik 有何优点?
IronPDF 提供卓越的呈现质量,支持外部 CSS 和 JavaScript 文件,并允许 URL 到 PDF 的转换。它还能比 Telerik PdfProcessing 更好地保留原始文档的样式和界面。
我可以使用 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,确保优质渲染,保留文档样式。






