2013-02-28 63 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。

希望这会有所帮助。

亲切的问候,维姆