Azure和Rotativa PDF打印为ASP.NET MVC3 [英] Azure and Rotativa PDF print for ASP.NET MVC3

查看:121
本文介绍了Azure和Rotativa PDF打印为ASP.NET MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的<一个href=\"http://letsfollowtheyellowbrickroad.blogspot.dk/2012/02/rotativa-how-to-print-pdf-in-aspnet-mvc.html\"相对=nofollow>为ASP.NET MVC应用程序生成从HTML内容的PDF Rotativa PDF打印。运行在一个标准的IIS的MVC应用程序本身时,这个伟大的工程;生成的PDF几乎瞬间。

I'm using the Rotativa PDF print for an ASP.NET MVC application to generate a pdf from html content. This works great when running the MVC application by itself on an standard IIS; the pdf is generated almost instantly.

但是,当MVC应用程序部署在Azure的Web角色(包括在开发环境和cloudapp.net本地),生成PDF打印需要长达45秒,并且似乎也有显示内容ressources麻烦(链接被破坏)。有些事情似乎是错误的,它不应该花这么长时间。

But when MVC application is deployed as a web role in Azure (both locally on a dev environment and on cloudapp.net), generating the pdf print takes up to 45 seconds, and also seems have trouble displaying content ressources (the links are broken). Some thing seems wrong, it shouldn't take that long.

PDF生成本身是使用wkhtmltopdf工具,它转换HTML内容为PDF完成。 wkhtmltopdf是一个可执行文件,并且通过使用的Process.Start,而这又通过MVC应用程序调用执行。

The pdf generation itself is done using the wkhtmltopdf tool, which converts html content to PDF. wkhtmltopdf is an executable and is executed by using Process.Start, which again called by MVC application.

    /// <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();
    }

有没有人有什么可能导致的问题和有辱人格的性能吗?任何想法

Does anyone have any ideas on what might be causing the problems and degrading the performance?

溴。

M

推荐答案

我找到了根源。在生成的PDF中无效的网址,是导致性能下降。由于负载均衡,天青包含在URL中的端口号,所以我需要的URL转换为公共URL没有端口号。

I found the root cause. The broken urls in generated pdf, was causing the performance degrade. Due to load balancing, Azure includes the port number in the URL, so I needed to convert the URL to a "public" url without the portnumber.

这篇关于Azure和Rotativa PDF打印为ASP.NET MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆