Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly use WkHTMLToSharp to convert HTML file to PDF?

I need to convert a bunch of HTML files (about 30) to PDFs. Would be awesome if I could create a TOC and link pages, but right now I'd just be happy converting the individual files :)

I've tried a couple solutions already, the most successful was EO.PDF, but it put a nasty watermark on every page, and it couldn't handle files over a few meg, and some of mine are 10meg+.

I've read a lot of good things about wkhtmltopdf, and I found the wrapper for it, WkHTMLToSharp. I am unable to find any documentation, so I cobbled together the following bit of code, that is throwing an exception. I'd appreciate any help resolving this.

I noted the line that is causing the exception. The (very UNhelpful) exception is:

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

--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 resolving the issue with the missing DLL, I discovered that bit of code actually converts a string of HTML, not a file. I CAN work with that, but would MUCH prefer to work with the HTML files.

In addition, none of the images are being displayed in the PDF file. They are all JPGs (I know there is an issue with GIFS).

like image 765
Dizzy49 Avatar asked Jul 17 '11 09:07

Dizzy49


1 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;
}
like image 191
Derin Avatar answered Oct 17 '22 19:10

Derin