我正在使用 WkhtmlToImage 将网页呈现为图像。当我从命令行运行它时,一切都很好。但是,当我从从我的网络应用程序启动的进程运行它时,它不会。
我已经验证我使用的论点是相同的。我能看到的唯一区别是,当我从命令行运行它时,我将文件保存到磁盘,而当我从 Web 应用程序执行它时,我使用 stdOut 并返回字节数组。有谁知道为什么会这样?我在用11.0-rc2
//taken from the Rotativa library - https://github.com/webgio/Rotativa/
private static byte[] Convert(string wkhtmltopdfPath, string switches, string html)
{
// switches:
// "-q" - silent output, only errors - no progress messages
// " -" - switch output to stdout
// "- -" - switch input to stdin and output to stdout
switches = "-q " + switches + " -";
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
switches += " -";
html = SpecialCharsEncode(html);
}
var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltoimage.exe"),
Arguments = switches,
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true,
WorkingDirectory = wkhtmltopdfPath,
CreateNoWindow = true
}
};
proc.Start();
// generate PDF from given HTML string, not from URL
if (!string.IsNullOrEmpty(html))
{
using (var sIn = proc.StandardInput)
{
sIn.WriteLine(html);
}
}
var ms = new MemoryStream();
using (var sOut = proc.StandardOutput.BaseStream)
{
byte[] buffer = new byte[4096];
int read;
while ((read = sOut.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
}
string error = proc.StandardError.ReadToEnd();
if (ms.Length == 0)
{
throw new Exception(error);
}
proc.WaitForExit();
return ms.ToArray();
}
更新我发现在 Windows 中使用 stdOut 时,这是库的一个已知问题。如果有人有任何想法,我会全力以赴。
http://code.google.com/p/wkhtmltopdf/issues/detail?id=335&q=wkhtmltoimage%20stdout http://code.google.com/p/wkhtmltopdf/issues/detail?id=998&q=wkhtmltoimage%20stdout