Are you looking for a reliable way to generate PDF documents in your .NET Core application using C#? Look no further! In this tutorial, we will explore the dinktopdf library and walk you through a step-by-step example of how to use it to generate PDFs.
What is dinktopdf?
DinkToPdf is a powerful .NET Core library that uses the WebKit rendering engine to convert HTML content to PDF. It provides a simple and straightforward way to generate professional-looking PDF documents from HTML templates or raw HTML code.
Step 1: Setting up the project
Before we dive into the code, make sure you have a .NET Core project set up. You can create a new project or use an existing one. Once your project is ready, open your preferred development environment.
Step 2: Installing the dinktopdf package
To use dinktopdf in your project, you need to install the dinktopdf NuGet package. Open the NuGet Package Manager Console and run the following command:
Install-Package DinkToPdf
This command will install the latest version of the dinktopdf package and its dependencies into your project.
Step 3: Creating a PDF generator class
Now, let’s create a PDF generator class that will handle the conversion of HTML to PDF. In your project, add a new class file and name it ‘PdfGenerator.cs’.
using DinkToPdf;
namespace YourNamespace
{
public class PdfGenerator
{
private readonly SynchronizedConverter _converter;
public PdfGenerator()
{
var globalSettings = new GlobalSettings()
{
PaperSize = PaperKind.A4,
Orientation = Orientation.Portrait,
Margins = new MarginSettings { Top = 10 },
DocumentTitle = “Generated PDF”
};
_converter = new SynchronizedConverter(new PdfTools());
}
public byte[] GeneratePdfFromHtml(string html)
{
var document = new HtmlToPdfDocument()
{
GlobalSettings = globalSettings,
Objects = { new ObjectSettings { HtmlContent = html } }
};
return _converter.Convert(document);
}
}
}
This class initializes the converter and provides a method ‘GeneratePdfFromHtml’ that takes an HTML string as input and returns the generated PDF as a byte array.
Step 4: Generating the PDF
Now that our PDF generator class is ready, let’s see how to use it to generate a PDF. In your desired code location, create an instance of the ‘PdfGenerator’ class and call the ‘GeneratePdfFromHtml’ method with your desired HTML content.
var pdfGenerator = new PdfGenerator(); var htmlContent = "
<h1>Hallo Welt</h1>"; var pdfBytes = pdfGenerator.GeneratePdfFromHtml(htmlContent);
The ‘pdfBytes’ variable now contains the generated PDF in byte array format. You can save it to a file or send it as a response in your web application.
Conclusion
Congratulations! You have successfully learned how to use the dinktopdf library in .NET Core to generate PDF documents using C#. With the help of the dinktopdf library, you can easily convert HTML content into professional-looking PDFs in your applications. Feel free to explore the library further and leverage its rich features to enhance your PDF generation process.
Leave a Reply