2

以前はwkhtmltopdfを正常に使用していましたが、プロセスを開始するときに特定のアカウントを使用する必要があるというシナリオがあります。有効なユーザー名/pwdを設定すると、標準出力ストリームは空になり、戻りコードは-1になります。username / pwdをコメントアウトするとすぐに、期待どおりに機能します。

これを.Net4、Win764ビットでテストします。

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になります。

他の人がこれに遭遇したことを願って、一般的なシナリオのように思えます、確かに単純な何かを見逃しています...

助けてくれてありがとう!

4

2 に答える 2

3

また、.net アプリケーションで WKHTMLTOPDF を使用しています。あなたを助けるかもしれないことを私が発見したいくつかのことは次のとおりです。

  • wkhtmltopdf プロセスからの出力は、StandardOutput ではなく、StandardError に入れられます。したがって、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 
于 2013-11-06T17:13:07.963 に答える
1

.netで可能かどうかはわかりませんが、異なる資格情報(Win APIのCreateProcessAsUserなど)でプロセスを作成してパイプを作成するときは、明らかにCreateNamedPipe Nullセキュリティまたはローカルセキュリティ属性に渡す必要があります。

あなたも同じ問題を抱えているかもしれません。

于 2012-06-06T23:29:34.507 に答える