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。
插件下载: ,下载插件后,需要将 "plugins" 文件夹拷贝到 spire.pdf.dll 所在的文件夹下。
注意:对于 mac 和 linux 环境下的 qt 插件包,为了避免权限问题引起 plugins 包访问不到,需先将压缩包原封不动的拷贝系统里,然后在系统中再进行解压使用里面的 plugins 包;使用 qt 插件转换 html to pdf 前请确保电脑上安装了 microsoft visual c 2015 redistributable。
示例下载:c# 和 vb.net
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 =@"this is a test for converting html string to pdf
- spire.pdf supports to convert html in url into pdf
- spire.pdf supports to convert html string into pdf
- with the new plugin
";
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 = "this is a test for converting html string to pdf " & vbcr & vblf & " - spire.pdf supports to convert html in url into pdf
" & vbcr & vblf & " - spire.pdf supports to convert html string into pdf
" & vbcr & vblf & " - with the new plugin
"
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截图展示: