im using this libwkhtmltox.dll to convert my html to pdf. Im using c# .netCore.
Usage:
private static string CreatePdf(string content, string fileName)
{
var fullPath = $"{_projectSettingsOptions.Pdf}/{fileName}";
using (var sw = new StreamWriter(new FileStream(fullPath, FileMode.Create), Encoding.UTF8))
{
sw.Write(content);
sw.Flush();
}
new Pdf(_projectSettingsOptions).Convert(fullPath, content);
return fullPath;
}
About the code above:
Then i call the Pdf class, passing the path and the content to convert.
new Pdf(_projectSettingsOptions).Convert(fullPath, content);
This is my Pdf class:
namespace SiteMFPManager.Library.Util
{
using Assembly;
using DinkToPdf;
using Settings;
using System.IO;
public class Pdf
{
public Pdf(ProjectSettingsOptions projectSettingsOptions) =>
new CustomAssemblyLoadContext().LoadUnmanagedLibrary(Path.Combine(projectSettingsOptions.References, "libwkhtmltox", "libwkhtmltox.dll"));
public void Convert(string fullPath, string content) =>
new SynchronizedConverter(new PdfTools()).Convert(new HtmlToPdfDocument
{
GlobalSettings =
{
ColorMode = ColorMode.Color,
Orientation = Orientation.Portrait,
PaperSize = PaperKind.A4,
Margins = new MarginSettings { Top = 10 },
Out = fullPath,
},
Objects =
{
new ObjectSettings
{
PagesCount = true,
HtmlContent = content,
WebSettings = { DefaultEncoding = "utf-8" }
}
}
});
}
}
The CreatePdf method its executed two times. In the first time, it executes and show to me in the console, this: Qt: Could not initialize OLE (error 80010106), when the code is executed for the second time, the application stops, no exceptions happen, nothing... Just that message.
If you need more information to help me with this problem, just tell me.
Sorry if the post is bad formatted, im new to this...
To get the SynchronizedConverter working properly in .Net Core you need to register it as a Singleton (Most likely in Startup.cs ConfigureServices):
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools()));
(Also described here: https://github.com/rdvojmoc/DinkToPdf#dependancy-injection)
After that you can inject the Converter into your Controller instead of creating a new instance. This way the DLL gets called always in the same Thread.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With