HTML String to PDF
Use the fromHtml
method to convert HTML strings and files into PDF documents. The resulting PDF document should be identical to the one generated by the print feature of the Google Chrome browser. IronPDF utilizes the Chrome engine for rendering HTML to PDF documents, ensuring the same output when using the same configuration as Google Chrome.
In addition to its rendering capabilities, IronPDF offers a wealth of features for manipulating PDF documents. It seamlessly combines rendering and manipulation into a set of new features. For example, consider the HTML stamping feature of IronPDF, which involves rendering HTML and merging the resulting PDF with the main HTML content to produce a final document with HTML stamps either behind or in front of the primary content.
using IronPdf; // Import the IronPDF library
namespace PdfGeneration
{
class Program
{
static void Main(string[] args)
{
// Define the HTML content to be converted
string htmlContent = "<html><body><h1>This is a sample PDF document</h1><p>Generated using IronPDF.</p></body></html>";
// Create a new instance of the HtmlToPdf class
HtmlToPdf Renderer = new HtmlToPdf();
// Use the RenderHtmlAsPdf method to convert the HTML to a PDF document
PdfDocument pdf = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the created PDF to the specified file path
pdf.SaveAs("output.pdf");
// Notify the user that the PDF generation is complete
Console.WriteLine("PDF generated and saved as 'output.pdf'.");
}
}
}
using IronPdf; // Import the IronPDF library
namespace PdfGeneration
{
class Program
{
static void Main(string[] args)
{
// Define the HTML content to be converted
string htmlContent = "<html><body><h1>This is a sample PDF document</h1><p>Generated using IronPDF.</p></body></html>";
// Create a new instance of the HtmlToPdf class
HtmlToPdf Renderer = new HtmlToPdf();
// Use the RenderHtmlAsPdf method to convert the HTML to a PDF document
PdfDocument pdf = Renderer.RenderHtmlAsPdf(htmlContent);
// Save the created PDF to the specified file path
pdf.SaveAs("output.pdf");
// Notify the user that the PDF generation is complete
Console.WriteLine("PDF generated and saved as 'output.pdf'.");
}
}
}
Imports IronPdf ' Import the IronPDF library
Namespace PdfGeneration
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Define the HTML content to be converted
Dim htmlContent As String = "<html><body><h1>This is a sample PDF document</h1><p>Generated using IronPDF.</p></body></html>"
' Create a new instance of the HtmlToPdf class
Dim Renderer As New HtmlToPdf()
' Use the RenderHtmlAsPdf method to convert the HTML to a PDF document
Dim pdf As PdfDocument = Renderer.RenderHtmlAsPdf(htmlContent)
' Save the created PDF to the specified file path
pdf.SaveAs("output.pdf")
' Notify the user that the PDF generation is complete
Console.WriteLine("PDF generated and saved as 'output.pdf'.")
End Sub
End Class
End Namespace
Explanation of the C# Code
Library Import: The
IronPdf
namespace is imported to allow the use of IronPDF library functions for HTML to PDF conversion.HTML Definition: A simple HTML string is defined, which will be rendered into a PDF.
HtmlToPdf Renderer Initialization: An instance of
HtmlToPdf
is created. This instance will be responsible for handling the conversion of HTML to PDF.Rendering HTML to PDF: The
RenderHtmlAsPdf
method on theHtmlToPdf
object is called with the HTML string. This method converts the HTML content into a PDF document.Saving the PDF: The generated PDF document is saved to a file named
output.pdf
using theSaveAs
method.- User Notification: The console outputs a message notifying the user that the PDF has been generated and saved successfully.