Generate a PDF from HTML in C#

This guide explains how to create a PDF document from an HTML source — for example, creating a PDF from an HTML file or a website.

Creating a PDF from an HTML file

To create a PDF from an HTML file, follow these steps:

  1. Create a GdPictureDocumentConverter object.
  2. Load the source document by passing its path to the LoadFromFile method. Recommended: Specify the source document format with a member of the DocumentFormat enumeration.
  3. Save the output in a new PDF document with the SaveAsPDF method.

The example below creates a PDF document from an HTML file:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load the source document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.html", GdPicture14.DocumentFormat.DocumentFormatHTML);
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");

Creating a PDF from a website

To create a PDF from a website, follow these steps:

  1. Ensure you have the Chrome browser installed.
  2. Create a GdPictureDocumentConverter object.
  3. Pass the path to Chrome to the SetWebBrowserPath method of GdPictureDocumentUtilities.
  4. Pass the URL of the website to the LoadFromHttp method of the GdPictureDocumentConverter object. Recommended: Specify the source document format with a member of the DocumentFormat enumeration.
  5. Save the output in a new PDF document with the SaveAsPDF method.

The example below creates a PDF document from a website:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Set the path to Chrome.
GdPictureDocumentUtilities.SetWebBrowserPath(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe");
// Load the HTML document from the URL.
gdpictureDocumentConverter.LoadFromHttp(new Uri("https://www.nutrient.io/"), GdPicture14.DocumentFormat.DocumentFormatHTML);
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");

Optional HTML configuration properties

Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter object:

The example below creates a PDF document from an HTML file with a custom configuration:

using GdPictureDocumentConverter gdpictureDocumentConverter = new GdPictureDocumentConverter();
// Load the source document.
gdpictureDocumentConverter.LoadFromFile(@"C:\temp\source.html", GdPicture14.DocumentFormat.DocumentFormatHTML);
// Configure the conversion.
gdpictureDocumentConverter.HtmlPageHeight = 1028;
gdpictureDocumentConverter.HtmlPageMarginBottom = 15;
gdpictureDocumentConverter.HtmlPageMarginLeft = 15;
gdpictureDocumentConverter.HtmlPageMarginRight = 15;
gdpictureDocumentConverter.HtmlPageMarginTop = 15;
gdpictureDocumentConverter.HtmlPageWidth = 768;
gdpictureDocumentConverter.HtmlPreferCSSPageSize = false;
// Save the output in a new PDF document.
gdpictureDocumentConverter.SaveAsPDF(@"C:\temp\output.pdf");