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:
- Create a
GdPictureDocumentConverter
object. - Load the source document by passing its path to the
LoadFromFile
method. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. - 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");
Using gdpictureDocumentConverter As 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")End Using
Used methods
Related topics
Creating a PDF from a website
To create a PDF from a website, follow these steps:
- Ensure you have the Chrome browser installed.
- Create a
GdPictureDocumentConverter
object. - Pass the path to Chrome to the
SetWebBrowserPath
method ofGdPictureDocumentUtilities
. - Pass the URL of the website to the
LoadFromHttp
method of theGdPictureDocumentConverter
object. Recommended: Specify the source document format with a member of theDocumentFormat
enumeration. - 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");
Using gdpictureDocumentConverter As 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")End Using
Used methods
Related topics
Optional HTML configuration properties
Optionally, configure the conversion with the following properties of the GdPictureDocumentConverter
object:
HtmlEmulationType
HtmlPageHeight
HtmlPageMarginBottom
HtmlPageMarginLeft
HtmlPageMarginRight
HtmlPageMarginTop
HtmlPageWidth
HtmlPreferCSSPageSize
HtmlPreferOnePage
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");
Using gdpictureDocumentConverter As 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")End Using
Used methods and properties
Related topics