跳至页脚内容
产品比较

Telerik HTML到PDF生成器与IronPDF

由于复杂性,生成PDF文档编程可能很复杂,特别是当包括图像、表格、文本、格式和其他功能时。

主要挑战是弄清楚如何将普通文本文档转换为PDF格式。 有多种方法可以使用,但选择一种能保持原始文档格式的方法是至关重要的。

在本教程中,我将比较Progress Software Corporation的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库,可以在不需要Adobe Acrobat或其他第三方软件的情况下生成PDF。 该库可以从零开始创建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生成器对比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 GUI进行安装
  3. 下载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);
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文档的用户界面,也没有加载任何图像。

class="content-img-align-center"> Telerik HTML到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生成器对比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。 它在呈现之前会等待加载所有相关文件,产生卓越的结果。 它保留了所有颜色、设计和用户界面。 您可以看到下面的输出。

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

class="content__image-caption">URL到PDF

您可以在IronPDF教程页面上获取更多关于IronPDF的教程并查看它们的实际效果。

比较

正如我们在IronPDF和Telerik的ASP.NET结果的用户界面输出中看到的,我们可以说Telerik不是HTML到PDF转换的好选择,因为其渲染质量不佳。 您可以在下文中看到IronPDF和Telerik输出的区别。

class="content-img-align-center"> Telerik HTML到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生成器对比IronPDF - 图6:IronPDF功能

class="content__image-caption">IronPDF功能

结论

在本文中,我们比较了IronPDF和Telerik PdfDocument处理库,发现IronPDF在HTML到PDF转换方面远胜于Telerik库。

IronPDF是进行所有PDF相关操作的优秀库。 您可以在所有最新的.NET和.NET Core框架中创建、编辑和修改PDF文件。 访问IronPDF许可页面以获取有关IronPDF产品包的分发和许可的更多信息。

[{i:(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 更好地保留原始文档的样式和界面。

我可以使用 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,提供全面的文档,并提供强大的呈现能力,保留原始文档的样式和界面,确保更优质的质量。

Curtis Chau
技术作家

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。