title: Url to pdf using wkhtmltopdf.exe at server side
body: <p>Back few days i'm trying to make a web application that convert url to pdf. Finally i have done this with the help of wkhtmltopdf.exe
  My class code is given below</p>
  
  <pre><code>public class PDFGenerator
  {
      public static string HtmlToPdf(string pdfOutputLocation, string outputFilenamePrefix, string[] urls,
          string[] options = null,
          string pdfHtmlToPdfExePath = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe")
      {
          string urlsSeparatedBySpaces = string.Empty;
          try
          {
              //Determine inputs
              if ((urls == null) || (urls.Length == 0))
                  throw new Exception("No input URLs provided for HtmlToPdf");
              else
                  urlsSeparatedBySpaces = String.Join(" ", urls); //Concatenate URLs
  
              string outputFolder = pdfOutputLocation;
              string outputFilename = outputFilenamePrefix + "_" + DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss-fff") + ".PDF"; // assemble destination PDF file name
  
              var p = new System.Diagnostics.Process()
              {
                  StartInfo =
                  {
                      FileName = pdfHtmlToPdfExePath,
                      Arguments = ((options == null) ? "" : String.Join(" ", options)) + " " + urlsSeparatedBySpaces + " " + outputFilename,
                      UseShellExecute = false, // needs to be false in order to redirect output
                      RedirectStandardOutput = true,
                      RedirectStandardError = true,
                      RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
                      WorkingDirectory = HttpContext.Current.Server.MapPath(outputFolder)
                  }
              };
  
              p.Start();
  
              // read the output here...
              var output = p.StandardOutput.ReadToEnd();
              var errorOutput = p.StandardError.ReadToEnd();
  
              // ...then wait n milliseconds for exit (as after exit, it can't read the output)
              p.WaitForExit(60000);
  
              // read the exit code, close process
              int returnCode = p.ExitCode;
              p.Close();
  
              // if 0 or 2, it worked so return path of pdf
              if ((returnCode == 0) || (returnCode == 2))
                  return outputFolder + outputFilename;
              else
                  throw new Exception(errorOutput);
          }
          catch (Exception exc)
          {
              throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilenamePrefix, exc);
          }
      }
  }
  </code></pre>
  
  <p>and my button click event code is given below</p>
  
  <pre><code>//Create PDF from a single URL
      var pdfUrl = PdfGenerator.PDFGenerator.HtmlToPdf(pdfOutputLocation: "~/PDFs/",
          outputFilenamePrefix: "GeneratedPDF",
          urls: new string[] { "http://news.bbc.co.uk" });
  
      //Create PDF from multiple URLs
      pdfUrl = PdfGenerator.PDFGenerator.HtmlToPdf(pdfOutputLocation: "~/PDFs/",
          outputFilenamePrefix: "GeneratedPDF",
          urls: new string[] { "http://www.google.co.uk", "http://news.bbc.co.uk" });
  </code></pre>
  
  <p>all code is working properly and saving pdf in my PDFs code. But i have 2 questions:</p>
  
  <ol>
  <li>This code is not working when i host my website because <code>C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe</code> executing error. Is any dll library avail for wkhtmltopdf?</li>
  <li>When i run this code pdf saved in silent mode. I want to know that is this possible to save pdf with save dialog or ask user to folder location where they want to save pdf?
  Sorry for my bad english.</li>
  </ol>