2013-02-28 65 views
0

我需要在Windows Phone 8(c#)中將HTML文件轉換爲pdf或圖像。我需要一種方法來呈現HTML文件,以PDF或圖像 任何人都知道如何?HTML到PDF或圖像c#

回答

0

對於轉換PDF,你可以用它wkhtmltopdf

而且代碼

string pdfHtmlToPdfExePath ="C:\\Program Files (x86)\\wkhtmltopdf\\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 HttpContext.Current.Server.MapPath(outputFolder +"//"+ outputFilename); 
       else 
        throw new Exception(errorOutput); 
      } 
      catch (Exception exc) 
      { 
       throw new Exception("Problem generating PDF from HTML, URLs: " + urlsSeparatedBySpaces + ", outputFilename: " + outputFilename, exc); 
      } 
+0

這是一個非常好的工具,但圖像/輸出PDF字體可以在某些情況下,很醜陋。 – ChruS 2013-02-28 13:09:21

+1

謝謝,但這對Windows Phone來說很有用 – 2013-02-28 13:24:55

1

在我們的代碼庫,我們也有HTML網頁轉換爲PDF文件。 我們選擇了第三方軟件供應商​​

此供應商有a paying tool可以輕鬆地將HTML轉換爲PDF。

希望這會有所幫助。

親切的問候,維姆