1

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

4

1 に答える 1

3

wkhtmltoimage.exeプロセスには、I/OストリームではなくI/Oファイルを使用することをお勧めします。

public static byte[] Convert(string wkhtmltopdfPath, string switches, string html)
{
    using (var tempFiles = new TempFileCollection())
    {
        var input = tempFiles.AddExtension("htm");
        var output = tempFiles.AddExtension("jpg");
        File.WriteAllText(input, html);

        switches += string.Format(" -f jpeg {0} {1}", input, output);
        var psi = new ProcessStartInfo(Path.Combine(wkhtmltopdfPath, "wkhtmltoimage.exe"))
        {
            UseShellExecute = false,
            CreateNoWindow = true,
            Arguments = switches
        };
        using (var process = Process.Start(psi))
        {
            process.WaitForExit((int)TimeSpan.FromSeconds(30).TotalMilliseconds);
        }

        return File.ReadAllBytes(output);
    }
}

その後:

byte[] result = Convert(
    @"c:\Program Files (x86)\wkhtmltopdf", 
    "",
    File.ReadAllText("test.htm")
)
于 2012-12-11T13:04:59.940 に答える