c# - 使用 ProcessInfo 用户名和密码调用 wkhtmltopdf 时无法读取 StandardOutput

标签 c# .net process console wkhtmltopdf

我之前曾成功使用过 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。我发现的一些可能对您有帮助的事情是:

在您的情况下,您可能会遇到写入错误并导致死锁的问题?

您可以将进程设置为异步读取标准错误和标准输出,就像我在下面的示例中所做的那样。我希望这有帮助

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/

相关文章:

c# - 如何更改 VS2015 中的代码生成策略?

c# - .NET Core TestServer 返回状态 200 而不是 404 以将请求发送到不存在的路径

c - 一旦可用就读取子进程的输出?

c - WCOREDUMP(status) 似乎总是返回 true,即使该功能已关闭

process - 操作系统如何在进程执行时处理其职责?

c# - Visual Studio 中用于重命名日志记录方法参数字符串的正则表达式?

c# - 删除表中的所有实体

c# - 在 .Net Core 2.1 的 XUnit 单元测试中注册 AutoMapper

c# - 如何在 .NET 中匹配正则表达式 "shortest match"

.net - 对于陷入 1.1 世界的 dotnet 3.5 编码员来说,有什么能让他们的生活更轻松吗?