So, I've been spending a couple days beating my head over this library, trying to get simple HTML to render correctly. b tags don't seem to work sometimes, there are frequent missing blocks of text, or white-on-white renderings. It's a great start but it's really not production ready. Its also really long in the tooth, only supporting HTML 4.01.
If you just need something quick, easy, and free, and actually working to convert HTML to PDF just use Chrome:
try
{
var tempPath = Path.Combine(Path.GetTempPath(), "AHYAPI");
Directory.CreateDirectory(tempPath);
//Im using a cshtml render service, but your HTML file can come from anywhere
string html = await _razorRenderService.GetTemplateHtmlAsStringAsync("OrderConfirmationPdf", model);
var htmlPath = Path.Combine(tempPath, $"OrderPDF_{model.Order.OrderNumber}.html");
var pdfPath = Path.Combine(tempPath, $"OrderPDF_{model.Order.OrderNumber}.pdf");
File.WriteAllText(htmlPath, html);
if(File.Exists(pdfPath))
{
File.Delete(pdfPath);
}
ProcessStartInfo ps = new ProcessStartInfo()
{
FileName = @"C:\Program Files\Google\Chrome\Application\chrome.exe",
WorkingDirectory = Path.GetDirectoryName(_settings.PathToChromeExe),
UseShellExecute = false,
};
ps.ArgumentList.Add("--headless");
ps.ArgumentList.Add("--disable-gpu");
ps.ArgumentList.Add($"--user-data-dir={Path.Combine(Path.GetDirectoryName(pdfPath), "Chrome\\")}");
ps.ArgumentList.Add("--print-to-pdf-no-header"); //Remove this line if you want page numbers and other goo
ps.ArgumentList.Add($"--print-to-pdf={pdfPath}");
ps.ArgumentList.Add(htmlPath);
Process converter = Process.Start(ps);
if(!converter.WaitForExit(20000))
{
converter.Kill();
}
if (converter.ExitCode != 0)
{
Console.WriteLine("An error occurred.");
}
else
{
//This bit will launch the PDF in your viewer. this part isn't required though.
ProcessStartInfo ps2 = new ProcessStartInfo()
{
UseShellExecute = true,
FileName = pdfPath
};
Process.Start(ps2);
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
So, I've been spending a couple days beating my head over this library, trying to get simple HTML to render correctly. b tags don't seem to work sometimes, there are frequent missing blocks of text, or white-on-white renderings. It's a great start but it's really not production ready. Its also really long in the tooth, only supporting HTML 4.01.
If you just need something quick, easy, and free, and actually working to convert HTML to PDF just use Chrome: