Skip to content

About

A simple wrapper for wkhtmltopdf in ASP.NET Core

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

DevBox.WkHtmlToPdf

NuGet Version

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

  1. Add DevBox.WkHtmlToPdf package to your project:
dotnet add package DevBox.WkHtmlToPdf
  1. 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.

  1. 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
  1. If you work with Linux or Docker, you need to install libgdiplus and libc6-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.

  1. Converting a HTML:
var buffer = await _pdfConverterService.FromHtmlAsync("<html>...</html>", options =>
{
    // Overrides the default options
    options.Title = "My Report";
});
  1. Converting a Razor View:
var buffer = await _pdfConverterService.FromViewAsync("PathToView/ViewName", model, options =>
{
    // If "true", you'll find all temporary files used by wkhtmltopdf in "bin/Debug/.../WkHtmlToPdf/Temp"
    // or the path specified in ".SetTempPath"
    options.KeepTempFiles = Debugger.IsAttached;

    // Overrides the default options
    options.HeaderFooter.HeaderHtml = "PathToView/HeaderViewName";
});

About

A simple wrapper for wkhtmltopdf in ASP.NET Core

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages