[英]Unable to generate PDF from HTML using wkhtmltopdf
我正在嘗試使用wkhtmltopdf將鏈接(例如www.google.com
)轉換為PDF(請參閱調用wkhtmltopdf從HTML生成PDF ),但是在這種情況下,我得到的錯誤是未生成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);
}
請幫我解決這個問題。
您應該指向實際的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";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.