HTML Files to PDF
Convert HTML to PDF using IronPDF
To convert an HTML file to a PDF document, you can use the fromHtml
method provided by the IronPDF library from Iron Software. The resulting PDF will include the complete content from your HTML file.
Below is an example of how you can achieve this using C#:
// Include the necessary namespace for IronPDF
using IronPdf;
namespace HtmlToPdfExample
{
class Program
{
static void Main(string[] args)
{
// Define the path to your HTML file
var htmlFilePath = "path/to/yourfile.html";
// Create an instance of the IronPdf Renderer
var renderer = new HtmlToPdf();
// Convert the HTML file to a PDF document
var pdf = renderer.RenderHtmlFileAsPdf(htmlFilePath);
// Define the output path for the PDF document
var outputPdfPath = "output.pdf";
// Save the rendered PDF document to the specified path
pdf.SaveAs(outputPdfPath);
// Output success message
Console.WriteLine($"PDF saved successfully to {outputPdfPath}");
}
}
}
// Include the necessary namespace for IronPDF
using IronPdf;
namespace HtmlToPdfExample
{
class Program
{
static void Main(string[] args)
{
// Define the path to your HTML file
var htmlFilePath = "path/to/yourfile.html";
// Create an instance of the IronPdf Renderer
var renderer = new HtmlToPdf();
// Convert the HTML file to a PDF document
var pdf = renderer.RenderHtmlFileAsPdf(htmlFilePath);
// Define the output path for the PDF document
var outputPdfPath = "output.pdf";
// Save the rendered PDF document to the specified path
pdf.SaveAs(outputPdfPath);
// Output success message
Console.WriteLine($"PDF saved successfully to {outputPdfPath}");
}
}
}
CONVERTER NOT RUNNING
Explanation:
- Namespace:
using IronPdf;
is necessary to access the IronPDF library's functionalities. - Renderer Creation:
HtmlToPdf renderer = new HtmlToPdf();
instantiates a new HTML to PDF renderer. - Convert HTML to PDF:
renderer.RenderHtmlFileAsPdf(htmlFilePath);
reads the HTML file and converts it into a PDF document. - Save PDF:
pdf.SaveAs(outputPdfPath);
saves the generated PDF to the specified file path.
Exporting the PDF
After rendering the HTML to PDF, you can use the SaveAs
method to export the document as 'output.pdf' in the current directory or to a specified path of your choice.
For more information on how to effectively use IronPDF for your PDF creation and manipulation needs, you can explore the IronPDF documentation on Iron Software's official website.