Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devs: An Alternative to this Library #190

Open
shadowfoxish opened this issue Aug 20, 2021 · 1 comment
Open

Devs: An Alternative to this Library #190

shadowfoxish opened this issue Aug 20, 2021 · 1 comment

Comments

@shadowfoxish
Copy link

shadowfoxish commented Aug 20, 2021

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}");
}
@Deantwo
Copy link

Deantwo commented Feb 3, 2023

So you literally use Chrome to do it instead? Smart idea, but not really viable when you don't know if the user has Chrome installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants