问题描述
怎么把一个网页转成PDF文件,求助啊~~
解决方案
解决方案二:
iTextSharp控件可以,在网上搜索一下,以前用过很灵活的
解决方案三:
兄弟,有什么实例代码么,以前没用过,突然公司要我弄这个,头都大了啊~
解决方案四:
引用1楼liulang457的回复:
iTextSharp控件可以,在网上搜索一下,以前用过很灵活的
求助啊
解决方案五:
支持1楼的http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp楼主可以看看
解决方案六:
引用4楼zhouxingyu_kingstar的回复:
支持1楼的http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp楼主可以看看
谢谢,有点看不懂啊,document.Close();之后,PDF文件保存在哪里呢,没有看到哪里有保存路径啊
解决方案七:
搜索wkhtmltopdf.exe下载然后参考我在项目中使用的代码.还有记得给分--usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.IO;namespaceAK.Scorpion.HtmlToPdf{publicclassHtmlToPdf{///<summary>///HTML转换PDF///</summary>///<paramname="html">Hmtl字符串</param>publicstaticvoidConvertHtmlToPdf(stringhtml){if(string.IsNullOrEmpty(html)){return;}else{stringfileNameWithOutExtention=HttpContext.Current.Server.MapPath(@"PDF"+Guid.NewGuid()+".pdf");//输出文件名称stringwkhtmltopdfPath=HttpContext.Current.Server.MapPath(@"wkhtmltopdfwkhtmltopdf.exe");if(!File.Exists(wkhtmltopdfPath)){thrownewException(String.Format("File'{0}'notfound.Checkifwkhtmltopdfapplicationisinstalled.",wkhtmltopdfPath));}ProcessStartInfosi;StringBuilderparamsBuilder=newStringBuilder();paramsBuilder.Append("--page-sizeA4");paramsBuilder.AppendFormat(""{0}""{1}"","-",fileNameWithOutExtention);si=newProcessStartInfo();si.CreateNoWindow=true;si.FileName=wkhtmltopdfPath;si.Arguments=paramsBuilder.ToString();si.UseShellExecute=false;si.RedirectStandardError=true;si.RedirectStandardInput=true;using(varprocess=newProcess()){process.StartInfo=si;process.Start();using(varstream=process.StandardInput){byte[]buffer=Encoding.UTF8.GetBytes(html);stream.BaseStream.Write(buffer,0,buffer.Length);stream.WriteLine();}if(!process.WaitForExit(1000)){thrownewException("转换超时!");}}if(File.Exists(fileNameWithOutExtention)){//把文件读进文件流FileStreamfs=newFileStream(fileNameWithOutExtention,FileMode.Open);byte[]file=newbyte[fs.Length];fs.Read(file,0,file.Length);fs.Close();//Response给客户端下载HttpContext.Current.Response.Clear();HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+fileNameWithOutExtention);//强制下载HttpContext.Current.Response.ContentType="application/octet-stream";HttpContext.Current.Response.BinaryWrite(file);HttpContext.Current.Response.Flush();HttpContext.Current.Response.End();}else{thrownewException("文件不存在!");}}}}}
解决方案八:
我这个是把HTML页面作为一个字符串来实现转换的不知道你是如何如果是页面转换PDF可以百度aspose.pdf这个DLL文件相关例子很多
解决方案九:
iTextSharp
解决方案十:
引用6楼SomethingJack的回复:
搜索wkhtmltopdf.exe下载然后参考我在项目中使用的代码.还有记得给分--usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.IO;namespaceAK.Scorpion.HtmlToPdf{publicclassHtmlToPdf{///<summary>///HTML转换PDF///</summary>///<paramname="html">Hmtl字符串</param>publicstaticvoidConvertHtmlToPdf(stringhtml){if(string.IsNullOrEmpty(html)){return;}else{stringfileNameWithOutExtention=HttpContext.Current.Server.MapPath(@"PDF"+Guid.NewGuid()+".pdf");//输出文件名称stringwkhtmltopdfPath=HttpContext.Current.Server.MapPath(@"wkhtmltopdfwkhtmltopdf.exe");if(!File.Exists(wkhtmltopdfPath)){thrownewException(String.Format("File'{0}'notfound.Checkifwkhtmltopdfapplicationisinstalled.",wkhtmltopdfPath));}ProcessStartInfosi;StringBuilderparamsBuilder=newStringBuilder();paramsBuilder.Append("--page-sizeA4");paramsBuilder.AppendFormat(""{0}""{1}"","-",fileNameWithOutExtention);si=newProcessStartInfo();si.CreateNoWindow=true;si.FileName=wkhtmltopdfPath;si.Arguments=paramsBuilder.ToString();si.UseShellExecute=false;si.RedirectStandardError=true;si.RedirectStandardInput=true;using(varprocess=newProcess()){process.StartInfo=si;process.Start();using(varstream=process.StandardInput){byte[]buffer=Encoding.UTF8.GetBytes(html);stream.BaseStream.Write(buffer,0,buffer.Length);stream.WriteLine();}if(!process.WaitForExit(1000)){thrownewException("转换超时!");}}if(File.Exists(fileNameWithOutExtention)){//把文件读进文件流FileStreamfs=newFileStream(fileNameWithOutExtention,FileMode.Open);byte[]file=newbyte[fs.Length];fs.Read(file,0,file.Length);fs.Close();//Response给客户端下载HttpContext.Current.Response.Clear();HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+fileNameWithOutExtention);//强制下载HttpContext.Current.Response.ContentType="application/octet-stream";HttpContext.Current.Response.BinaryWrite(file);HttpContext.Current.Response.Flush();HttpContext.Current.Response.End();}else{thrownewException("文件不存在!");}}}}}
为什么用你这个会出现这种问题啊~求助,大神。
解决方案十一:
iTextSharp网页转图片再导到PDF里
解决方案十二:
引用10楼wangqi7719435的回复:
iTextSharp网页转图片再导到PDF里
我用iTextSharp的时候导入程序集的时候总是报错,
解决方案十三:
引用9楼luoyingzs的回复:
Quote: 引用6楼SomethingJack的回复:
搜索wkhtmltopdf.exe下载然后参考我在项目中使用的代码.还有记得给分--usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.IO;namespaceAK.Scorpion.HtmlToPdf{publicclassHtmlToPdf{///<summary>///HTML转换PDF///</summary>///<paramname="html">Hmtl字符串</param>publicstaticvoidConvertHtmlToPdf(stringhtml){if(string.IsNullOrEmpty(html)){return;}else{stringfileNameWithOutExtention=HttpContext.Current.Server.MapPath(@"PDF"+Guid.NewGuid()+".pdf");//输出文件名称stringwkhtmltopdfPath=HttpContext.Current.Server.MapPath(@"wkhtmltopdfwkhtmltopdf.exe");if(!File.Exists(wkhtmltopdfPath)){thrownewException(String.Format("File'{0}'notfound.Checkifwkhtmltopdfapplicationisinstalled.",wkhtmltopdfPath));}ProcessStartInfosi;StringBuilderparamsBuilder=newStringBuilder();paramsBuilder.Append("--page-sizeA4");paramsBuilder.AppendFormat(""{0}""{1}"","-",fileNameWithOutExtention);si=newProcessStartInfo();si.CreateNoWindow=true;si.FileName=wkhtmltopdfPath;si.Arguments=paramsBuilder.ToString();si.UseShellExecute=false;si.RedirectStandardError=true;si.RedirectStandardInput=true;using(varprocess=newProcess()){process.StartInfo=si;process.Start();using(varstream=process.StandardInput){byte[]buffer=Encoding.UTF8.GetBytes(html);stream.BaseStream.Write(buffer,0,buffer.Length);stream.WriteLine();}if(!process.WaitForExit(1000)){thrownewException("转换超时!");}}if(File.Exists(fileNameWithOutExtention)){//把文件读进文件流FileStreamfs=newFileStream(fileNameWithOutExtention,FileMode.Open);byte[]file=newbyte[fs.Length];fs.Read(file,0,file.Length);fs.Close();//Response给客户端下载HttpContext.Current.Response.Clear();HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+fileNameWithOutExtention);//强制下载HttpContext.Current.Response.ContentType="application/octet-stream";HttpContext.Current.Response.BinaryWrite(file);HttpContext.Current.Response.Flush();HttpContext.Current.Response.End();}else{thrownewException("文件不存在!");}}}}}为什么用你这个会出现这种问题啊~求助,大神。
你不需要下载看看文件
解决方案十四:
引用12楼SomethingJack的回复:
Quote: 引用9楼luoyingzs的回复:
Quote: 引用6楼SomethingJack的回复:
搜索wkhtmltopdf.exe下载然后参考我在项目中使用的代码.还有记得给分--usingSystem;usingSystem.Data;usingSystem.Configuration;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Text;usingSystem.Diagnostics;usingSystem.IO;namespaceAK.Scorpion.HtmlToPdf{publicclassHtmlToPdf{///<summary>///HTML转换PDF///</summary>///<paramname="html">Hmtl字符串</param>publicstaticvoidConvertHtmlToPdf(stringhtml){if(string.IsNullOrEmpty(html)){return;}else{stringfileNameWithOutExtention=HttpContext.Current.Server.MapPath(@"PDF"+Guid.NewGuid()+".pdf");//输出文件名称stringwkhtmltopdfPath=HttpContext.Current.Server.MapPath(@"wkhtmltopdfwkhtmltopdf.exe");if(!File.Exists(wkhtmltopdfPath)){thrownewException(String.Format("File'{0}'notfound.Checkifwkhtmltopdfapplicationisinstalled.",wkhtmltopdfPath));}ProcessStartInfosi;StringBuilderparamsBuilder=newStringBuilder();paramsBuilder.Append("--page-sizeA4");paramsBuilder.AppendFormat(""{0}""{1}"","-",fileNameWithOutExtention);si=newProcessStartInfo();si.CreateNoWindow=true;si.FileName=wkhtmltopdfPath;si.Arguments=paramsBuilder.ToString();si.UseShellExecute=false;si.RedirectStandardError=true;si.RedirectStandardInput=true;using(varprocess=newProcess()){process.StartInfo=si;process.Start();using(varstream=process.StandardInput){byte[]buffer=Encoding.UTF8.GetBytes(html);stream.BaseStream.Write(buffer,0,buffer.Length);stream.WriteLine();}if(!process.WaitForExit(1000)){thrownewException("转换超时!");}}if(File.Exists(fileNameWithOutExtention)){//把文件读进文件流FileStreamfs=newFileStream(fileNameWithOutExtention,FileMode.Open);byte[]file=newbyte[fs.Length];fs.Read(file,0,file.Length);fs.Close();//Response给客户端下载HttpContext.Current.Response.Clear();HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+fileNameWithOutExtention);//强制下载HttpContext.Current.Response.ContentType="application/octet-stream";HttpContext.Current.Response.BinaryWrite(file);HttpContext.Current.Response.Flush();HttpContext.Current.Response.End();}else{thrownewException("文件不存在!");}}}}}为什么用你这个会出现这种问题啊~求助,大神。
你不需要下载看看文件
不需要下载?看看文件是什么意思啊。
解决方案十五:
楼主,问题解决了么?
解决方案:
我现在也遇到了这个问题