首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >适用于ASP.NET MVC3的Azure和Rotativa PDF打印

问适用于ASP.NET MVC3的Azure和Rotativa PDF打印
EN

Stack Overflow用户
提问于 2012-06-08 05:55:45
回答 1查看 3.8K关注 0票数 2

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

但是当MVC应用程序部署为Azure中的web角色时(无论是在本地开发环境还是在cloudapp.net上),生成pdf print需要45秒,而且似乎在显示内容资源时也有困难(链接断开)。有些事情看起来不对劲,应该不会花那么长时间。

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

代码语言:javascript
复制
    /// <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();
    }

有没有人知道是什么原因导致了问题并降低了性能?

Br.

我

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-12 07:58:12

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

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10940431

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档