翻譯|使用教程|編輯:胡濤|2023-06-15 11:08:51.510|閱讀 222 次
概述:本文將演示如何使用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 = "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 插件的 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 = "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
以下是使用無插件的 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@fc6vip.cn