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 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.
Advanced Rendering: It uses Chrome’s rendering engine (via Chromium) for accurate, high-quality PDF generation, making it more reliable than many other HTML-to-PDF libraries.
Custom Headers and Footers
IronPDF allows developers to add custom headers and footers to PDF documents, which can include dynamic content such as page numbers, document title, or custom text.
Support for JavaScript in PDFs
IronPDF enables JavaScript execution within the HTML content before PDF generation. This allows for dynamic content rendering, such as form calculations or interactivity in the generated PDFs.
Edit Existing PDFs
IronPDF provides the ability to edit existing PDFs. You can modify text, images, and add annotations to existing PDF files. This feature is useful for watermarking documents, adding signatures, or updating content within PDF files.
Merge and Split PDFs
Support for Interactive Forms
You can create, fill, and manipulate PDF forms using IronPDF. It provides full support for interactive forms (like text fields, checkboxes, and radio buttons) and allows you to pre-fill forms with data.
Page Manipulation
IronPDF offers various methods for manipulating individual pages within a PDF document, such as rotating pages, deleting pages, or reordering them. This helps in customizing the structure of the final document.
Security and Encryption
IronPDF allows you to apply password protection and encryption to PDFs, ensuring that your documents are secure. You can set user permissions, such as preventing printing, copying, or editing the PDF.
Watermarking and Branding
Adding watermarks to PDF documents is easy with IronPDF. You can overlay text or images as watermarks onto pages, providing protection against unauthorized copying or distributing of your documents.
Text and Image Extraction
IronPDF allows for text and image extraction from PDF documents, enabling developers to extract data for processing or reuse.
Unicode and Multi-language Support
IronPDF has robust Unicode support, meaning it can handle international characters and fonts, making it ideal for generating PDFs in multiple languages.
Optimized for Performance
API and Developer-Friendly Tools
IronPDF comes with a comprehensive and easy-to-use API. Developers can quickly get started by using simple method calls to perform complex tasks.
Cross-Platform Support
To get started, open Visual Studio and create a new project as below.
Select create console application.
Provide project name and location.
Select .NET version
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
Also, IronPDF can be installed using Visual Studio Package Manager.
Below is code for an HTML to PDF converter application. Here, an HTML string is used as input and converted to a PDF. You can convert an HTML file to a PDF using the same steps, with an additional step to read the content from the HTML file.
using IronPdf;
class Program
{
static void Main()
{
// Specify license key
IronPdf.License.LicenseKey = "Your Key";
// Create a new ChromePdfRenderer object
var Renderer = new ChromePdfRenderer();
// Define the HTML string to be converted
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");
}
}
using IronPdf;
class Program
{
static void Main()
{
// Specify license key
IronPdf.License.LicenseKey = "Your Key";
// Create a new ChromePdfRenderer object
var Renderer = new ChromePdfRenderer();
// Define the HTML string to be converted
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");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Specify license key
IronPdf.License.LicenseKey = "Your Key"
' Create a new ChromePdfRenderer object
Dim Renderer = New ChromePdfRenderer()
' Define the HTML string to be converted
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:
Creating the Renderer:
HTML Content:
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:
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.SaveAs()
method. This stores the newly created PDF on the disk.using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
// Create a new ChromePdfRenderer object
var Renderer = new ChromePdfRenderer();
// Define HTML content
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></html>";
// Define header and footer 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
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
// Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
// Save the PDF document to a file
pdfDocument.SaveAs("report.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
// Create a new ChromePdfRenderer object
var Renderer = new ChromePdfRenderer();
// Define HTML content
string htmlContent = "<html><body><h1>IronPDF: An Awesome PDF Generation Library</h1><h1>Report</h1><p>This is a sample report.</p></body></html>";
// Define header and footer 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
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
// Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(new ChromePdfRenderOptions
{
HtmlHeader= new HtmlHeaderFooter() { HtmlFragment=headerHtml },
HtmlFooter = new HtmlHeaderFooter() { HtmlFragment=footerHtml }
});
// Save the PDF document to a file
pdfDocument.SaveAs("report.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
' Create a new ChromePdfRenderer object
Dim Renderer = New ChromePdfRenderer()
' Define HTML content
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>"
' Define header and footer 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
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
' Add headers and footers to the PDF document
pdfDocument.AddHtmlHeadersAndFooters(New ChromePdfRenderOptions With {
.HtmlHeader= New HtmlHeaderFooter() With {.HtmlFragment=headerHtml},
.HtmlFooter = New HtmlHeaderFooter() With {.HtmlFragment=footerHtml}
})
' Save the PDF document to a file
pdfDocument.SaveAs("report.pdf")
End Sub
End Class
License Key Setup:
IronPdf.License.LicenseKey
is set with your unique IronPDF license key to enable the library's full functionality.Create the PDF Renderer:
ChromePdfRenderer
is created, which will be used to render HTML content into a PDF format. This is part of IronPDF's rendering engine.Define HTML Content:
Define Header and Footer HTML:
HTML to PDF Conversion:
RenderHtmlAsPdf()
method is called to convert the HTML content into a PDF document.Adding Headers and Footers:
AddHtmlHeadersAndFooters()
method is called on the pdfDocument
object. This method adds the previously defined header and footer to the generated PDF. The ChromePdfRenderOptions
is used to pass the header and footer settings.SaveAs()
method is used to save the generated PDF to a file named "report.pdf" on the disk.using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
// Create a new ChromePdfRenderer object
var Renderer = new ChromePdfRenderer();
// 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>";
// Convert HTML content to a PDF document
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a file
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
using IronPdf;
class Program
{
static void Main()
{
IronPdf.License.LicenseKey = "your key";
// Create a new ChromePdfRenderer object
var Renderer = new ChromePdfRenderer();
// 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>";
// Convert HTML content to a PDF document
var pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the PDF document to a file
pdfDocument.SaveAs("awesomeIronPDF_styled_content.pdf");
}
}
Imports IronPdf
Friend Class Program
Shared Sub Main()
IronPdf.License.LicenseKey = "your key"
' Create a new ChromePdfRenderer object
Dim Renderer = New ChromePdfRenderer()
' 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>"
' Convert HTML content to a PDF document
Dim pdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the PDF document to a file
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;
}
// 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:
IronPdf.License.LicenseKey
is set to your unique IronPDF license key, allowing access to full features of the IronPDF library.Define HTML Content with External Resources:
The HTML string is defined with:
<h1>
), a subheading, and a paragraph (<p>
) with an ID dynamic-text
that is styled by the external CSS and potentially modified by the JavaScript.Rendering HTML to PDF:
Renderer.RenderHtmlAsPdf()
method is called to convert the HTML content, including the linked CSS and JavaScript, into a PDF document. Note that IronPDF can handle external resources like CSS and JavaScript if the resources are accessible, such as when the files are hosted locally or remotely.Saving the PDF:
SaveAs()
method.styles.css
and script.js
files should be accessible in the environment where the code runs. If the files are local, make sure they are in the correct directory or provide the full path to the files.Invoice Generation
Reports and Data Analysis
Legal and Contract Documents
Educational Materials
Forms and Surveys
To get started with IronPDF, you can access a trial license. Include it at the beginning of your code as shown 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.