【问题标题】:Image is not rendering properly图像未正确渲染
【发布时间】:2012-11-28 16:28:58
【问题描述】:

我正在使用 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

【问题讨论】:

  • 在控制台应用程序中运行此代码是否有效?
  • 看看这个链接,它会解释如何使用代码code.google.com/p/wkhtmltopdf'StackOverFlow可能重复以前的帖子'stackoverflow.com/questions/11907986/convert-html-file-to-image
  • @DarinDimitrov 如果在控制台应用程序中运行,我会得到相同的结果
  • @DJKRAZE 我知道那个网站,谢谢 :) 另外,我不认为这是一个骗局,因为这个问题是如何生成网页的图像。我的更多是特定库的问题。
  • 不是问题 Joe.. 这似乎是同一个问题.. +1

标签: c# asp.net-mvc image-processing bytearray wkhtmltoimage


【解决方案1】:

您最好将 I/O 文件用于 wkhtmltoimage.exe 进程而不是 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")
)

【讨论】:

  • 纯属天才!今晚我会试一试。谢谢
  • 它就像一个魅力。太感谢了。你不知道你为我节省了多少时间:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2013-04-04
  • 2013-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-10
相关资源
最近更新 更多