2012-04-18 15 views
2

我以前成功地使用過wkhtmltopdf,但現在我有一個場景,我需要在啓動過程時使用特定的帳戶。當我設置一個有效的用戶名/密碼時,標準輸出流爲空,返回碼爲-1。只要我註釋掉用戶名/密碼,它就會按預期工作。用ProcessInfo用戶名和密碼調用wkhtmltopdf時無法讀取StandardOutput

在.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,密碼,域,輸出有數據,否則的話我試着使用的憑證,輸出爲空白,RETURNCODE爲-1。

希望能有其他人進入這個運行,似乎是一個常見的情況,我肯定失去了一些東西簡單...

感謝您的幫助!

回答

1

我不確定,是否可以在.net中使用,但是當我使用不同的憑據創建進程(例如Win API中的CreateProcessAsUser)並創建管道時,我必須明確傳遞給CreateNamedPipe Null security or Local security屬性。

可能是你有同樣的問題。

2

我也在.net應用程序中使用WKHTMLTOPDF。我發現了一些可能對您有幫助的事情:

在您的情況下,您可能遇到了寫入錯誤和導致死鎖的問題?

您可以將您的進程設置爲異步讀取standarderror和標準輸出,如我在下面的示例中所做的那樣。我希望這可以幫助

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