將 HTML 轉(zhuǎn)換為 PDF
Spire.PDF for .NET 是一款專門對 Word 文檔進(jìn)行操作的 .NET 類庫。致力于在于幫助開發(fā)人員輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔,而無需安裝 Microsoft Word。
行號用于在每行文本旁邊顯示 Word 自動計(jì)算的行數(shù)。當(dāng)我們需要參考合同或法律文件等文檔中的特定行時(shí),它非常有用。word中的行號功能允許我們設(shè)置起始值、編號間隔、與文本的距離以及行號的編號方式。使用 Spire.Doc,我們可以實(shí)現(xiàn)上述所有功能。本文將介紹如何將 HTML 轉(zhuǎn)換為 PDF。
歡迎加入spire技術(shù)交流群:767755948
將 HTML 內(nèi)容轉(zhuǎn)換為 PDF 具有許多優(yōu)勢,包括能夠離線閱讀,以及以高保真度保留內(nèi)容和格式。Spire.PDF提供了兩種HTML轉(zhuǎn)PDF的方法,一種是使用QT Web插件,另一種是不使用插件。我們建議您使用 QT 插件進(jìn)行轉(zhuǎn)換。
以下部分演示了如何使用Spire.PDF for .NET使用或不使用 QT 插件將HTML 網(wǎng)頁(URL)或HTML 字符串呈現(xiàn)為 PDF 文檔。
- 使用 QT 插件將 URL 轉(zhuǎn)換為 PDF
- 使用 QT 插件將 HTML 字符串轉(zhuǎn)換為 PDF
- 無需插件即可將 URL 轉(zhuǎn)換為 PDF
- 不使用插件將 HTML 字符串轉(zhuǎn)換為 PDF
	安裝適用于 .NET 的 Spire.PDF
首先,您需要將包含在 Spire.PDF for.NET 包中的 DLL 文件添加為您的 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過NuGet安裝
PM> Install-Package Spire.PDF
下載插件
如果您選擇插件方式,請從以下鏈接下載適合您操作系統(tǒng)的插件。- Windows x86
- Windows x64
- Linux x64
- Mac x64
		 
 
	
此外,我們建議您將項(xiàng)目的“Platform Objectives”相應(yīng)地設(shè)置為 x64 或 x86。
		 
 
	
使用 QT 插件將 URL 轉(zhuǎn)換為 PDF
以下是使用帶 QT 插件的 Spire.PDF 將 URL 轉(zhuǎn)換為 PDF 的步驟。- 指定要轉(zhuǎn)換的 URL 路徑。
- 指定生成的 PDF 文件的路徑。
- 指定插件路徑,并將其分配為HtmlConverter.PluginPath屬性的值。
- 調(diào)用HtmlConverter.Convert(string url, string fileName, bool enableJavaScript, int timeout, SizeF pageSize, PdfMargins margins)方法將 URL 轉(zhuǎn)換為 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 = "http://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 = "http://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 字符串轉(zhuǎn)換為 PDF
以下是使用帶 QT 插件的 Spire.PDF 將 HTML 字符串轉(zhuǎn)換為 PDF 的步驟。- 從 .html 文件中獲取 HTML 字符串。
- 指定生成的 PDF 文件的路徑。
- 指定插件路徑,并將其分配為HtmlConverter.PluginPath屬性的值。
- 調(diào)用HtmlConverter.Convert(string htmlString, string fileName, bool enableJavaScript, int timeout, SizeF pageSize, PdfMargins margins, Spire.Pdf.HtmlConverter.LoadHtmlType htmlType)方法將 HTML 字符串轉(zhuǎn)換為 PDF 文檔。
注意:只有內(nèi)聯(lián) CSS 樣式和內(nèi)部 CSS 樣式才能在 PDF 上正確呈現(xiàn)。如果您有外部 CSS 樣式表,請將其轉(zhuǎn)換為內(nèi)聯(lián)或內(nèi)部 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 轉(zhuǎn)換為 PDF
以下是使用無插件的 Spire.PDF 將 URL 轉(zhuǎn)換為 PDF 的步驟。- 創(chuàng)建一個(gè)PdfDocument對象。
- 創(chuàng)建一個(gè)PdfPageSettings對象,并通過它設(shè)置頁面大小和邊距。
- 創(chuàng)建一個(gè)PdfHtmlLayoutFormat對象,并將其IsWaiting屬性設(shè)置為 true。
- 指定要轉(zhuǎn)換的 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 = "http://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 = "http://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 字符串轉(zhuǎn)換為 PDF
以下是使用無插件的 Spire.PDF 將 HTML 字符串轉(zhuǎn)換為 PDF 的步驟。- 創(chuàng)建一個(gè)PdfDocument對象。
- 創(chuàng)建一個(gè)PdfPageSettings對象,并通過它設(shè)置頁面大小和邊距。
- 創(chuàng)建一個(gè)PdfHtmlLayoutFormat對象,并將其IsWaiting屬性設(shè)置為 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交談
QQ交談 在線咨詢
在線咨詢 
                 
                
 渝公網(wǎng)安備
            50010702500608號
渝公網(wǎng)安備
            50010702500608號
             
            
 客服熱線
客服熱線