DevBox.WkHtmlToPdf
1.0.8
dotnet add package DevBox.WkHtmlToPdf --version 1.0.8
NuGet\Install-Package DevBox.WkHtmlToPdf -Version 1.0.8
<PackageReference Include="DevBox.WkHtmlToPdf" Version="1.0.8" />
<PackageVersion Include="DevBox.WkHtmlToPdf" Version="1.0.8" />
<PackageReference Include="DevBox.WkHtmlToPdf" />
paket add DevBox.WkHtmlToPdf --version 1.0.8
#r "nuget: DevBox.WkHtmlToPdf, 1.0.8"
#:package DevBox.WkHtmlToPdf@1.0.8
#addin nuget:?package=DevBox.WkHtmlToPdf&version=1.0.8
#tool nuget:?package=DevBox.WkHtmlToPdf&version=1.0.8
DevBox.WkHtmlToPdf
A simple wrapper for wkhtmltopdf, enabling conversion of HTML or Razor Views directly into PDF files within ASP.NET Core applications.
This is a refactor from Wkhtmltopdf.NetCore since is deprecated.
It's also important to read this recommendations.
Installation
- Add DevBox.WkHtmlToPdf package to your project:
dotnet add package DevBox.WkHtmlToPdf
- Download the desirable version of wkhtmltopdf and add to your project:
.
├── MyProject
│ ├── WkHtmlToPdf
│ │ ├── Windows
│ │ │ └── wkhtmltopdf.exe
│ │ └── Linux
│ │ └── wkhtmltopdf
│ └── MyProject.csproj
└── MyProject.sln
OS/Distribution | Supported on | Architectures |
---|---|---|
Windows | 64-bit | |
Debian | 11 bullseye | 64-bit |
The package could copy the binaries automatically and verify the OS, but it would be large. This way the executable becomes configurable.
- Edit your
.csproj
:
<ItemGroup>
<Content Include="WkHtmlToPdf\**\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
If you have wkhtmltopdf installed in your server, you can skip item 2 configuring through
SetExecutableFilePath
using an absolute path.
- Configure your
IServiceCollection
:
services.AddHttpContextAccessor();
services.AddWkHtmlToPdf(options =>
{
options.SetExecutableFilePath("custom-absolute-path/wkhtmltopdf.exe");
// Customize the default options
options.Title = "My PDF";
options.PageSize = PageSize.A4;
options.Orientation = PdfOrientation.Landscape;
// ...
});
- Default executable file path:
- Windows:
WkHtmlToPdf/Windows/wkhtmltopdf.exe
- Linux:
WkHtmlToPdf/Linux/wkhtmltopdf
- MAC:
WkHtmlToPdf/Mac/wkhtmltopdf
- Windows:
- If you work with Linux or Docker, you need to install
libgdiplus
andlibc6-dev
.
apt-get update
apt-get -y install libgdiplus libc6-dev
FROM mcr.microsoft.com/dotnet/aspnet:7.0
# ...
RUN apt-get update -qq && apt-get -y install libgdiplus libc6-dev
- If you have problems with differents fonts in Windows vs Docker, you can try:
RUN apt-get update -qq && apt-get -y install libgdiplus libc6-dev fontconfig fonts-liberation
RUN fc-cache -f -v
Usage
Inject IPdfConverterService
in any class you need.
- Converting a HTML:
var buffer = await _pdfConverterService.FromHtmlAsync("<html>...</html>", options =>
{
// Overrides the default options
options.Title = "My Report";
});
- Converting a Razor View:
var buffer = await _pdfConverterService.FromViewAsync("PathToView/ViewName", model, options =>
{
// Overrides the default options
options.HeaderFooter.HeaderHtml = "PathToView/HeaderViewName";
});
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net7.0
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.8 | 587 | 4/9/2025 |