2016-08-23 29 views
0

返回幾天我試圖製作一個將url轉換爲pdf的web應用程序。最後我有一個的wkhtmltopdf.exe 我的類代碼的幫助下完成這個下面在服務器端使用wkhtmltopdf.exe將網址轉換爲pdf

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

,並給出以下

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

我的按鈕單擊事件代碼提供的所有代碼工作正常,並保存PDF在我的PDFs代碼中。但我有2個問題:

  1. 此代碼不工作時,我主辦我的網站,因爲C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe執行錯誤。是否有任何DLL庫wkhtmltopdf?
  2. 當我運行這個代碼pdf保存在靜默模式。我想知道這是可以保存PDF保存對話框或要求用戶文件夾的位置,他們要保存pdf? 對不起,我的英語不好。

回答

0
  1. 將您的「wkhtmltopdf.exe」在項目文件夾,以便它可以很容易地訪問你的應用程序。如果您在'C'文件夾中有.exe文件,可能會出現一些與訪問相關的問題。試試這個,它爲我工作。