HTML converter Features

10 Jan 202324 minutes to read

URL to PDF

To convert website URL or local HTML file to PDF document using Convert method in HtmlToPdfConverter class. refer to the following code example.

//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize HTML to PDF converter
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()
'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document 
document.Save("Output.pdf")

document.Close(True)

You can download a complete working sample from GitHub.

HTML String to PDF

The HTML to PDF converter provides support for converting HTML string to PDF. While converting HTML string to PDF, converter provides option to specify the base URL.

baseURL: Path of the resources (images, style sheets, scripts.,) used in the input HTML string.

For the following HTML string, the baseURL will be the path of the syncfusion_logo.gif image.

For example, if the above image is in “C:/Temp/ HTMLFiles/syncfusion_logo.gif” location, then the baseURL will be as follows.

baseURL: C:/Temp/HTMLFiles/

To convert the HTML string to PDF using Convert method, refer to the following code example.

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//HTML string and Base URL 
string htmlText = "<html><body><img src=\"syncfusion_logo.gif\" alt=\"Syncfusion_logo\" width=\"200\" height=\"70\"><p> Hello World</p></body></html>";
string baseUrl = @"C:/Temp/HTMLFiles/";

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert(htmlText, baseUrl);
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize HTML to PDF converter
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

'HTML string and Base URL 
Dim htmlText As String = "<html><body><img src=""syncfusion_logo.gif"" alt=""Syncfusion_logo"" width=""200"" height=""70""><p> Hello World</p></body></html>"
Dim baseUrl As String = "C:/Temp/HTMLFiles/"

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert(htmlText, baseUrl)
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<body>
<img src="syncfusion_logo.gif" alt="Syncfusion_logo" width="200" height="70">
<p> Hello World</p>
</body>
</html>

You can download a complete working sample from GitHub.

URL to Image

To convert website URL or local HTML file to Image using ConvertToImage method, refer to the following code example.

//Initialize HTML to PDF converter
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to Image
Image[] image = htmlConverter.ConvertToImage("https://www.google.com");
//Save and dispose the image file
image[0].Save("Output.jpg");
image[0].Dispose();
'Initialize HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()
'Convert URL to Image
Dim image As Image[] = htmlConverter.ConvertToImage("https://www.google.com")
'Save and dispose the image file
image[0].Save("Output.jpg")

image[0].Dispose(True)
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
//Convert URL to Image
Image image = htmlConverter.ConvertToImage("https://www.google.com");
byte[] imageByte = image.ImageData;
//Save the image
File.WriteAllBytes("Output.jpg", imageByte);

You can download a complete working sample from GitHub.

HTML String to Image

The Blink rendering engine supports converting HTML string to Image. While converting HTML string to Image, converter provides an option to specify the base URL.

baseURL: Path of the resources (images, style sheets, scripts.,) used in the input HTML string.

For the following HTML string, the baseURL will be the path of the syncfusion_logo.gif image.

For example, if the previous image is in “C:/Temp/ HTMLFiles/syncfusion_logo.gif” location then the baseURL will be as follows.

baseURL: C:/Temp/HTMLFiles/

To convert the HTML string to Image using ConvertToImage method, refer to the following code example.

//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//HTML string and Base URL
string htmlText = "<html><body><img src=\"syncfusion_logo.gif\" alt=\"Syncfusion_logo\" width=\"200\" height=\"70\"><p> Hello World</p></body></html>";
string baseUrl = @"C:/Temp/HTMLFiles/";

//Convert HTML string to Image
Image[] image = htmlConverter.ConvertToImage(htmlText, baseUrl)
//Save and dispose the image file
image[0].Save("Output.jpg");
image[0].Dispose();
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

'HTML string and Base URL
Dim htmlText As String = "<html><body><img src=""syncfusion_logo.gif"" alt=""Syncfusion_logo"" width=""200"" height=""70""><p> Hello World</p></body></html>"
Dim baseUrl As String = "C:/Temp/HTMLFiles/"

'Convert HTML string to Image
Dim image As Image[] = htmlConverter.Convert(htmlText, baseUrl)
'Save and dispose the image file
image[0].Save("Output.jpg")
image[0].Dispose(True)
//Initialize HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//HTML string and Base URL
string htmlText = "<html><body><img src=\"syncfusion_logo.gif\" alt=\"Syncfusion_logo\" width=\"200\" height=\"70\"><p> Hello World</p></body></html>";
string baseUrl = @"C:/Temp/HTMLFiles/";

//Convert HTML string to Image
Image image = htmlConverter.ConvertToImage(htmlText, baseUrl);
byte[] imageByte = image.ImageData;
//Save the image
File.WriteAllBytes("Output.jpg", imageByte);
<html>
<body>
    <img src="syncfusion_logo.gif" alt="Syncfusion_logo" width="200" height="70">
    <p> Hello World</p>
</body>
</html>

You can download a complete working sample from GitHub.

JavaScript

The Blink HTML converter supports enabling or disabling the JavaScript using EnableJavaScript property in BlinkConverterSettings class. while converting HTML to PDF. Refer to the following code example.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Disable JavaScript; By default, true
blinkConverterSettings.EnableJavaScript = false;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Disable JavaScript; By default True
blinkConverterSettings.EnableJavaScript = False
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Additional delay

The Blink HTML converter provides an option to set the AdditionalDelay property while converting HTML to PDF. Additional delay is the waiting time of the converter for loading the external resources (styles, scripts, images and more). Refer to the following code example.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
// Set additional delay; units in milliseconds
blinkConverterSettings.AdditionalDelay = 3000;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Set additional delay; units in milliseconds
blinkConverterSettings.AdditionalDelay = 3000
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")

document.Close(True)

You can download a complete working sample from GitHub.

The Blink HTML converter support preserving URL links from HTML to PDF using EnableHyperLink property in BlinkConverterSettings class. Refer to the following code snippet.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Enable hyperlinks; By default - true
blinkConverterSettings.EnableHyperLink = false;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = NewHtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Enable hyperlinks; By default - True
blinkConverterSettings.EnableHyperLink = False
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Bookmarks

The Blink HTML converter provides support for creating bookmarks automatically by enabling the EnableBookmarks property.

NOTE

The bookmarks are added from the <h> tag, it supports from <h1> to <h6>.

Refer to the following code snippet.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Set enable bookmarks
settings.EnableBookmarks = true;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert HTML to PDF
PdfDocument document = htmlConverter.Convert("input.html"); 
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Set enable bookmarks
settings.EnableBookmarks = True
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert HTML to PDF
Dim document As PdfDocument = htmlConverter.Convert("input.html")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<head>
<style>
body
{
text-align: left;
font-size: large;
padding-left: 5px;
}
</style>
</head>
<body>

<h1>Syncfusion</h1>

<h2>Introduction</h2>
	Syncfusion is the enterprise technology partner of choice for software development, delivering a broad range of web, mobile, and desktop controls coupled with a service-oriented approach throughout the entire application life cycle. 
<h2>Products</h2>
	<h4>WEB</h4>
		The most comprehensive suite for enterprise web development.
	<h4>Desktop</h4>
		Comprehensive suite of over 115 components including the fastest chart and grid components.
	<h4>Mobile</h4>
		Comprehensive suite of components for Xamarin.iOS, Xamarin.Android and Xamarin.Forms including the fastest chart and grid.
<h2>Consulting</h2>
	We can build web, mobile, and desktop applications better and faster than anyone since we build on top of our award-winning suite of components and frameworks, saving you time and money.
<h2>Company</h2>
	<h4>About us</h4>
		Syncfusion has established itself as the trusted partner worldwide for use in mission-critical applications. Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 12,000 customers, including large financial institutions, Fortune 100 companies, and global IT consultancies.
	<h4>contact us</h4>
		Morrisville Office
		Company Headquarters
		2501 Aerial Center Parkway
		Suite 200
		Morrisville, NC 27560
		USA
</body>
</html>

You can download a complete working sample from GitHub.

Table of contents

The Blink HTML converter provides support for creating a table of contents automatically by using the EnableToc property.

NOTE

TOC are added from the <h> tag, it supports from <h1> to <h6>.

Refer to the following code snippet.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Set enable table of contents
settings.EnableToc = true;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert HTML to PDF
PdfDocument document = htmlConverter.Convert("input.html");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Set enable table of contents
settings.EnableToc = True
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert HTML to PDF
Dim document As PdfDocument = htmlConverter.Convert("input.html")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<head>
<style>
body
{
text-align: left;
font-size: large;
padding-left: 5px;
}
</style>
</head>
<body>

<h1>Syncfusion</h1>

<h2>Introduction</h2>
	Syncfusion is the enterprise technology partner of choice for software development, delivering a broad range of web, mobile, and desktop controls coupled with a service-oriented approach throughout the entire application life cycle. 
<h2>Products</h2>
	<h4>WEB</h4>
		The most comprehensive suite for enterprise web development.
	<h4>Desktop</h4>
		Comprehensive suite of over 115 components including the fastest chart and grid components.
	<h4>Mobile</h4>
		Comprehensive suite of components for Xamarin.iOS, Xamarin.Android and Xamarin.Forms including the fastest chart and grid.
<h2>Consulting</h2>
	We can build web, mobile, and desktop applications better and faster than anyone since we build on top of our award-winning suite of components and frameworks, saving you time and money.
<h2>Company</h2>
	<h4>About us</h4>
		Syncfusion has established itself as the trusted partner worldwide for use in mission-critical applications. Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 12,000 customers, including large financial institutions, Fortune 100 companies, and global IT consultancies.
	<h4>contact us</h4>
		Morrisville Office
		Company Headquarters
		2501 Aerial Center Parkway
		Suite 200
		Morrisville, NC 27560
		USA
</body>
</html>

You can download a complete working sample from GitHub.

Table of contents with custom style

The Blink HTML converter provides support for customizing the table of contents style. Each header tag style can be customized by using HtmlToPdfTocStyle.

Refer to the following code snippet.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Set enable table of contents
settings.EnableToc = true;
//Set the style for level 1(H1) items in table of contents
HtmlToPdfTocStyle tocstyleH1 = new HtmlToPdfTocStyle();
tocstyleH1.Font = new PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Regular);
tocstyleH1.BackgroundColor = new PdfSolidBrush(new PdfColor(Color.FromArgb(68, 114, 196)));
tocstyleH1.ForeColor = PdfBrushes.White;
tocstyleH1.Padding = new PdfPaddings(5, 5, 3, 3);
settings.Toc.SetItemStyle(1, tocstyleH1);
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert HTML to PDF
PdfDocument document = htmlConverter.Convert("input.html");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Set enable table of contents
settings.EnableToc = True
'Set the style for level 1(H1) items in table of contents
Dim tocstyleH1 As New HtmlToPdfTocStyle()
tocstyleH1.Font = New PdfStandardFont(PdfFontFamily.TimesRoman, 10, PdfFontStyle.Regular)
tocstyleH1.BackgroundColor = New PdfSolidBrush(New PdfColor(Color.FromArgb(68, 114, 196)))
tocstyleH1.ForeColor = PdfBrushes.White
tocstyleH1.Padding = New PdfPaddings(5, 5, 3, 3)
settings.Toc.SetItemStyle(1, tocstyleH1)
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert HTML to PDF
Dim document As PdfDocument = htmlConverter.Convert("input.html")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<head>
<style>
body
{
text-align: left;
font-size: large;
padding-left: 5px;
}
</style>
</head>
<body>

<h1>Syncfusion</h1>

<h2>Introduction</h2>
	Syncfusion is the enterprise technology partner of choice for software development, delivering a broad range of web, mobile, and desktop controls coupled with a service-oriented approach throughout the entire application life cycle. 
<h2>Products</h2>
	<h4>WEB</h4>
		The most comprehensive suite for enterprise web development.
	<h4>Desktop</h4>
		Comprehensive suite of over 115 components including the fastest chart and grid components.
	<h4>Mobile</h4>
		Comprehensive suite of components for Xamarin.iOS, Xamarin.Android and Xamarin.Forms including the fastest chart and grid.
<h2>Consulting</h2>
	We can build web, mobile, and desktop applications better and faster than anyone since we build on top of our award-winning suite of components and frameworks, saving you time and money.
<h2>Company</h2>
	<h4>About us</h4>
		Syncfusion has established itself as the trusted partner worldwide for use in mission-critical applications. Founded in 2001 and headquartered in Research Triangle Park, N.C., Syncfusion has more than 12,000 customers, including large financial institutions, Fortune 100 companies, and global IT consultancies.
	<h4>contact us</h4>
		Morrisville Office
		Company Headquarters
		2501 Aerial Center Parkway
		Suite 200
		Morrisville, NC 27560
		USA
</body>
</html>

You can download a complete working sample from GitHub.

Media Type

The Blink HTML Converter allows selection of media type while converting HTML to PDF. Blink rendering engine supports Screen and Print media types. Refer to the following code snippet to select Print MediaType.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set print media type
blinkConverterSettings.MediaType = MediaType.Print;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Set print media type
blinkConverterSettings.MediaType = MediaType.Print
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

NOTE

Print MediaType MediaType enables the repeat html table header and footer support on every PDF page.

HTML Form to PDF Form

Blink rendering engine provides support for converting HTML forms to PDF fillable forms automatically by using the EnableForm property. To convert HTML form to PDF form, refer to the following code example.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Set enable form
settings.EnableForm = true;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com"); 
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Set enable form
settings.EnableForm = True
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.syncfusion.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Windows authentication

The webpage you want to convert may protected with windows authentication. Blink rendering engine provides support for converting the Windows Authenticated webpage to PDF document by providing the Username and Password property in BlinkConverterSettings class. Refer to the following code example.

// Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
blinkConverterSettings.Username = "username";
blinkConverterSettings.Password = "password";
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.example.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
blinkConverterSettings.Username = "username"
blinkConverterSettings.Password = "password"
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.example.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Form authentication

The Blink HTML converter provides support for form authentication by using Cookies property in BlinkConverterSettings class. The cookies are send to web server for form authentication when the HTML page is requested. Each cookie is represented by a name and value. Refer to the following code example.

// Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
// Add cookies as name and value pair
blinkConverterSettings.Cookies.Add("CookieName1", " CookieValue1");
blinkConverterSettings.Cookies.Add("CookieName2", " CookieValue2");
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.example.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Add cookies
blinkConverterSettings.Cookies.Add("Name1", "Value1")
blinkConverterSettings.Cookies.Add("Name2", "Value2")
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.example.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Token-based authentication

The Blink HTML converter supports token-based authentication by using the HttpRequestHeaders property in BlinkConverterSettings class.. The token values will be send to web server when the HTML page is requested. Refer to the following code example.

//Initialize HTML to PDF converter 
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Add a bearer token to login a webpage
settings.HttpRequestHeaders.Add("Authorization", "bearer <<token value here>>");
//Assign Blink settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.example.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize HTML to PDF converter 
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings
'Add a bearer token to login a webpage
settings.HttpRequestHeaders.Add("Authorization", "bearer <<token value here>>")
'Assign Blink settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.example.com")
'Save and close the PDF document 
document.Save("Output.pdf")
document.Close(true)

You can download a complete working sample from GitHub.

Offline conversion

The Blink HTML converter supports converting HTML to PDF in offline mode using EnableOfflineMode property in BlinkConverterSettings class. While converting HTML to PDF in offline mode, the converter does not access the resources from the internet. This may increase the performance in slow internet connection.

NOTE

If an online URL is converted in offline mode, the converter will generate empty PDF as it will not try to load any resource from online.

Refer to the following code snippet.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Enable offline mode
blinkConverterSettings.EnableOfflineMode = true;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Enable offline mode
blinkConverterSettings.EnableOfflineMode = True
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

HTTP GET and POST

The Blink HTML converter supports transmitting the parameter to the webpage. There are two methods to access a webpage. By default, Blink uses GET method. By using HTTP GET method, the parameters can be passed in the query string. In POST method, the parameters can be passed by using the HttpPostFields property.
Refer to the following code snippet to access a webpage using HTTP POST.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Add HTTP post parameters to HttpPostFields
settings.HttpPostFields.Add("firstName", "Andrew");
settings.HttpPostFields.Add("lastName", "Fuller");
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.example.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Add HTTP Post parameters to HttpPostFields 
settings.HttpPostFields.Add("firstName", "Andrew")
settings.HttpPostFields.Add("lastName", "Fuller")
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.example.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Use the following code snippet to access a webpage using HTTP GET.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

string url = "https://www.example.com";
Uri getMethodUri = new Uri(url);
string httpGetData = getMethodUri.Query.Length > 0 ? "&" : "?" + String.Format("{0}={1}", "firstName", "Andrew");
httpGetData += String.Format("&{0}={1}", "lastName", "Fuller");
string urlToConvert = url + httpGetData;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert(urlToConvert);
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim url As String = "https://www.example.com"
Dim getMethodUri As New Uri(url)
Dim httpGetData As String = If(getMethodUri.Query.Length > 0, "&", "?" + [String].Format("{0}={1}", "firstName", "Andrew"))
httpGetData += [String].Format("&{0}={1}", "lastName", "Fuller")
Dim urlToConvert As String = url & httpGetData

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert(urlToConvert)
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

System proxy

By default, the Blink rendering engine use system proxy settings for converting HTML to PDF. If proxy server is configured in the system, then the rendering engine automatically use the same settings for the conversion. Follow the below steps to set the system proxy settings:

  1. Control Panel > Network and Internet > Internet Options.
  2. From Internet properties window, open LAN settings under connections tab.
  3. Then, set proxy server address and port in LAN settings window.

Please refer below screenshots:

Manual proxy

Manual proxy settings

Manual proxy

You can specify the manual proxy settings for the conversion using the ProxySettings property. Refer to the following code snippet to configure the manual proxy settings for the conversion.

//Initialize the HTML to PDF converter.

HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Set manual proxy settings
settings.ProxySettings.HostName = "127.0.0.1";
settings.ProxySettings.PortNumber = 8080;
settings.ProxySettings.Type = BlinkProxyType.HTTP;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Set manual proxy settings
settings.ProxySettings.HostName = "127.0.0.1"
settings.ProxySettings.PortNumber = 8080
settings.ProxySettings.Type = BlinkProxyType.HTTP
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

Viewport

Adjusting the HTML content size in PDF is possible by using the ViewPortSize property of Blink HTML converter.
Refer to the following code snippet to adjust Blink viewport.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set Blink viewport size
blinkConverterSettings.ViewPortSize = new Size(800, 0);
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Set Blink viewport size
blinkConverterSettings.ViewPortSize = New Size(800, 0)
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.

Partial webpage to PDF

The Blink rendering engine provides support for converting only the part of an HTML document like a table, div, or image elements from the URL/HTML string. You can convert the particular HTML element using ConvertPartialHtml method by specifying the HTML element ID, refer to the following code example.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

//Convert Partial webpage to PDF
PdfDocument document = htmlConverter.ConvertPartialHtml("input.html", "pic");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

'Convert Partial webpage to PDF
Dim document As PdfDocument = htmlConverter. ConvertPartialHtml("input.html", "pic")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<head>
</head>
<body>
Hello world
	<div id="pic">
		<img src=" syncfusion_logo.gif" alt="Smiley face" width="42" height="42"><br>
		This is a Syncfusion Logo
	</div>
	<div>
		Hello world
	</div>
</body>
</html>

You can download a complete working sample from GitHub.

HTML to single PDF page

By using this SinglePageLayout property, you can render the whole HTML content into a single PDF page. The PDF page size is limited to 14400 points. There are two options to enable this feature since this is disabled by default.

1. FitWidth
2. FitHeight

Fit width option: Using this option, the HTML converter adjusts the PDF page height based on the HTML content height. PDF page width remains constant for this option.
Fit height option: Using this option, the HTML converter scale the HTML content and PDF page width to render the whole HTML content within the height. PDF page height remains constant for this option.

Refer to the following code snippet.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings settings = new BlinkConverterSettings();
//Set singlePageLayout option to render the whole HTML content in a single PDF page
settings.SinglePageLayout = SinglePageLayout.FitWidth;
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim settings As BlinkConverterSettings = New BlinkConverterSettings()
'Set singlePageLayout option to render the whole HTML content in a single PDF page
settings.SinglePageLayout = SinglePageLayout.FitWidth
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = settings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

Layout Result

Getting height of the HTML content in PDF document is possible by using the PdfLayoutResult class. Using this result, you can add contents after converting HTML to PDF. Refer to the following code example.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

PdfLayoutResult layoutResult = null;
//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.syncfusion.com", out layoutResult); 
//Draw the text at the end of HTML content
PdfFont font = new PdfStandardFont(PdfFontFamily.Helvetica, 11);
document.Pages[document.Pages.Count - 1].Graphics.DrawString("End of HTML content", font, PdfBrushes.Red, new PointF(0, layoutResult.Bounds.Bottom));
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim layoutResult As PdfLayoutResult = Nothing
'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.syncfusion.com", layoutResult)
'Draw the text at the end of HTML content
Dim font As PdfFont = New PdfStandardFont(PdfFontFamily.Helvetica, 11)
document.Pages((document.Pages.Count - 1)).Graphics.DrawString("End of HTML content", font, PdfBrushes.Red, New PointF(0, layoutResult.Bounds.Bottom))
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

Windows status

Windows status can be used instead of additional delay. In additional delay, the amount of time required for loading the resources is unpredictable. This behavior can be avoided by using WindowStatus property.

NOTE

This feature requires changes in the HTML file.

NOTE

If windows status does not match in code and HTML, then the converter will meet with deadlock.

Refer to the following code snippet.

// Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set windows status
blinkConverterSettings.WindowStatus = "completed";
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("input.html");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Set windows status.
blinkConverterSettings.WindowStatus = "completed"
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("input.html")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)
<html>
<head>
</head>
<body>
    <div id="message">
        Wait for 2 Seconds
    </div>
    <script type="text/javascript">
        setTimeout(function () {
            document.getElementById("message").innerHTML = "Hello World!!";
            window.status = "completed";
        }, 2000);
    </script>
</body>
</html>

You can download a complete working sample from GitHub.

Temporary path

The Blink HTML converter launching Chrome browser to perform conversion. While launching Chrome browser, temporary files are created in a temporary folder.

By default, HTML converter takes system temporary path (C:\Users<<username»\AppData\Local\Temp or C:\Windows\Temp) to perform the conversion.

The temporary path can be changed by using the TempPath property of BlinkConverterSettings. If this property is set, the converter uses the provided path to perform the conversion. Refer to the following code example.

//Initialize the HTML to PDF converter.
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();

BlinkConverterSettings blinkConverterSettings = new BlinkConverterSettings();
//Set Temporary Path to generate temporary files
blinkConverterSettings.TempPath = @"C:/HtmlConversion/Temp/";
//Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings;

//Convert URL to PDF
PdfDocument document = htmlConverter.Convert("https://www.google.com");
FileStream fileStream = new FileStream("HTML-to-PDF.pdf", FileMode.CreateNew, FileAccess.ReadWrite);
//Save and close the PDF document.
document.Save(fileStream);
document.Close(true);
'Initialize the HTML to PDF converter.
Dim htmlConverter As HtmlToPdfConverter = New HtmlToPdfConverter()

Dim blinkConverterSettings As BlinkConverterSettings = New BlinkConverterSettings()
'Set Temporary Path to generate temporary files
blinkConverterSettings.TempPath = "C:/HtmlConversion/Temp/"
'Assign Blink converter settings to HTML converter
htmlConverter.ConverterSettings = blinkConverterSettings

'Convert URL to PDF
Dim document As PdfDocument = htmlConverter.Convert("https://www.google.com")
'Save and close the PDF document
document.Save("Output.pdf")
document.Close(True)

You can download a complete working sample from GitHub.