title: Azure and Rotativa PDF print for ASP.NET MVC3
body: <p>I'm using the <a href="http://letsfollowtheyellowbrickroad.blogspot.dk/2012/02/rotativa-how-to-print-pdf-in-aspnet-mvc.html" rel="nofollow">Rotativa PDF print</a> 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.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <pre><code>    /// &lt;summary&gt;
      /// Converts given URL or HTML string to PDF.
      /// &lt;/summary&gt;
      /// &lt;param name="wkhtmltopdfPath"&gt;Path to wkthmltopdf.&lt;/param&gt;
      /// &lt;param name="switches"&gt;Switches that will be passed to wkhtmltopdf binary.&lt;/param&gt;
      /// &lt;param name="html"&gt;String containing HTML code that should be converted to PDF.&lt;/param&gt;
      /// &lt;returns&gt;PDF as byte array.&lt;/returns&gt;
      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)) &gt; 0)
              {
                  ms.Write(buffer, 0, read);
              }
          }
  
          string error = proc.StandardError.ReadToEnd();
  
          if (ms.Length == 0)
          {
              throw new Exception(error);
          }
  
          proc.WaitForExit();
  
          return ms.ToArray();
      }
  </code></pre>
  
  <p>Does anyone have any ideas on what might be causing the problems and degrading the performance?</p>
  
  <p>Br.</p>
  
  <p>M</p>