翻譯|使用教程|編輯:胡濤|2023-06-15 11:08:51.510|閱讀 243 次
概述:本文將演示如何使用Spire.PDF for .NET使用或不使用 QT 插件將HTML 網(wǎng)頁(yè)(URL)或HTML 字符串呈現(xiàn)為 PDF 文檔。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
Spire.Doc 是一款專(zhuān)門(mén)對(duì) Word 文檔進(jìn)行操作的 類(lèi)庫(kù)。在于幫助開(kāi)發(fā)人員無(wú)需安裝 Microsoft Word情況下,輕松快捷高效地創(chuàng)建、編輯、轉(zhuǎn)換和打印 Microsoft Word 文檔。擁有近10年專(zhuān)業(yè)開(kāi)發(fā)經(jīng)驗(yàn)Spire系列辦公文檔開(kāi)發(fā)工具,專(zhuān)注于創(chuàng)建、編輯、轉(zhuǎn)換和打印Word/PDF/Excel等格式文件處理,小巧便捷。
E-iceblue 功能類(lèi)庫(kù)Spire 系列文檔處理組件均由中國(guó)本土團(tuán)隊(duì)研發(fā),不依賴(lài)第三方軟件,不受其他國(guó)家的技術(shù)或法律法規(guī)限制,同時(shí)適配國(guó)產(chǎn)操作系統(tǒng)如中科方德、中標(biāo)麒麟等,兼容國(guó)產(chǎn)文檔處理軟件 WPS(如 .wps/.et/.dps 等格式
Spire.PDF for.net下載 Spire.PDF for java下載
將 HTML 內(nèi)容轉(zhuǎn)換為 PDF 具有許多優(yōu)勢(shì),包括能夠離線(xiàn)閱讀,以及以高保真度保留內(nèi)容和格式。Spire.PDF提供了兩種HTML轉(zhuǎn)PDF的方法,一種是使用QT Web插件,另一種是不使用插件。我們建議您使用 QT 插件進(jìn)行轉(zhuǎn)換。
以下部分演示了如何使用Spire.PDF for .NET使用或不使用 QT 插件將HTML 網(wǎng)頁(yè)(URL)或HTML 字符串呈現(xiàn)為 PDF 文檔。
首先,您需要將包含在 Spire.PDF for.NET 包中的 DLL 文件添加為您的 .NET 項(xiàng)目中的引用。DLL 文件可以從此鏈接下載或通過(guò)NuGet安裝
PM> Install-Package Spire.PDF
如果您選擇插件方式,請(qǐng)從以下鏈接下載適合您操作系統(tǒng)的插件。
將包解壓縮到磁盤(pán)上的某個(gè)位置以獲取“plugins”文件夾。在本例中,我們將插件保存在“F:\Libraries\Plugin\plugins-windows-x64\plugins”路徑下。
此外,我們建議您將項(xiàng)目的“平臺(tái)目標(biāo)”相應(yīng)地設(shè)置為 x64 或 x86。
以下是使用帶 QT 插件的 Spire.PDF 將 URL 轉(zhuǎn)換為 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 字符串轉(zhuǎn)換為 PDF 的步驟。
注意:只有內(nèi)聯(lián) CSS 樣式和內(nèi)部 CSS 樣式才能在 PDF 上正確呈現(xiàn)。如果您有外部 CSS 樣式表,請(qǐng)將其轉(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
以下是使用無(wú)插件的 Spire.PDF 將 URL 轉(zhuǎn)換為 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
以下是使用無(wú)插件的 Spire.PDF 將 HTML 字符串轉(zhuǎn)換為 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 轉(zhuǎn)換為 PDF,如果您有其他問(wèn)題也可以繼續(xù)瀏覽本系列文章,獲取相關(guān)教程,你還可以給我留言或者加入我們的官方技術(shù)交流群。
歡迎下載|體驗(yàn)更多E-iceblue產(chǎn)品
獲取更多信息請(qǐng)咨詢(xún) ;技術(shù)交流Q群(767755948)
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@ke049m.cn