我之前曾成功使用过 wkhtmltopdf,但现在我有一个场景,即在启动该过程时需要使用特定帐户。当我设置有效的用户名/密码时,标准输出流为空,返回码为-1。一旦我注释掉用户名/密码,它就会按预期工作。
在 .Net 4、Win 7 64 位中测试此功能。
class Program
{
static void Main(string[] args)
{
var wkhtmlDir = AppDomain.CurrentDomain.BaseDirectory;
var wkhtml = wkhtmlDir + @"\wkhtmltopdf.exe";
var info = new ProcessStartInfo(wkhtml);
info.CreateNoWindow = true;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.UseShellExecute = false;
info.WorkingDirectory = wkhtmlDir;
info.Arguments = "http://www.google.com -";
var securePassword = new SecureString();
var password = "mypassword";
foreach (var c in password)
{
securePassword.AppendChar(c);
}
//comment out next three lines, and it works!
info.UserName = "myuser";
info.Password = securePassword;
info.Domain = "mydomain";
using (var process = Process.Start(info))
{
var output = process.StandardOutput.ReadToEnd();
// wait or exit
process.WaitForExit(60000);
var returnCode = process.ExitCode;
}
}
如果我注释掉 info.UserName、Password、Domain,输出有数据,否则如果我尝试使用凭据,输出为空且 returnCode 为 -1。
希望其他人也遇到过这种情况,这似乎是一个常见的情况,我肯定错过了一些简单的东西......
感谢您的帮助!!
最佳答案
我还在 .net 应用程序中使用 WKHTMLTOPDF。我发现的一些可能对您有帮助的事情是:
- wkhtmltopdf 进程的输出被放入 StandardError,而不是 StandardOutput。所以你应该重定向 StandardError 流而不是 StandardOutput
- 像您所做的那样重定向 standardError 和 StandardOutput 可能会(根据我对 wkhtmltopdf 的经验)会导致您的应用程序出现死锁。请参阅此页面以了解有关原因的更多信息 http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
在您的情况下,您可能会遇到写入错误并导致死锁的问题?
您可以将进程设置为异步读取标准错误和标准输出,就像我在下面的示例中所做的那样。我希望这有帮助
sub doConversion()
dim p as System.Diagnostics.Process = new System.Diagnostics.Process()
p.StartInfo.FileName = "wkhtmltopdf.exe"
dim url as string = "[[[YOUR URL TO CONVERT]]]"
dim outfilename as string = "[[[WHERE YOU WANT THE FILE]]]"
dim switches as string = ""
switches &= "--disable-smart-shrinking --print-media-type "
switches &= "--margin-top 0mm --margin-bottom 0mm --margin-right 0mm --margin-left 0mm "
switches &= "--page-size A4 "
p.StartInfo.Arguments = switches & Url & " " & outfilename
console.writeline("Running command: " & commandToRun & " " & switches & Url & " " & outfilename)
'## needs to be false in order to redirect output
p.StartInfo.UseShellExecute = false
p.StartInfo.RedirectStandardOutput = true
AddHandler p.OutputDataReceived, addressOf PDFOutputHandler
p.StartInfo.RedirectStandardError = true
AddHandler p.ErrorDataReceived, addressOf PDFOutputHandler
p.StartInfo.WorkingDirectory = rootPath & iif(rootpath.endswith("\"),"","\")
console.writeline("Starting...")
try
p.Start()
p.BeginOutputReadLine()
p.BeginErrorReadLine()
console.writeline("Started...")
catch ex as exception
throw new exception("Could not start process [" & p.startInfo.Filename & "] with arguments [" & p.startInfo.Arguments & "], " & vbcrlf & "(RootPath: " & rootpath & ")" & vbcrlf & ex.tostring & vbcrlf)
end try
'## ...then wait for exit
p.WaitForExit()
end sub
Private Sub PDFOutputHandler(sendingProcess As Object, outLine As DataReceivedEventArgs)
If Not String.IsNullOrEmpty(outLine.Data) Then
console.writeline(outLine.Data)
End If
End Sub
关于c# - 使用 ProcessInfo 用户名和密码调用 wkhtmltopdf 时无法读取 StandardOutput,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10214995/