gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:07:41 25 4
gpt4 key购买 nike

我以前成功使用过 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com