1、安装引用Aspose.Html
1)使用Nuget界面管理器
直接分别搜索 "Aspose.Html",找到对应的点安装即可。
相关文档:VS(Visual Studio)中Nuget的使用
2)使用Package Manager命令安装
PM> Install-Package Aspose.Html
注意:使用Aspose是需要有License的,如没有也可以网上找一下破解版本。
2、使用Aspose.Html将HTML转成PDF
使用Aspose.HTML for .NET 提供了Converter
类的静态方法,可以很方便的将HTML转换成PDF,代码如下,
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;using Aspose.Html.Converters;using Aspose.Html.Saving;namespace ConsoleApplication{ class Program { public static string outputFile { get; private set; } static void Main(string[] args) { //var license = new Aspose.Html.License(); //license.SetLicense("Aspose.Html.lic"); Converter.ConvertHTML(@"Convert HTML to PDF!
", ".", new PdfSaveOptions(), Path.Combine("/home/wonhero", "convert-with-single-line.pdf")); Console.ReadKey(); } }}
3、使用Aspose.Html将URL中HTML转换PDF
使用Aspose.Html中HTMLDocument
可以直接指定URL,将URL中HTML转换PDF,代码如下,
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Net;using System.Net.Http;using System.Text;using System.Threading.Tasks;using Aspose.Html;using Aspose.Html.Converters;using Aspose.Html.Dom;using Aspose.Html.Saving;namespace ConsoleApplication{ class Program { private static Stream GetContentFromUrlAsStream(string url, ICredentials credentials = null) { using (var handler = new HttpClientHandler { Credentials = credentials }) using (var httpClient = new HttpClient(handler)) { return httpClient.GetStreamAsync(url).GetAwaiter().GetResult(); } } static void Main(string[] args) { //var license = new Aspose.Html.License(); //license.SetLicense("Aspose.Html.lic"); const string url = "https://en.wikipedia.org/wiki/Aspose_API"; var document = new HTMLDocument(url); Converter.ConvertHTML(document, new PdfSaveOptions(),"/home/wonhero/output.pdf"); Console.ReadKey(); } }}