C# 生转换网页为pdf c#pdf 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 iTextSharp 控件可以,在网上搜索一下,以前用过很灵活的 支持1楼的 http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp楼主可以看看 谢谢,有点看不懂啊, document.Close();之后,PDF文件保存在哪里呢,没有看到哪里有保存路径啊 搜索wkhtmltopdf.exe 下载然后参考我在项目中使用的代码.还有记得给分- - using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Text;using System.Diagnostics;using System.IO;namespace AK.Scorpion.HtmlToPdf{ public class HtmlToPdf { /// <summary> /// HTML转换PDF /// </summary> /// <param name="html">Hmtl字符串</param> public static void ConvertHtmlToPdf(string html) { if (string.IsNullOrEmpty(html)) { return; } else { string fileNameWithOutExtention = HttpContext.Current.Server.MapPath(@"PDF\" + Guid.NewGuid() + ".pdf");//输出文件名称 string wkhtmltopdfPath = HttpContext.Current.Server.MapPath(@"wkhtmltopdf\wkhtmltopdf.exe"); if (!File.Exists(wkhtmltopdfPath)) { throw new Exception(String.Format("File '{0}' not found. Check if wkhtmltopdf application is installed.", wkhtmltopdfPath)); } ProcessStartInfo si; StringBuilder paramsBuilder = new StringBuilder(); paramsBuilder.Append("--page-size A4 "); paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", "-", fileNameWithOutExtention); si = new ProcessStartInfo(); si.CreateNoWindow = true; si.FileName = wkhtmltopdfPath; si.Arguments = paramsBuilder.ToString(); si.UseShellExecute = false; si.RedirectStandardError = true; si.RedirectStandardInput = true; using (var process = new Process()) { process.StartInfo = si; process.Start(); using (var stream = process.StandardInput) { byte[] buffer = Encoding.UTF8.GetBytes(html); stream.BaseStream.Write(buffer, 0, buffer.Length); stream.WriteLine(); } if (!process.WaitForExit(1000)) { throw new Exception("转换超时!"); } } if (File.Exists(fileNameWithOutExtention)) { //把文件读进文件流 FileStream fs = new FileStream(fileNameWithOutExtention, FileMode.Open); byte[] file = new byte[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 { throw new Exception("文件不存在!"); } } } }} 我这个是把HTML页面作为一个字符串来实现转换的 不知道你是如何 如果是页面转换PDF可以百度aspose.pdf 这个DLL文件 相关例子很多 iTextSharp网页转图片再导到PDF里 我用iTextSharp的时候导入程序集的时候总是报错, 大家讨论一下对于下面的问题的两种回答是否都是正确的: 请教 c# 有哪些比较有名的数据库简单操作Dll或控件,谢谢 谁有C#写的语音与视频的代码?高分相求! 这个正则表达式怎么写啊? 请问我这个程序是否有内存溢出? 点击一个treeview的父节点而不展开其子节点 用MAPX能否做出像MAPINFO那样整形,加节点的功能?? 开发人员操作数据库一般要注意哪些注意事项? C#中操作DBF提示“超出系统资源”问题 SOS:调用dll报找不到入口点的错误 ,谁能告诉我怎么解决 C#发送的组播,Android接收不到,求帮助。 C#语言问题请教,[Localizable(false)],NLog源码
http://www.codeproject.com/Questions/203481/HTML-convert-to-PDF-using-itextsharp
楼主可以看看
谢谢,有点看不懂啊, document.Close();之后,PDF文件保存在哪里呢,没有看到哪里有保存路径啊
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Diagnostics;
using System.IO;namespace AK.Scorpion.HtmlToPdf
{
public class HtmlToPdf
{
/// <summary>
/// HTML转换PDF
/// </summary>
/// <param name="html">Hmtl字符串</param>
public static void ConvertHtmlToPdf(string html)
{
if (string.IsNullOrEmpty(html))
{
return;
}
else
{
string fileNameWithOutExtention = HttpContext.Current.Server.MapPath(@"PDF\" + Guid.NewGuid() + ".pdf");//输出文件名称
string wkhtmltopdfPath = HttpContext.Current.Server.MapPath(@"wkhtmltopdf\wkhtmltopdf.exe");
if (!File.Exists(wkhtmltopdfPath))
{
throw new Exception(String.Format("File '{0}' not found. Check if wkhtmltopdf application is installed.", wkhtmltopdfPath));
} ProcessStartInfo si;
StringBuilder paramsBuilder = new StringBuilder();
paramsBuilder.Append("--page-size A4 ");
paramsBuilder.AppendFormat("\"{0}\" \"{1}\"", "-", fileNameWithOutExtention); si = new ProcessStartInfo();
si.CreateNoWindow = true;
si.FileName = wkhtmltopdfPath;
si.Arguments = paramsBuilder.ToString();
si.UseShellExecute = false;
si.RedirectStandardError = true;
si.RedirectStandardInput = true; using (var process = new Process())
{
process.StartInfo = si;
process.Start();
using (var stream = process.StandardInput)
{
byte[] buffer = Encoding.UTF8.GetBytes(html);
stream.BaseStream.Write(buffer, 0, buffer.Length);
stream.WriteLine();
}
if (!process.WaitForExit(1000))
{
throw new Exception("转换超时!");
}
} if (File.Exists(fileNameWithOutExtention))
{
//把文件读进文件流
FileStream fs = new FileStream(fileNameWithOutExtention, FileMode.Open);
byte[] file = new byte[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
{
throw new Exception("文件不存在!");
}
}
} }
}