中转方式:
大多数时候,都是使用WebBrowser空间,将网页截图,然后插入PDF文件,另外也可以将网页先保存为Word文件,然后再从Word转PDF。
PDF虚拟打印机:
还有一种方式,就是使用虚拟打印机,安装一个PDF虚拟打印机,将它设置成默认打印机,比如PDF-XChange,将它打印到文件,打印的效果很好,但没法代码控制,始终会弹出一个打印对话框。
HTML2PDF:
另外,还可以使用HTML2PDF-X Pilot,下面是介绍:
使用很简单,只需要给一个URL,下面是实例代码:
' Create HTML2PDF instance:
Set html2pdf_obj = CreateObject("html2pdfx.HTML2PDFX")
' Initiate engine:
html2pdf_obj.StartHTMLEngine "demo", "demo"
' Set PDF filename:
html2pdf_obj.OutputFileName = "URL_to_PDF.pdf"
' Set page margins:
html2pdf_obj.MarginLeft = 10
html2pdf_obj.MarginRight = 10
html2pdf_obj.MarginTop = 50
html2pdf_obj.MarginBottom = 50
html2pdf_obj.SetCustomPaperSize 420,594
' After PageTimeout (in sec) loading will stop and conversion will proceed
' with loaded content:
html2pdf_obj.PageTimeout = 40
' Start PDF generation:
html2pdf_obj.BeginDoc
' Define base URL for resolving relative links:
html2pdf_obj.BaseURL = "http://www.baidu.com"
' Load URL:
html2pdf_obj.LoadURL "http://www.baidu.com"
' Perform conversion:
html2pdf_obj.ConvertAll
' Finish PDF generation:
html2pdf_obj.EndDoc
Set html2pdf_obj = nothing
也可以添加引用到ASP.NET项目中使用。
使用该软件,需要购买授权,否则生成的PDF文件会有一个版权说明:
Demonstration use of HTML2PDF-X Pilot library.
If you have any questions regarding the work of the library, please ask.
ABCpdf.NET:
ABCpdf.NET是一个能够很方便生成pdf的.net组件,能够运行在以下操作系统中:Windows 2000, Windows XP, Windows Server 2003, Windows Vista, and Windows Server 2008.官方建议运行环境安装IE6或者以上版本。对应不同的系统,它有32位和64位的版本,使用时注意版本的选用。
ABCpdf的功能比较多,比如可以读word、excel等文件,可以保存pdf、xps、swf等格式文件。本文主要介绍其生成pdf的方法。使用时,需要ABCpdf.DLL和ABCpdfCE7.DLL支持。其中ABCpdf.DLL(.net调用接口)需要引用到项目中,ABCpdfCE7.DLL(核心驱动)放在ABCpdf.DLL的同一目录下即可。
详细内容,请参考下面的两篇文章:
http://www.cnblogs.com/yshlzy2005/archive/2010/1/21.html
在使用的时候,需要注册,下面是注册码:
Use 341-639-358 for a Standard License or
719-253-057 for a Professional License
注册后,需要重启计算机。
下面是一段ASP.NET实例代码:
Doc theDoc = new Doc(); //创建一个Doc对象
theDoc.Rect.Inset(24, 48);
//Rect默认是文档整个页面大小, 这里的Inset表示将Rect左右留出24的空白,上下留出48的空白
int theID = theDoc.AddImageUrl("具体的网页地址");
while (true)
{
if (!theDoc.Chainable(theID))
break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
theDoc.Save(Server.MapPath("1.pdf"));
注:
由于这些资源下载比较慢,大家需要的话,可以找我要安装文件。
网页转PDF方法
本文介绍了几种将网页转换为PDF文件的技术方案,包括使用WebBrowser空间截图、PDF虚拟打印机、HTML2PDF-XPilot库及ABCpdf.NET组件。通过这些工具可以实现网页内容的高质量输出。
1316





