DEV Community

Satish Patil
Satish Patil

Posted on • Edited on

4

HTMLtoPdf - Converts HTML content to PDF using chrome executable

Note : Requires Chrome executable

Nuget Link - https://www.nuget.org/packages/Sats.HTMLtoPdf

Github link - https://github.com/satsvelke/HTMLtoPdf

Usage

           var url = @"d:\Vaccination.html";
          var chromePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

          // returns byte array of file 
          var pdf = new ChromeOptions().AddOptions(b =>
                                      {
                                          b.Headless();
                                          b.DisableGPU();
                                          b.WithoutHeader();

                                      }).Pdf(new ChromeDetails() { ChromePath = chromePath, HtmlPath = url
 OutputPath = @"d:\print.pdf" // optional (add if 
             Environment.CurrentDirectory does not have access rights)
         });

           File.WriteAllBytes(@"d:\print.pdf", pdf);  
Enter fullscreen mode Exit fullscreen mode

Version : 2.1

        var url = @"d:\test.html";
        var chromePath = @"C:\Program Files\Google\Chrome\Application\chrome.exe";

        var output = new ChromeOptions().AddOptions(b =>
                            {
                                b.Headless();
                                b.DisableGPU();
                                b.WithoutHeader();

                            }).ToPdf(new ChromeDetails()
                            {
                                ChromePath = chromePath,
                                HtmlPath = url,
                                DeleteOutputFile = true, //optional
                               // OutputPath = @"d:\print.pdf" // (add if Environment.CurrentDirectory does not have access rights)
                            });


        File.WriteAllBytes(@"d:\print.pdf", output.FileDetails.File);
Enter fullscreen mode Exit fullscreen mode

output.FileDetails.File will have byte array of created pdf
output.ProcessDetails will have output details like errors

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay