翻译|使用教程|编辑:胡涛|2023-06-15 11:08:51.510|阅读 243 次
概述:本文将演示如何使用Spire.PDF for .NET使用或不使用 QT 插件将HTML 网页(URL)或HTML 字符串呈现为 PDF 文档。
# 界面/图表报表/文档/IDE等千款热门软控件火热销售中 >>
相关链接:
Spire.Doc 是一款专门对 Word 文档进行操作的 类库。在于帮助开发人员无需安装 Microsoft Word情况下,轻松快捷高效地创建、编辑、转换和打印 Microsoft Word 文档。拥有近10年专业开发经验Spire系列办公文档开发工具,专注于创建、编辑、转换和打印Word/PDF/Excel等格式文件处理,小巧便捷。
E-iceblue 功能类库Spire 系列文档处理组件均由中国本土团队研发,不依赖第三方软件,不受其他国家的技术或法律法规限制,同时适配国产操作系统如中科方德、中标麒麟等,兼容国产文档处理软件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下载 Spire.PDF for java下载
将 HTML 内容转换为 PDF 具有许多优势,包括能够离线阅读,以及以高保真度保留内容和格式。Spire.PDF提供了两种HTML转PDF的方法,一种是使用QT Web插件,另一种是不使用插件。我们建议您使用 QT 插件进行转换。
以下部分演示了如何使用Spire.PDF for .NET使用或不使用 QT 插件将HTML 网页(URL)或HTML 字符串呈现为 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 插件的 Spire.PDF 将 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 = "//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 插件的 Spire.PDF 将 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
以下是使用无插件的 Spire.PDF 将 URL 转换为 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 = "//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
以下是使用无插件的 Spire.PDF 将 HTML 字符串转换为 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,如果您有其他问题也可以继续浏览本系列文章,获取相关教程,你还可以给我留言或者加入我们的官方技术交流群。
欢迎下载|体验更多E-iceblue产品
获取更多信息请咨询 ;技术交流Q群(767755948)
本站文章除注明转载外,均为本站原创或翻译。欢迎任何形式的转载,但请务必注明出处、不得修改原文相关链接,如果存在内容上的异议请邮件反馈至chenjj@ke049m.cn