Generate a PDF from HTML in C#
HTML content can render differently across browsers, devices, and viewport sizes. Converting HTML to PDF gives you a fixed output format for sharing, archiving, and printing.
Use this workflow when you need to:
- Preserve layout and styling in a stable document format
- Distribute web content for offline access
- Archive rendered pages for audit or compliance workflows
Prepare the project
Register the SDK license before running conversion logic. For setup details, refer to the getting started with .NET SDK guide.
using GdPicture14;
LicenseManager licence = new LicenseManager();licence.RegisterKEY("");Set up the web browser engine
Configure a Chrome headless shell for HTML rendering:
GdPictureDocumentUtilities.SetWebBrowserPath(GdPictureDocumentUtilities.FetchLatestChromeHeadlessShell(Environment.CurrentDirectory));This call downloads the runtime on first use and reuses it on later runs.
If you run this workflow inside Docker on Linux, you also need to install Chrome in the image and run the container as a non-root user. Refer to the running Chrome-based conversions in Docker guide.
Convert the HTML
Create a document converter instance:
using GdPictureDocumentConverter converter = new GdPictureDocumentConverter();Load HTML from a URL:
converter.LoadFromHttp(new("https://www.nutrient.io/sdk/dotnet"));Save the rendered output to PDF:
converter.SaveAsPDF(@"output.pdf");Handle errors
LoadFromHttp and SaveAsPDF return GdPictureStatus values. Use those values in your error handling flow. For details, refer to the handling errors with .NET SDK guide.
Conclusion
This workflow renders an HTML page through Chrome and saves the result as a PDF, preserving the page’s layout, fonts, images, and styling.