PDF控件Spire.PDF for .NET【转换】教程:将 HTML 转换为 PDF

Spire.Doc是一款专门对 Word 文档进行操作的 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。 

将 HTML 内容转换为 PDF 具有许多优势,包括能够离线阅读,以及以高保真度保留内容和格式。Spire.PDF提供了两种HTML转PDF的方法,一种是使用QT Web插件,另一种是不使用插件。我们建议您使用 QT 插件进行转换。

以下部分演示了如何使用Spire.PDF for .NET使用或不使用 QT 插件将HTML 网页(URL)HTML 字符串呈现为 PDF 文档。

  • 使用 QT 插件将 URL 转换为 PDF
  • 使用 QT 插件将 HTML 字符串转换为 PDF
  • 无需插件即可将 URL 转换为 PDF
  • 不使用插件将 HTML 字符串转换为 PDF

安装适用于 .NET 的 Spire.PDF

首先,您需要将包含在 Spire.PDF for.NET 包中的 DLL 文件添加为您的 .NET 项目中的引用。DLL 文件可以从此链接下载或通过NuGet安装

PM> Install-Package Spire.PDF

下载插件

如果您选择插件方式,请从以下链接下载适合您操作系统的插件。

将包解压缩到磁盘上的某个位置以获取“plugins”文件夹。在本例中,我们将插件保存在“F:\Libraries\Plugin\plugins-windows-x64\plugins”路径下。

此外,我们建议您将项目的“平台目标”相应地设置为 x64 或 x86。

使用 QT 插件将 URL 转换为 PDF

以下是使用带 QT 插件的 Spire.PDF 将 URL 转换为 PDF 的步骤。

  • 指定要转换的 URL 路径。
  • 指定生成的 PDF 文件的路径。
  • 指定插件路径,并将其分配为HtmlConverter.PluginPath属性的值。
  • 调用HtmlConverter.Convert(string url, string fileName, bool enableJavaScript, int timeout, SizeF pageSize, PdfMargins margins)方法将 URL 转换为 PDF 文档。

【C#】

using Spire.Pdf.Graphics;
using Spire.Pdf.HtmlConverter.Qt;
using System.Drawing;

namespace ConvertUrlToPdf
{
class Program
{
static void Main(string[] args)
{
//Specify the URL path
string url = "https://www.wikipedia.org/";

//Specify the output file path
string fileName = "UrlToPdf.pdf";

//Specify the plugin path
string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

//Set the plugin path
HtmlConverter.PluginPath = pluginPath;

//Convert URL to PDF
HtmlConverter.Convert(url, fileName, true, 100000, new Size(1080, 1000), new PdfMargins(0));
}
}
}

【VB.NET】

Imports Spire.Pdf.Graphics
Imports Spire.Pdf.HtmlConverter.Qt
Imports System.Drawing

Namespace ConvertUrlToPdf
Class Program
Shared Sub Main(ByVal args() As String)
'Specify the URL path
Dim url As String = "https://www.wikipedia.org/"

'Specify the output file path
Dim fileName As String = "UrlToPdf.pdf"

'Specify the plugin path
Dim pluginPath As String = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins"

'Set the plugin path
HtmlConverter.PluginPath = pluginPath

'Convert URL to PDF
HtmlConverter.Convert(url, fileName, True, 100000, New Size(1080, 1000), New PdfMargins(0))
End Sub
End Class
End Namespace

使用 QT 插件将 HTML 字符串转换为 PDF

以下是使用带 QT 插件的 Spire.PDF 将 HTML 字符串转换为 PDF 的步骤。

  • 从 .html 文件中获取 HTML 字符串。
  • 指定生成的 PDF 文件的路径。
  • 指定插件路径,并将其分配为HtmlConverter.PluginPath属性的值。
  • 调用HtmlConverter.Convert(string htmlString, string fileName, bool enableJavaScript, int timeout, SizeF pageSize, PdfMargins margins, Spire.Pdf.HtmlConverter.LoadHtmlType htmlType)方法将 HTML 字符串转换为 PDF 文档。

注意:只有内联 CSS 样式和内部 CSS 样式才能在 PDF 上正确呈现。如果您有外部 CSS 样式表,请将其转换为内联或内部 CSS 样式。

【C#】

using System.IO;
using Spire.Pdf.HtmlConverter.Qt;
using System.Drawing;
using Spire.Pdf.Graphics;

namespace ConvertHtmlStringToPdfWithPlugin
{
class Program
{
static void Main(string[] args)
{
//Get the HTML string from a .html file
string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");

//Specify the output file path
string fileName = "HtmlStringToPdf.pdf";

//Specify the plugin path
string pluginPath = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins";

//Set plugin path
HtmlConverter.PluginPath = pluginPath;

//Convert HTML string to PDF
HtmlConverter.Convert(htmlString, fileName, true, 100000, new Size(1080, 1000), new PdfMargins(0), Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode);
}
}
}

【VB.NET】

Imports System.IO
Imports Spire.Pdf.HtmlConverter.Qt
Imports System.Drawing
Imports Spire.Pdf.Graphics

Namespace ConvertHtmlStringToPdfWithPlugin
Class Program
Shared Sub Main(ByVal args() As String)
'Get the HTML string from a .html file
Dim htmlString As String = File.ReadAllText("C:\Users\Administrator\Desktop\Document\Html\Sample.html")

'Specify the output file path
Dim fileName As String = "HtmlStringToPdf.pdf"

'Specify the plugin path
Dim pluginPath As String = "F:\\Libraries\\Plugin\\plugins-windows-x64\\plugins"

'Set plugin path
HtmlConverter.PluginPath = pluginPath

'Convert URL to PDF
HtmlConverter.Convert(htmlString, fileName, True, 100000, New Size(1080, 1000), New PdfMargins(0), Spire.Pdf.HtmlConverter.LoadHtmlType.SourceCode)
End Sub
End Class
End Namespace

无需插件即可将 URL 转换为 PDF

以下是使用无插件的 Spire.PDF 将 URL 转换为 PDF 的步骤。

  • 创建一个PdfDocument对象。
  • 创建一个PdfPageSettings对象,并通过它设置页面大小和边距。
  • 创建一个PdfHtmlLayoutFormat对象,并将其IsWaiting属性设置为 true。
  • 指定要转换的 URL 路径。
  • 使用PdfDocument.LoadFromHTML()方法从 URL 路径加载 HTML 。
  • 使用PdfDocument.SaveToFile()方法将文档保存到 PDF 文件。

【C#】

using System;
using Spire.Pdf;
using System.Threading;
using Spire.Pdf.HtmlConverter;
using System.Drawing;

namespace ConverUrlToPdfWithoutPlugin
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Create a PdfPageSettings object
PdfPageSettings setting = new PdfPageSettings();

//Save page size and margins through the object
setting.Size = new SizeF(1000, 1000);
setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);

//Create a PdfHtmlLayoutFormat object
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();

//Set IsWaiting property to true
htmlLayoutFormat.IsWaiting = true;

//Specific the URL path to convert
String url = "https://www.wikipedia.org/";

//Load HTML from a URL path using LoadFromHTML method
Thread thread = new Thread(() =>
{ doc.LoadFromHTML(url, true, true, false, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

//Save the document to a PDF file
doc.SaveToFile("UrlToPdf.pdf");
doc.Close();
}
}
}

【VB.NET】

Imports System
Imports Spire.Pdf
Imports System.Threading
Imports Spire.Pdf.HtmlConverter
Imports System.Drawing

Namespace ConverUrlToPdfWithoutPlugin
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Create a PdfPageSettings object
Dim setting As PdfPageSettings = New PdfPageSettings()

'Save page size and margins through the object
setting.Size = New SizeF(1000, 1000)
setting.Margins = New Spire.Pdf.Graphics.PdfMargins(20)

'Create a PdfHtmlLayoutFormat object
Dim htmlLayoutFormat As PdfHtmlLayoutFormat = New PdfHtmlLayoutFormat()

'Set IsWaiting property to true
htmlLayoutFormat.IsWaiting = True

'Specific the URL path to convert
Dim url As String = "https://www.wikipedia.org/"

'Load HTML from a URL path using LoadFromHTML method
Thread thread = New Thread(() =>
{
doc.LoadFromHTML(url, True, True, False, setting, htmlLayoutFormat)
}
)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()

'Save the document to a PDF file
doc.SaveToFile("UrlToPdf.pdf")
doc.Close()
End Sub
End Class
End Namespace

不使用插件将 HTML 字符串转换为 PDF

以下是使用无插件的 Spire.PDF 将 HTML 字符串转换为 PDF 的步骤。

  • 创建一个PdfDocument对象。
  • 创建一个PdfPageSettings对象,并通过它设置页面大小和边距。
  • 创建一个PdfHtmlLayoutFormat对象,并将其IsWaiting属性设置为 true。
  • 从 .html 文件中读取 HTML 字符串。
  • 使用PdfDocument.LoadFromHTML()方法从 HTML 字符串加载 HTML 。
  • 使用PdfDocument.SaveToFile()方法将文档保存到 PDF 文件。

【C#】

using Spire.Pdf;
using Spire.Pdf.HtmlConverter;
using System.IO;
using System.Threading;
using System.Drawing;

namespace ConvertHtmlStringToPdfWithoutPlugin
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument doc = new PdfDocument();

//Create a PdfPageSettings object
PdfPageSettings setting = new PdfPageSettings();

//Save page size and margins through the object
setting.Size = new SizeF(1000, 1000);
setting.Margins = new Spire.Pdf.Graphics.PdfMargins(20);

//Create a PdfHtmlLayoutFormat object
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();

//Set IsWaiting property to true
htmlLayoutFormat.IsWaiting = true;

//Read html string from a .html file
string htmlString = File.ReadAllText(@"C:\Users\Administrator\Desktop\Document\Html\Sample.html");

//Load HTML from html string using LoadFromHTML method
Thread thread = new Thread(() =>
{ doc.LoadFromHTML(htmlString, true, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();

//Save to a PDF file
doc.SaveToFile("HtmlStringToPdf.pdf");
}
}
}

【VB.NET】

Imports Spire.Pdf
Imports Spire.Pdf.HtmlConverter
Imports System.IO
Imports System.Threading
Imports System.Drawing

Namespace ConvertHtmlStringToPdfWithoutPlugin
Class Program
Shared Sub Main(ByVal args() As String)
'Create a PdfDocument object
Dim doc As PdfDocument = New PdfDocument()

'Create a PdfPageSettings object
Dim setting As PdfPageSettings = New PdfPageSettings()

'Save page size and margins through the object
setting.Size = New SizeF(1000, 1000)
setting.Margins = New Spire.Pdf.Graphics.PdfMargins(20)

'Create a PdfHtmlLayoutFormat object
Dim htmlLayoutFormat As PdfHtmlLayoutFormat = New PdfHtmlLayoutFormat()

'Set IsWaiting property to true
htmlLayoutFormat.IsWaiting = True

'Read html string from a .html file
Dim htmlString As String = File.ReadAllText("C:\Users\Administrator\Desktop\Document\Html\Sample.html")

'Load HTML from html string using LoadFromHTML method
Thread thread = New Thread(() =>
{
doc.LoadFromHTML(htmlString, True, setting, htmlLayoutFormat)
}
)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()

'Save to a PDF file
doc.SaveToFile("HtmlStringToPdf.pdf")
End Sub
End Class
End Namespace

以上便是如何将HTML 转换为 PDF,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

猜你喜欢

转载自blog.csdn.net/m0_67129275/article/details/131302506