C# wkhtmltopdf';访问被拒绝';在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo-startInfo)
使用wkhtmltopdf生成pdf它工作正常,但有时会抛出 “访问被拒绝”在 系统.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 例如:它一次生成20个文件,其中一个pdf文件抛出“拒绝访问”,并生成19个重新命名文件。大多数情况下,所有PDF都生成得很好。在这种情况下该怎么办C# wkhtmltopdf';访问被拒绝';在System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo-startInfo),c#,html,pdf,wkhtmltopdf,access-denied,C#,Html,Pdf,Wkhtmltopdf,Access Denied,使用wkhtmltopdf生成pdf它工作正常,但有时会抛出 “访问被拒绝”在 系统.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo) 例如:它一次生成20个文件,其中一个pdf文件抛出“拒绝访问”,并生成19个重新命名文件。大多数情况下,所有PDF都生成得很好。在这种情况下该怎么办 public byte[] WKHtmlToPdf(string url) { var file
public byte[] WKHtmlToPdf(string url)
{
var fileName = string.Format("{0}.pdf", Guid.NewGuid());
//var wkhtmlDir = "C:\\Program Files\\wkhtmltopdf\\";
//var wkhtml = "C:\\Program Files\\wkhtmltopdf\\wkhtmltopdf.exe";
var wkhtmlDir = HttpContext.Server.MapPath(@"~/wkhtmltopdf");
var wkhtml = HttpContext.Server.MapPath(@"~/wkhtmltopdf/wkhtmltopdf.exe");
var p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.FileName = wkhtml;
p.StartInfo.WorkingDirectory = wkhtmlDir;
string switches = "";
switches += "--print-media-type ";
switches += "--margin-top 10mm --margin-bottom 10mm --margin-right 10mm --margin-left 10mm ";
switches += "--page-size Letter ";
p.StartInfo.Arguments = switches + " " + url + " " + fileName;
p.Start();
// wait or exit
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
var outputFile = string.Format("{0}\\{1}", p.StartInfo.WorkingDirectory, fileName);
byte[] bytes = System.IO.File.ReadAllBytes(outputFile);
return returnCode == 0 ? bytes: null;
}
是前19个有效,后1个失败,还是随机的?它是随机的,当我再次运行它时,它工作正常