Azure and Rotativa PDF print for ASP.NET MVC3

余生颓废 提交于 2019-12-21 21:23:06

问题


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.

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.

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?

Br.

M


回答1:


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.



来源:https://stackoverflow.com/questions/10940431/azure-and-rotativa-pdf-print-for-asp-net-mvc3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!