Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
In today's tutorial, we explore a comparison between two popular C# libraries for converting HTML to PDF: Iron PDF and Dink to PDF. We begin by ensuring both libraries are installed using the NuGet package manager. Iron PDF utilizes a Chrome PDF renderer instance to convert HTML content into high-quality PDF documents, easily handling both HTML strings and URLs. In contrast, Dink to PDF involves defining global and object settings for PDF creation, serving as a wrapper for the WKHTML to PDF library. While both libraries are effective, Iron PDF provides a simpler, more pixel-perfect solution with dedicated features for PDF handling. Dink to PDF's strength lies in its configuration flexibility. Ultimately, the choice depends on your specific application needs. Experiment with both to determine which suits your project best. For further exploration, check out the software link provided to download and test these libraries yourself.
Here's a basic example of how to use each library:
// Ensure you have installed the IronPDF NuGet package before running this example.
using IronPdf;
class Program
{
static void Main()
{
// Create a PDF Renderer using IronPDF
var Renderer = new HtmlToPdf();
// Render an HTML string as a PDF document
var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");
// Save the PDF document to the file system
PDF.SaveAs("HelloWorld.pdf");
// Open the PDF document in the default viewer
System.Diagnostics.Process.Start("HelloWorld.pdf");
}
}
// Ensure you have installed the IronPDF NuGet package before running this example.
using IronPdf;
class Program
{
static void Main()
{
// Create a PDF Renderer using IronPDF
var Renderer = new HtmlToPdf();
// Render an HTML string as a PDF document
var PDF = Renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");
// Save the PDF document to the file system
PDF.SaveAs("HelloWorld.pdf");
// Open the PDF document in the default viewer
System.Diagnostics.Process.Start("HelloWorld.pdf");
}
}
' Ensure you have installed the IronPDF NuGet package before running this example.
Imports IronPdf
Friend Class Program
Shared Sub Main()
' Create a PDF Renderer using IronPDF
Dim Renderer = New HtmlToPdf()
' Render an HTML string as a PDF document
Dim PDF = Renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>")
' Save the PDF document to the file system
PDF.SaveAs("HelloWorld.pdf")
' Open the PDF document in the default viewer
System.Diagnostics.Process.Start("HelloWorld.pdf")
End Sub
End Class
// Ensure you have installed the DinkToPdf and DinkToPdf.Native.System32 NuGet packages before running this example.
using System.Diagnostics;
using DinkToPdf;
using DinkToPdf.Contracts;
class Program
{
static void Main()
{
// Initialize a converter
var converter = new BasicConverter(new PdfTools());
// Define global settings for PDF conversion
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 }
};
// Define object settings to convert specific html
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = "<h1>Hello, World!</h1>",
WebSettings = { DefaultEncoding = "utf-8" },
HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }
};
// Create the PDF document with specified settings
var pdfDocument = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
// Convert the document and save to the file system
converter.Convert(pdfDocument, "HelloWorld.pdf");
// Open the PDF document in the default viewer
Process.Start("HelloWorld.pdf");
}
}
// Ensure you have installed the DinkToPdf and DinkToPdf.Native.System32 NuGet packages before running this example.
using System.Diagnostics;
using DinkToPdf;
using DinkToPdf.Contracts;
class Program
{
static void Main()
{
// Initialize a converter
var converter = new BasicConverter(new PdfTools());
// Define global settings for PDF conversion
var globalSettings = new GlobalSettings
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 }
};
// Define object settings to convert specific html
var objectSettings = new ObjectSettings
{
PagesCount = true,
HtmlContent = "<h1>Hello, World!</h1>",
WebSettings = { DefaultEncoding = "utf-8" },
HeaderSettings = { FontName = "Arial", FontSize = 9, Right = "Page [page] of [toPage]", Line = true }
};
// Create the PDF document with specified settings
var pdfDocument = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { objectSettings }
};
// Convert the document and save to the file system
converter.Convert(pdfDocument, "HelloWorld.pdf");
// Open the PDF document in the default viewer
Process.Start("HelloWorld.pdf");
}
}
' Ensure you have installed the DinkToPdf and DinkToPdf.Native.System32 NuGet packages before running this example.
Imports System.Diagnostics
Imports DinkToPdf
Imports DinkToPdf.Contracts
Friend Class Program
Shared Sub Main()
' Initialize a converter
Dim converter = New BasicConverter(New PdfTools())
' Define global settings for PDF conversion
Dim globalSettings As New GlobalSettings With {
.ColorMode = ColorMode.Color,
.Orientation = Orientation.Portrait,
.PaperSize = PaperKind.A4,
.Margins = New MarginSettings With {.Top = 10}
}
' Define object settings to convert specific html
Dim objectSettings As New ObjectSettings With {
.PagesCount = True,
.HtmlContent = "<h1>Hello, World!</h1>",
.WebSettings = { DefaultEncoding = "utf-8" },
.HeaderSettings = {
FontName = "Arial",
FontSize = 9,
Right = "Page [page] of [toPage]",
Line = True
}
}
' Create the PDF document with specified settings
Dim pdfDocument = New HtmlToPdfDocument() With {
.GlobalSettings = globalSettings,
.Objects = { objectSettings }
}
' Convert the document and save to the file system
converter.Convert(pdfDocument, "HelloWorld.pdf")
' Open the PDF document in the default viewer
Process.Start("HelloWorld.pdf")
End Sub
End Class
Further Reading: Exploring Alternatives to DinkToPDF for HTML to PDF Conversion