将 HTML 转换为 PDF
Spire.PDF for .NET 是一款专门对 Word 文档进行操作的 .NET 类库。致力于在于帮助开发人员轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档,而无需安装 Microsoft Word。
行号用于在每行文本旁边显示 Word 自动计算的行数。当我们需要参考合同或法律文件等文档中的特定行时,它非常有用。word中的行号功能允许我们设置起始值、编号间隔、与文本的距离以及行号的编号方式。使用 Spire.Doc,我们可以实现上述所有功能。本文将介绍如何将 HTML 转换为 PDF。
欢迎加入spire技术交流群:767755948
将 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
下载插件
如果您选择插件方式,请从以下链接下载适合您操作系统的插件。- Windows x86
- Windows x64
- Linux x64
- Mac x64
此外,我们建议您将项目的“Platform Objectives”相应地设置为 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 文档。
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 = "//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 = "//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 文件。
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 = "//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 = "//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 文件。
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

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线