WkhtmlToImageを使用してWebページを画像にレンダリングしています。コマンドラインから実行すると、すべてがうまく機能します。ただし、Webアプリから開始したプロセスから実行すると、実行されません。
使用している引数が同じであることを確認しました。私が見ることができる唯一の違いは、コマンドラインから実行するときはファイルをディスクに保存し、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