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,如果您有其他问题也可以继续浏览本系列文章,获取相关教程~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Free Spire.PDF for .NETSpire.PDF for .NET 的免费版本,无需购买即可用于个人或商业用途。使用该组件,程序员可以 在.NET 程序中创建、读取、写入、编辑和操作 PDF 文档。这个控件能支持的功能十分全面,例如文档安全性设置(电子签名),提取 PDF 文本、附件、图片,PDF 合并和拆分,更新 Metadata,设置 Section,绘制图形、插入图片、表格制作和加工、导入数据等等。除此以外,Spire.PDF 还可以将 TXT 文本、图片、HTML 高质量地转换PDF 文件格式。 主要功能如下: 1.高质量的文档转换。Free Spire.PDF for .NET 支持 PDF 到 Word、XPS、SVG、EMF、Text 和图片(EMF、JPG、PNG、BMP、TIFF)的格式转换。也支持从 XML、HTML、RTF、XPS、Text、图片等格式生成 PDF 文档。 2.文档操作及域功能。支持合并、拆分 PDF 文档,在原有的 PDF 文档页添加覆盖页。同时,Spire.PDF 提供导入、邮戳、小册子功能,以及帮助用户从数据库读取数据并填充到域的域填写功能。 3. 安全性设置。用户可以通过设置密码和数字签名来保护 PDF 文档。用户密码和所有者密码可以确定加密的 PDF 文档的可读性、可修改性、是否可打印等有选择性的限制。与此同时,数字签名作为一个更有效的方法,可以应用于维护和对PDF文档进行身份验证。 4.数据提取。支持快速高效地从 PDF 文档提取图片、文本、PDF 分页,以及附件。 5.文件属性设置。支持对 Metadata、文件属性、页面方向、页面大小进行设置。其中文件属性包括文件限制(打印、页面提取、加评论等方面的权限限制)以及文件描述属性(文件名称、作者、主题、关键字等)。使用 Spire.PDF for .NET,用户还可以根据自己阅读喜好设定默认打开页码,分页模式,缩放比例和打印缩放,等等。 6.其他功能。 支持多种语言,支持字体格式、对齐方式设置。 绘制文字,图片,图形。 支持添加图层,透明图像,Color Space,条形码到 PDF。 支持 PDF/A-1b、PDF/x1a:2001 格式。 添加梯状图形和矢量图像到指定位置。 添加并格式化表格。 插入交互元素,例如添加自定义的 Annotation、Action、JavaScript、附件、书签等。
Free Spire.PDFViewer for .NETSpire.PDFViewer for .NET 的免费版本,使用 Free Spire.PDFViewer,程序员可以在 WinForm 程序中加载并查看 PDF 文档。浏览 PDF 时,用户可以自由设置浏览选项,比如:自动适应纸张/自动适应窗口,上一页/下一页,放大/缩小,等等。Free Spire.PDFViewer 支持浏览含多种元素的 PDF,这些元素包括超链接,字体(TrueType, Type 0, Type 1, Type 3, OpenType and CJK font),图形,表格, Device-Dependent Color Spaces, DCT,JPEG2000 等图片格式。 此外,该控件还支持纵向、横向和自动打印文档,支持将 PDF 保存为多种流行的图片格式(.bmp, .png, .jpeg)。作为一款独立的 PDF 查看组件,Free Spire.PDFViewer 的运行不依赖 Adobe Reader 及其他任何第三方软件。 主要功能点: 1.从文件,流,字节数组读取文档 2.加载浏览加密的 PDF 文档 3.设置浏览选项(页面跳,缩放,自适应页面大小,旋,单页或多页显示) 4.显示缩略图 5.识别书签目录及跳到目标位置 6.存取附件到本地硬盘 7.支持多种打印方式:横向,纵向,自动 8.保存为图片(.bmp, .png, .jpeg) 9.支持多种PDF页面元素(超链接,字体,图形,图片,表格) 技术特点: 1.支持 .NET Framework 2.0, 3.0, 3.5, 4.0 2.仅支持 Windows Form 3.支持ASCIIHex, ASCII85, Flate, LZW, Run Length, CCITT Fax, DCT, JPX 解码 4.控件完全由 C# 代码开发 5.独立组件,无需 Adobe Reader 及其他任何第三方软件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值