How to use WkHTMLToSharp to convert HTML file to PDF?

I need to convert a bunch of HTML files (about 30) to PDF files. It would be great if I could create TOC and page links, but for now I will just be happy to convert individual files :)

I already tried a couple of solutions, the most successful was EO.PDF, but it put an unpleasant watermark on every page, and it could not process the files for several megabytes, and some of them - 10 megabytes +.

I read a lot of good things about wkhtmltopdf and I found a wrapper for it, WkHTMLToSharp. I cannot find any documentation, so I have combined the next bit of code that throws an exception. I would appreciate any help resolving this.

I noticed a line that throws an exception. An exception (very UNhelpful) is:

"The type initializer for 'WkHtmlToXSharp.WkHtmlToPdfConverter' threw an exception." 

- THE CODE -

 /// <summary> /// Creates a PDF file from the HTML file passed in /// </summary> /// <param name="cFile">Full path to HTML file to generate PDF from</param> /// <param name="pdfFile">Full path of PDF output file</param> public static void WritePDF(string cFile, string pdfFile) { // Generates "The type initializer for // 'WkHtmlToXSharp.WkHtmlToPdfConverter' threw an exception.": WkHtmlToPdfConverter w = new WkHtmlToPdfConverter(); byte[] strHTML = w.Convert(cFile); File.WriteAllBytes(pdfFile, strHTML); w.Dispose(); } 

After solving the problem with the missing DLL, I found that a bit of code actually converts an HTML string, not a file. I can work with this, but MUCH prefers to work with HTML files.

In addition, none of the images are displayed in the PDF file. This is all JPG (I know there is a problem with GIFS).

+6
source share
2 answers

Use WkHtmlToXSharp.

Download the latest DLL from Github

 public static string ConvertHTMLtoPDF(string htmlFullPath, string pageSize, string orientation) { string pdfUrl = htmlFullPath.Replace(".html", ".pdf"); try { #region USING WkHtmlToXSharp.dll //IHtmlToPdfConverter converter = new WkHtmlToPdfConverter(); IHtmlToPdfConverter converter = new MultiplexingConverter(); converter.GlobalSettings.Margin.Top = "0cm"; converter.GlobalSettings.Margin.Bottom = "0cm"; converter.GlobalSettings.Margin.Left = "0cm"; converter.GlobalSettings.Margin.Right = "0cm"; converter.GlobalSettings.Orientation = (PdfOrientation)Enum.Parse(typeof(PdfOrientation), orientation); if (!string.IsNullOrEmpty(pageSize)) converter.GlobalSettings.Size.PageSize = (PdfPageSize)Enum.Parse(typeof(PdfPageSize), pageSize); converter.ObjectSettings.Page = htmlFullPath; converter.ObjectSettings.Web.EnablePlugins = true; converter.ObjectSettings.Web.EnableJavascript = true; converter.ObjectSettings.Web.Background = true; converter.ObjectSettings.Web.LoadImages = true; converter.ObjectSettings.Load.LoadErrorHandling = LoadErrorHandlingType.ignore; Byte[] bufferPDF = converter.Convert(); System.IO.File.WriteAllBytes(pdfUrl, bufferPDF); converter.Dispose(); #endregion } catch (Exception ex) { throw new Exception(ex.Message, ex); } return pdfUrl; } 
+2
source

I would like to add an alternative sentence: do not use WkHtmlToXSharp - rather install wkhtmltopdf and use it directly. In my opinion, spawning processes in C # .net are pretty simple, and so this is a viable alternative.

I use this method and recommended it to other users to see the earlier answer I gave . I still find the example that I used there is a good example, so I will repeat it.

 var pi = new ProcessStartInfo(@"c:\wkhtmltopdf\wkhtmltopdf.exe"); pi.CreateNoWindow = true; pi.UseShellExecute = false; pi.WorkingDirectory = @"c:\wkhtmltopdf\"; pi.Arguments = "http://www.google.com gogl.pdf"; using (var process = Process.Start(pi)) { process.WaitForExit(99999); Debug.WriteLine(process.ExitCode); } 
+1
source

Source: https://habr.com/ru/post/892927/


All Articles