【问题标题】:Azure and Rotativa PDF print for ASP.NET MVC3用于 ASP.NET MVC3 的 Azure 和 Rotativa PDF 打印
【发布时间】:2012-06-12 00:23:26
【问题描述】:

我将Rotativa PDF print 用于 ASP.NET MVC 应用程序以从 html 内容生成 pdf。这在标准 IIS 上单独运行 MVC 应用程序时非常有用; pdf 几乎是立即生成的。

但是,当 MVC 应用程序在 Azure 中部署为 Web 角色时(在本地开发环境和 cloudapp.net 上),生成 pdf 打印文件最多需要 45 秒,而且显示内容资源似乎也有问题(链接坏了)。有些事情似乎不对劲,应该不会花那么长时间。

pdf 生成本身是使用 wkhtmltopdf 工具完成的,该工具将 html 内容转换为 PDF。 wkhtmltopdf 是一个可执行文件,使用 Process.Start 执行,再次被 MVC 应用程序调用。

    /// <summary>
    /// Converts given URL or HTML string to PDF.
    /// </summary>
    /// <param name="wkhtmltopdfPath">Path to wkthmltopdf.</param>
    /// <param name="switches">Switches that will be passed to wkhtmltopdf binary.</param>
    /// <param name="html">String containing HTML code that should be converted to PDF.</param>
    /// <returns>PDF as byte array.</returns>
    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 += " -";

        var proc = new Process
                       {
                           StartInfo = new ProcessStartInfo
                                           {
                                               FileName = Path.Combine(wkhtmltopdfPath, "wkhtmltopdf.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();
    }

是否有人对可能导致问题和降低性能的原因有任何想法?

兄弟。

M

【问题讨论】:

  • 您可以为您的 Azure VM 启用 RDP 并运行 PerfMon 以查看总共花费了 45 秒的时间,然后决定下一步该做什么。很容易猜到性能问题在哪里,但我相信它不会帮助你。
  • 感谢您的意见。我已经记录了从 proc.Start 到 proc.WaitForExit() 之后的时间。在操作之间平均需要 43 秒才能执行。我的猜测是读取内存流会降低性能。
  • 如果你在本地机器上做同样的事情需要多长时间?您能否进一步解读哪些 API 大部分时间都在消耗?如果有任何 Azure 特定 API,我当然可以帮助您根据需要进行优化,但是我认为现在没有任何意义可以开始进一步挖掘......
  • 我找到了根本原因。生成的 pdf 中损坏的 url 导致性能下降。由于负载平衡,Azure 在 URL 中包含端口号,因此我需要将 URL 转换为没有端口号的“公共”url。

标签: asp.net-mvc-3 azure pdf-generation


【解决方案1】:

我找到了根本原因。生成的 pdf 中损坏的 url 导致性能下降。由于负载平衡,Azure 在 URL 中包含端口号,因此我需要将 URL 转换为没有端口号的“公共”url。

【讨论】:

  • @Paras 问道,“您能否分享您的代码以将视图转换为 PDF。我遇到了同样的问题。这将是一个很大的帮助。”
猜你喜欢
  • 1970-01-01
  • 2018-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-31
  • 2011-02-18
相关资源
最近更新 更多