这会对你有所帮助........尝试一次。
protected static byte[] Convert(string wkhtmlPath, string switches, string html, string wkhtmlExe)
{
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
html = SpecialCharsEncode(html);
}
var proc = new Process();
var StartInfo = new ProcessStartInfo();
proc.StartInfo.FileName = Path.Combine(wkhtmlPath, wkhtmlExe);
proc.StartInfo.Arguments = switches;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.WorkingDirectory = wkhtmlPath;
proc.Start();
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
using (var sIn = proc.StandardInput)
{
sIn.WriteLine(html);
}
}
var ms = new MemoryStream();
using (var sOut = proc.StandardOutput.BaseStream)
{
byte[] buffer = new byte[4096];
int read;
while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
string error = proc.StandardError.ReadToEnd();
if (ms.Length == 0)
{
throw new Exception(error);
}
proc.WaitForExit();
return ms.ToString();
}
/// <summary>
/// Encode all special chars
/// </summary>
/// <param name="text">Html text</param>
/// <returns>Html with special chars encoded</returns>
private static string SpecialCharsEncode(string text)
{
var chars = text.ToCharArray();
var result = new StringBuilder(text.Length + (int)(text.Length * 0.1));
foreach (var c in chars)
{
var value = System.Convert.ToInt32(c);
if (value > 127)
result.AppendFormat("&#{0};", value);
else
result.Append(c);
}
return result.ToString();
}
}
这里我们返回内存流,我们还需要添加header,内容类型
public override void ExecuteResult(ControllerContext context)
{
// this.FileName = context.RouteData.GetRequiredString("action");
var fileContent = Convert(wkhtmltopdfPath, switches, null, wkhtmlExe);
var response = this.PrepareResponse(context.HttpContext.Response);
response.OutputStream.Write(fileContent, 0, fileContent.Length);
}
protected HttpResponseBase PrepareResponse(HttpResponseBase response)
{
response.ContentType = this.GetContentType();
this.FileName = "YourFile.pdf";
if (!String.IsNullOrEmpty(this.FileName))
response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", SanitizeFileName(this.FileName)));
response.AddHeader("Content-Type", this.GetContentType());
return response;
}