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:
- content = The html content i want to convert to pdf.
- fullPath = Where the html content will be stored.
- _projectSettingsOptions = Paths of the project, like image paths, pdf paths and so on...
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...