Spire.PDF支持将HTML转换成 PDF 格式。本文将详细介绍如何使用Spire.PDF提供的两种方式实现该转换。
普通方法
1、转换HTML URL到PDF
C#
//创建PDF文档
PdfDocument doc = new PdfDocument();
//加载HTML URL
String url = "https://www.e-iceblue.cn/";
Thread thread = new Thread(() =>
{ doc.LoadFromHTML(url, false, true, true); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
//保存为Pdf文档
doc.SaveToFile("sample.pdf");
VB.NET
'创建PDF文档
Dim doc As New PdfDocument()
'加载HTML URL
Dim url As [String] = "https://www.e-iceblue.cn/"
Dim thread As New Thread(Function()
doc.LoadFromHTML(url, False, True, True)
End Function)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
'保存为Pdf文档
doc.SaveToFile("sample.pdf")
2、转换HTML字符串到PDF
C#
//创建PDF文档
PdfDocument pdf = new PdfDocument();
//设置页面布局
PdfHtmlLayoutFormat htmlLayoutFormat = new PdfHtmlLayoutFormat();
//不等待加载
htmlLayoutFormat.IsWaiting = false;
//页面设置
PdfPageSettings setting = new PdfPageSettings();
setting.Size = PdfPageSize.A4;
//加载 HTML代码
string htmlCode = File.ReadAllText("C:\\..\\冰蓝科技 e-iceblue.html");
//使用单线程加载HTML代码
Thread thread = new Thread(() =>
{ pdf.LoadFromHTML(htmlCode, false, setting, htmlLayoutFormat); });
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
//保存为Pdf文档
pdf.SaveToFile("sample.pdf");
VB.NET
'创建PDF文档
Dim pdf As New PdfDocument()
'设置页面布局
Dim htmlLayoutFormat As New PdfHtmlLayoutFormat()
'不等待加载
htmlLayoutFormat.IsWaiting = False
'页面设置
Dim setting As New PdfPageSettings()
setting.Size = PdfPageSize.A4
'加载 HTML代码
Dim htmlCode As String = File.ReadAllText("C:\..\冰蓝科技 e-iceblue.html")
'使用单线程加载HTML代码
Dim thread As New Thread(Function()
pdf.LoadFromHTML(htmlCode, False, setting, htmlLayoutFormat)
End Function)
thread.SetApartmentState(ApartmentState.STA)
thread.Start()
thread.Join()
'保存为Pdf文档
pdf.SaveToFile("sample.pdf")
生成Pdf截图展示:
插件方法
Spire.PDF 还提供插件进行转换,该方式支持转换更多的 HTML 元素,如 HTTPS,CSS3,HTML5,JavaScript。
插件下载: windows_x86.zip windows_x64.zip macosx_x64.zip linux_x64.tar.gz,下载插件后,需要将 "plugins" 文件夹拷贝到 Spire.Pdf.dll 所在的文件夹下。
注意:对于 mac 和 linux 环境下的 qt 插件包,为了避免权限问题引起 plugins 包访问不到,需先将压缩包原封不动的拷贝系统里,然后在系统中再进行解压使用里面的 plugins 包;使用 QT 插件转换 HTML to PDF 前请确保电脑上安装了 Microsoft Visual C++ 2015 Redistributable。
示例下载:C# HtmlToPdf.zip 和 VB.NET HtmlToPdfVB.zip
1、转换HTML URL到PDF
C#
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert ("http://www.e-iceblue.cn/about-us.html/", "HTMLtoPDF.pdf",
//启用javascript
true,
//加载超时
100 * 1000,
//页面大小
new SizeF(612, 792),
//页边距
new PdfMargins(0, 0));
System.Diagnostics.Process.Start("HTMLtoPDF.pdf");
VB.NET
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert("http://www.e-iceblue.cn/about-us.html/", "HTMLtoPDF.pdf", True, 100 * 1000, New SizeF(612, 792), New PdfMargins(0, 0))
System.Diagnostics.Process.Start("HTMLtoPDF.pdf")
生成Pdf截图展示:
2、转换HTML 字符串到PDF
C#
string input =@"<strong>This is a test for converting HTML string to PDF </strong>
<ul><li>Spire.PDF supports to convert HTML in URL into PDF</li>
<li>Spire.PDF supports to convert HTML string into PDF</li>
<li>With the new plugin</li></ul>";
string outputFile = @"C:\ToPDF.pdf";
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(input,
outputFile,
//启用javascript
true,
//加载超时
10 * 1000,
//页面大小
new SizeF(612, 792),
//页边距
new Spire.Pdf.Graphics.PdfMargins(0),
//加载类型
LoadHtmlType.SourceCode);
System.Diagnostics.Process.Start(outputFile);
VB.NET
Dim input As String = "<strong>This is a test for converting HTML string to PDF </strong>" & vbCr & vbLf & " <ul><li>Spire.PDF supports to convert HTML in URL into PDF</li>" & vbCr & vbLf & " <li>Spire.PDF supports to convert HTML string into PDF</li>" & vbCr & vbLf & " <li>With the new plugin</li></ul>"
Dim outputFile As String = "C:\ToPDF.pdf"
Spire.Pdf.HtmlConverter.Qt.HtmlConverter.Convert(input, outputFile, True, 10 * 1000, New SizeF(612, 792), New Spire.Pdf.Graphics.PdfMargins(0), LoadHtmlType.SourceCode)
System.Diagnostics.Process.Start(outputFile)
生成Pdf截图展示: