我正在使用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应用程序再次调用它。
/// <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.
我
发布于 2012-06-12 07:58:12
我找到了根本原因。生成的pdf中损坏的urls会导致性能下降。由于负载平衡,Azure在URL中包含端口号,因此我需要将URL转换为不带端口号的“公共”url。
https://stackoverflow.com/questions/10940431
复制相似问题