Reputation: 1530
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).
Upvotes: 6
Views: 4337
Reputation: 13442
I'd like to add an alternative suggestion: Don't use WkHtmlToXSharp - rather install wkhtmltopdf and use it directly. Spawning processes in c#.net is quite simple in my opinion and so it is a viable alternative.
I use that method and have recommended it to others with success, see an earlier answer I gave. I still find the example I used there a nice example, so I'll 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);
}
Upvotes: 1
Reputation: 1230
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;
}
Upvotes: 2