网页是动态页面,网页带背景图和图片。现在只能将普通文本转化出来。求助!!!!
string fileNameWithOutExtention = HttpContext.Current.Server.MapPath("..\Content\pdf\" + Guid.NewGuid() + ".pdf");
string wkhtmltopdfPath = HttpContext.Current.Server.MapPath("..\App_Start\wkhtmltopdf.exe");
if (string.IsNullOrEmpty(html))
{
return "false";
}
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();
}
process.WaitForExit();
}
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();
}
else
{
throw new Exception("文件不存在!");
}
return fileNameWithOutExtention;