我正在尝试使用wkhtmltopdf将链接(例如www.google.com
)转换为PDF(请参阅Calling wkhtmltopdf to generate PDF from HTML)但在此我收到的错误是pdf未生成但代码运行正常且返回0
作为回复。
代码:
public static bool HtmlToPdf(string Url, string outputFilename)
{
// assemble destination PDF file name
string filename = @"C:\Users\Documents\visual studio 2012\Projects\PenchKin\PenchKin\Setup\test.pdf";
// get proj no for header
//Project project = new Project(int.Parse(outputFilename));
var p = new System.Diagnostics.Process();
p.StartInfo.FileName = @"C:\Users\documents\visual studio 2012\Projects\PenchKin\PenchKin\Setup\wkhtmltopdf-0.9.9-installer.exe";
string switches = "--print-media-type ";
switches += "--margin-top 4mm --margin-bottom 4mm --margin-right 0mm --margin-left 0mm ";
switches += "--page-size A4 ";
switches += "--no-background ";
switches += "--redirect-delay 100";
p.StartInfo.Arguments = switches + " " + Url + " " + filename;
p.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none
p.StartInfo.WorkingDirectory = p.StartInfo.FileName;
p.Start();
// read the output here...
string output = p.StandardOutput.ReadToEnd();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
// if 0 or 2, it worked (not sure about other values, I want a better way to confirm this)
return (returnCode == 0 || returnCode == 2);
}
请帮我解决这个问题。
答案 0 :(得分:1)
您应该指向实际的exe
,而不是安装程序。替换
p.StartInfo.FileName = @"C:\Users\documents\visual studio 2012\Projects\PenchKin\PenchKin\Setup\wkhtmltopdf-0.9.9-installer.exe";
通过
p.StartInfo.FileName = @"C:\path\to\binary\wkhtmltopdf.exe";