SwapToPdf 1.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package SwapToPdf --version 1.0.3                
NuGet\Install-Package SwapToPdf -Version 1.0.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SwapToPdf" Version="1.0.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SwapToPdf --version 1.0.3                
#r "nuget: SwapToPdf, 1.0.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SwapToPdf as a Cake Addin
#addin nuget:?package=SwapToPdf&version=1.0.3

// Install SwapToPdf as a Cake Tool
#tool nuget:?package=SwapToPdf&version=1.0.3                

SwapToPdf

SwapToPdf is a .NET library that allows developers to convert HTML documents to PDF files. It provides an easy-to-use API for integrating HTML-to-PDF conversion into .NET applications. SwapToPdf is based on the Webkit rendering engine and can handle complex layouts and styles. It's often used for generating reports, invoices, and other documents that need to be converted from HTML to PDF format.

Installation

Install SwapToPdf with NuGet

  Install-Package SwapToPdf

Use

Write Code to Convert HTML to PDF: Here's a basic example of how to use SwapToPdf in a .NET application:

Create a Service Declare a service.

namespace HtmlToPdf.Services
{
    public interface IPdfService
    {
        byte[] ConvertHtmlToPdf(string htmlContent);
    }
}



Implement a Service

using SwapToPdf;
using SwapToPdf.Contracts;

namespace HtmlToPdf.Services
{
    public class PdfService:IPdfService
    {
        private readonly ITransfer _converter;

        public PdfService(ITransfer converter)
        {
            _converter = converter;
        }

        public byte[] ConvertHtmlToPdf(string htmlContent)
        {
            var document = new HtmlToPdfDocument
            {
                GlobalSettings = {
                DocumentTitle = "PDF Document",
                PaperSize = PaperKind.A4
            },
                Objects = {
                new ObjectSettings
                {
                    HtmlContent = htmlContent
                }
            }
            };

            return _converter.Transfer(document);
        }
    }
}

Using service on controller.


using HtmlToPdf.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace HtmlToPdf.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class PdfController : ControllerBase
    {
        private readonly IPdfService _pdfService;
        public PdfController(IPdfService pdfService)
        {
            _pdfService = pdfService;
        }

        [HttpPost("convert")]
        public IActionResult ConvertHtmlToPdf([FromBody] string htmlContent)
        {
            var pdfBytes = _pdfService.ConvertHtmlToPdf(htmlContent);
            return File(pdfBytes, "application/pdf", "document.pdf");
        }
    }
}

Configure the Application:

Make sure that the application has access to the native libraries required by SwapToPdf. These libraries are often bundled with the libwkhtmltox.dll package and need to be in the correct directory or accessible from the application's runtime environment.

If the package is not available in your bin folder then download libwkhtmltox.dll from the browser and add that into your "\bin\Release\net8.0" folder.

SwapToPdf in Docker

To generate PDFs from HTML using SwapToPdf in a linux based container, the libwkhtmltox.so library is required. But it takes up a lot of space (43 MB) thereby increasing the time for initial checkouts considerably. A better way, if your app is containerized, is to let the Dockerfile define how to fetch libwkhtmltox when building the base image for your app.

But while running the application you will get following error.

System.AggregateException: 'One or more errors occurred. (Unable to load shared library 'libwkhtmltox' or one of its dependencies. In order to help diagnose loading problems, consider using a tool like strace. If you're using glibc, consider setting the LD_DEBUG environment variable: /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.7/libwkhtmltox.so: cannot open shared object file: No such file or directory /app/bin/Release/net8.0/libwkhtmltox.so: cannot open shared object file: No such file or directory /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.7/liblibwkhtmltox.so: cannot open shared object file: No such file or directory /app/bin/Release/net8.0/liblibwkhtmltox.so: cannot open shared object file: No such file or directory /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.7/libwkhtmltox: cannot open shared object file: No such file or directory /app/bin/Release/net8.0/libwkhtmltox: cannot open shared object file: No such file or directory /usr/share/dotnet/shared/Microsoft.NETCore.App/8.0.7/liblibwkhtmltox: cannot open shared object file: No such file or directory /app/bin/Release/net8.0/liblibwkhtmltox: cannot open shared object file: No such file or directory )'

RUN apt update
RUN apt install -y libgdiplus
RUN ln -s /usr/lib/libgdiplus.so /lib/x86_64-linux-gnu/libgdiplus.so
RUN apt-get install -y --no-install-recommends zlib1g fontconfig libfreetype6 libx11-6 libxext6 libxrender1 wget gdebi
RUN wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb
RUN gdebi --n wkhtmltox_0.12.5-1.stretch_amd64.deb
RUN ln -s /usr/local/lib/libwkhtmltox.so /usr/lib/libwkhtmltox.so
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.4 178 8/10/2024
1.0.3 114 8/10/2024