Welcome to our guide on using DinkToPdf, a .NET Core PInvoke wrapper for the wkhtmltopdf library. This powerful library leverages the Webkit engine to seamlessly convert HTML pages into PDF documents. In this article, we will walk you through the installation process, different types of converters, and provide useful troubleshooting tips!
Installation
To get started with DinkToPdf, you’ll first need to install the library via NuGet. Follow the steps below:
- Open the Package Manager Console in your project.
- Run the following command:
PM Install-Package DinkToPdf
Important Considerations
Please note that the DinkToPdf library has not been tested with IIS. It has been tested in console applications and with the Kestrel web server for Web Application and Web API purposes.
Setting Up Converters
Once the library is installed, you can choose between two types of converters based on your application’s requirements:
1. Basic Converter
This converter is intended for single-threaded applications. Here’s how to create one:
var converter = new BasicConverter(new PdfTools());
2. Synchronized Converter
For multi-threaded applications and web servers, use the synchronized converter which saves conversion tasks to a blocking collection executed on a single thread.
var converter = new SynchronizedConverter(new PdfTools());
Defining the Document to Convert
Next, you will need to define the document that you want to convert. Think of this step like preparing a recipe—gathering all your ingredients before cooking!
var doc = new HtmlToPdfDocument()
{
GlobalSettings = new GlobalSettings()
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Landscape,
PaperSize = PaperKind.A4Plus,
},
Objects = new List
{
new ObjectSettings()
{
PagesCount = true,
HtmlContent = @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. In consectetur mauris eget ultrices iaculis. Ut odio viverra, molestie lectus nec, venenatis turpis.",
WebSettings = new WebSettings()
{
DefaultEncoding = "utf-8"
},
HeaderSettings = new HeaderSettings()
{
FontSize = 9,
Right = "Page [page] of [toPage]",
Line = true,
Spacing = 2.812
}
}
}
};
Converting the Document
Now that your document is set up, let’s convert it to PDF. The process of saving the result can vary depending on whether you want to store it in a byte array or save it directly to disk.
Saving in a Byte Array
If the ‘Out’ property is an empty string, the result will be saved in a byte array:
byte[] pdf = converter.Convert(doc);
Saving to Disk
If the ‘Out’ property is defined, it will save the file directly to disk:
var doc = new HtmlToPdfDocument()
{
GlobalSettings = new GlobalSettings()
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings() { Top = 10 },
Out = @"C:\DinkToPdf\src\DinkToPdf.TestThreadSafe\test.pdf",
},
Objects = new List
{
new ObjectSettings()
{
Page = "http://google.com",
}
}
};
Now convert it!
converter.Convert(doc);
Dependency Injection
To ensure that the converter is properly managed, register it as a singleton in your application’s service container:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
}
Troubleshooting
If you encounter any issues, here are some helpful tips to resolve common problems:
- Ensure that the native library is correctly copied to your project’s root folder.
- Check that you selected the right architecture for your operating system.
- If the output is not as expected, double-check the HTML content you provided in the document settings.
- For further help, you can reach out at the official GitHub page for community support.
- For more insights, updates, or to collaborate on AI development projects, stay connected with [fxis.ai](https://fxis.ai).
At [fxis.ai](https://fxis.ai), we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.
Conclusion
In this article, we’ve walked through the steps to effectively use DinkToPdf for converting HTML to PDF. Whether you’re building a console application or a multi-threaded web server setup, DinkToPdf provides versatile and robust solutions to meet your needs.