10 .NET API products for your office documents
Total Suite Value:
$7,192 USD
In today's digital age, the need to convert HTML to PDF files is more important than ever. Whether you're generating reports, invoices, or simply archiving web content, converting HTML to PDF is a practical solution. IronPDF is a powerful library that simplifies this process, making it easy for developers to create high-quality PDF documents from HTML content
IronPDF is a .NET library that allows developers to convert HTML to PDF with ease. It supports a wide range of features, including CSS, JavaScript, and even embedded images. With IronPDF, you can create PDFs that look exactly like your HTML web pages, ensuring a seamless transition between formats. This library is particularly useful for web applications that need to generate dynamic PDF documents on the fly.
IronPDF allows developers to seamlessly integrate PDF functionality into .NET applications without needing to manually manage PDF file structures. IronPDF leverages the Chrome-based rendering engine to convert an HTML pages (including complex CSS, JavaScript, and images) into well-structured PDF documents. It can be used for generating reports, invoices, eBooks, or any type of document that needs to be presented in PDF format.
IronPDF is versatile, offering functionality that not only renders PDFs but also provides a wide range of PDF manipulation options like editing, form handling, encryption, and more.
HTML to PDF Conversion
HTML Rendering: IronPDF can convert HTML documents or web pages (including HTML with CSS, images, and JavaScript) directly into a PDF document. This is ideal for generating PDFs from dynamic web content.
Support for Modern HTML/CSS: IronPDF handles modern HTML5, CSS3, and JavaScript, ensuring that your web-based content is rendered accurately as a PDF, preserving the layout, fonts, and interactive elements.
Custom Headers and Footers
Support for JavaScript in PDFs
Edit Existing PDFs
Merge and Split PDFs
Support for Interactive Forms
Page Manipulation
Security and Encryption
Watermarking and Branding
Text and Image Extraction
Unicode and Multi-language Support
API and Developer-Friendly Tools
To get started, open Visual Studio and create a new project as below.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Select create console application.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Provide project name and location.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Select .NET version
Add from PixabayUpload
or drag and drop an image here
Add image alt text
click "create" button to complete the project creation.
Before you can start converting HTML to PDF, you'll need to install the IronPDF library. You can do this using NuGet Package Manager in Visual Studio or by running the following command in the Package Manager Console:
Install-Package IronPdf
Install-Package IronPdf
'INSTANT VB TODO TASK: The following line uses invalid syntax:
'Install-Package IronPdf
Also, IronPDF can be installed using Visual Studio Package Manager
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Below is code for HTML to PDF converter application. Here, HTML code or HTML elements represented in string format is used as input and converted to PDF. Convert HTML file to PDF using the same steps, with an additional step to read the content from the HTML file.
class Program
{
static void Main()
{
// Specify license key
IronPdf.License.LicenseKey = "Your Key";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
// Define the HTML string to be converted, can use html document
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1></body></html>";
// Convert HTML string to a PDF document
var document = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a file
document.SaveAs("html2Pdf.pdf");
}
}
class Program
{
static void Main()
{
// Specify license key
IronPdf.License.LicenseKey = "Your Key";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
// Define the HTML string to be converted, can use html document
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1></body></html>";
// Convert HTML string to a PDF document
var document = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a file
document.SaveAs("html2Pdf.pdf");
}
}
Friend Class Program
Shared Sub Main()
' Specify license key
IronPdf.License.LicenseKey = "Your Key"
' Create a new HtmlToPdf object
Dim Renderer = New ChromePdfRenderer()
' Define the HTML string to be converted, can use html document
Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1></body></html>"
' Convert HTML string to a PDF document
Dim document = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF document to a file
document.SaveAs("html2Pdf.pdf")
End Sub
End Class
License Key Setup:
The program begins by setting the IronPDF license key to enable full functionality of the library.
Creating the Renderer:
An instance of ChromePdfRenderer is created. This is the component responsible for rendering the HTML into a PDF document. It essentially acts as a bridge between the HTML content and the generated PDF.
HTML Content:
A string variable htmlContent is defined, which contains the HTML code you want to convert into a PDF. In this case, it’s a simple HTML structure with a heading.
HTML to PDF Conversion:
The Renderer.RenderHtmlAsPdf() method is called with the HTML string to generate a PDF document. This method processes the HTML content and converts it into a PDF format.
Saving the PDF:
The generated PDF document is saved to a file with the name "html2Pdf.pdf" using the SaveAs() method. This stores the newly created PDF on the disk.
Add from PixabayUpload
or drag and drop an image here
Add image alt text
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your code";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></html>";
string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
string footerHtml = "<div style='text-align: center;'>Confidential</div>";
// Convert the HTML content to a PDF document with headers and footers
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
pdfDocument.SaveAs("report.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your code";
// Create a new HtmlToPdf object
var Renderer = new ChromePdfRenderer();
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></html>";
string headerHtml = "<div style='text-align: right;'>Page {page} of {total-pages}</div>";
string footerHtml = "<div style='text-align: center;'>Confidential</div>";
// Convert the HTML content to a PDF document with headers and footers
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
pdfDocument.SaveAs("report.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your code"
' Create a new HtmlToPdf object
Dim Renderer = New ChromePdfRenderer()
Dim htmlContent As String = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></html>"
Dim headerHtml As String = "<div style='text-align: right;'>Page {page} of {total-pages}</div>"
Dim footerHtml As String = "<div style='text-align: center;'>Confidential</div>"
' Convert the HTML content to a PDF document with headers and footers
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlHeader= New HtmlHeaderFooter() With {.HtmlFragment=headerHtml},
.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment=footerHtml}
})
pdfDocument.SaveAs("report.pdf")
End Sub
End Class
License Key Setup:
Create the PDF Renderer:
Define HTML Content:
Define Header and Footer HTML:
Custom header and footer HTML strings are specified:
HTML to PDF Conversion:
Adding Headers and Footers:
Saving the PDF:
Add from PixabayUpload
or drag and drop an image here
Add image alt text
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
// Define the HTML content with links to external CSS and JS files
string htmlContent = @"
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
// Define the HTML content with links to external CSS and JS files
string htmlContent = @"
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>";
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
' Define the HTML content with links to external CSS and JS files
Dim htmlContent As String = "
<html>
<head>
<link rel='stylesheet' type='text/css' href='styles.css'>
<script src='script.js'></script>
</head>
<body>
<h1>IronPDF: An Awesome PDF Generation Library</h1>
<h1>Styled Content</h1>
<p id='dynamic-text'>This content is styled using an external CSS file and JavaScript.</p>
</body>
</html>"
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf")
End Sub
End Class
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}
/* styles.css */
body {
font-family: Arial, sans-serif;
margin: 20px;
}
h1 {
color: #007BFF;
}
p {
font-size: 14px;
line-height: 1.6;
}
' styles.css
body
If True Then
font-family: Arial, sans-serif
margin:
20px
End If
h1
If True Then
color:
#007BFF;
End If
p
If True Then
font-size: 14px
line-height: 1.6
End If
// script.js
document.addEventListener('DOMContentLoaded', function() {
var dynamicText = document.getElementById('dynamic-text');
dynamicText.textContent = "This content has been modified by JavaScript.";
});
// script.js
document.addEventListener('DOMContentLoaded', function() {
var dynamicText = document.getElementById('dynamic-text');
dynamicText.textContent = "This content has been modified by JavaScript.";
});
' script.js
document.addEventListener( 'DOMContentLoaded', @function() { var dynamicText = document.getElementById('dynamic-text'); dynamicText.textContent = "This content has been modified by JavaScript."; });
This code demonstrates how to use IronPDF in C# to generate a PDF from HTML content that includes links to external CSS and JavaScript files. It shows how to create a PDF with styled content and dynamic behavior (via JavaScript).
License Key Setup:
Define HTML Content with External Resources:
The HTML string is defined with:
A link to an external CSS file (styles.css) to style the content.
) with an ID dynamic-text that is styled by the external CSS and potentially modified by the JavaScript.
Rendering HTML to PDF:
Saving the PDF:
Add from PixabayUpload
or drag and drop an image here
Add image alt text
Invoice Generation
Reports and Data Analysis
Legal and Contract Documents
Educational Materials
Forms and Surveys
IronPDF. Place it before using IronPDF library like below
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key";
IronPdf.License.LicenseKey = "your key"
IronPDF is a powerful and versatile library that makes converting HTML to PDF a breeze. Whether you need to generate simple documents or complex reports with dynamic content, IronPDF has you covered. With its easy-to-use API and support for advanced features like headers, footers, and external resources, IronPDF is an invaluable tool for developers looking to create high-quality PDF documents from HTML content. Give it a try in your next project and experience the convenience of effortless HTML to PDF conversion.