This project implements the conversion of HTML to PDF using wkhtmltopdf. It works only at windows and linux, on x64 arch.
Package | Version | Downloads |
---|---|---|
WkHtmlSmartConvert | ||
WkHtmlSmartConvert.Embedded |
Packages with suport to .Net Core 2.1, 3.1 and .Net 5.0
-
Install the standard nuget package into your application.
dotnet add package WkHtmlSmartConvert
-
In the
ConfigureServices
method ofStartup.cs
, register the WkHtmlSmartConvert.services .AddWkHtmlSmartConvert() .AddPdf();
or, you can configure global options to every conversion, see PdfOptions.
services .AddWkHtmlSmartConvert() .AddPdf(options => { options.PageOrientation = PageOrientation.Landscape, options.IsGrayScale = true, options.Copies = 2, options.PageSize = PageSize.A0 });
The standard configuration looks for wkhtmltopdf installed on host and the environment variable PATH correctly configure, but instead of this, you can use the wkhtmltopdf embedded on the application.
-
Install the additional nuget package into your application.
dotnet add package WkHtmlSmartConvert.Embedded
-
Configure AddEmbedded just after AddWkHtmlSmartConvert.
services .AddWkHtmlSmartConvert() .AddEmbedded() .AddPdf();
You must inject IPdfConvert and call method ConvertAsync
public class ExamplePdfConvert
{
private readonly IPdfConvert _pdfConvert
public ExamplePdfConvert(IPdfConvert pdfConvert){
_pdfConvert = pdfConvert;
}
public async Task<byte[]> GeneratePdfAsync() {
var buffer = await _pdfConvert.ConvertAsync("<html>Hello World!</html>");
return buffer;
}
}