HakanL / WkHtmlToPdf-DotNet

C# .NET Core wrapper for wkhtmltopdf library that uses Webkit engine to convert HTML pages to PDF.
GNU Lesser General Public License v3.0
366 stars 66 forks source link

docker aspnet:6.0-alpine image not rendered in pdf file #89

Open stefanodesimone opened 1 year ago

stefanodesimone commented 1 year ago

Hi all, in this thread https://github.com/HakanL/WkHtmlToPdf-DotNet/issues/88 we can create pdf using aspnet:6.0-alpine as image base. If in the html file we have an image in base 64 <img src='data:image/jpeg;base64,iVBORw0KGg....... The image is not rendered.

docker file:

`RUN apk update && \ apk add --no-cache \ bash \ icu-libs \ krb5-libs \ libgcc \ libintl \ libssl1.1 \ libstdc++ \ zlib \ libstdc++ \ libx11 \ libxrender \ libxext \ libssl1.1 \ libjpeg \ gcompat \ ca-certificates \ fontconfig \ freetype \ ttf-dejavu \ ttf-droid \ ttf-freefont \ ttf-liberation && \ apk add --no-cache libgdiplus --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ && \ ln -s /usr/lib/libjpeg.so.8 /usr/lib/libjpeg.so.62

FROM mcr.microsoft.com/dotnet/sdk:6.0-alpine AS build WORKDIR /src ...... `

Any suggestions? Thanks in advance Stefano

HakanL commented 1 year ago

Test it in the non-Alpine docker sample first to determine if it's related to Alpine, or wkhtmltopdf. If it doesn't work in either docker container then test it with the native wkhtmltopdf to make sure your HTML is correct and is supported by the parent library (wkhtmltopdf).

stefanodesimone commented 1 year ago

Test it in the non-Alpine docker sample first to determine if it's related to Alpine, or wkhtmltopdf. If it doesn't work in either docker container then test it with the native wkhtmltopdf to make sure your HTML is correct and is supported by the parent library (wkhtmltopdf).

Hi HakanL, i've tried using this image FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base WORKDIR /app EXPOSE 443

Install dependencies

RUN apt-get update \ && apt-get install -y --no-install-recommends \ zlib1g \ libgdiplus \ libc6-dev \ ca-certificates \ fontconfig \ fontconfig-config \ fonts-dejavu-core \ libbsd0 \ libexpat1 \ libfontconfig1 \ libfontenc1 \ libfreetype6 \ libjpeg62-turbo \ libpng16-16 \ libssl1.1 \ libx11-6 \ libx11-data \ libxau6 \ libxcb1 \ libxdmcp6 \ libxext6 \ libxrender1 \ ucf \ x11-common \ xfonts-75dpi \ xfonts-base \ xfonts-encodings \ xfonts-utils \ && rm -rf /var/lib/apt/lists/*

The image in base64 render fine, I think is a package add problem... Stefano

HakanL commented 1 year ago

Ok, good you tested that. So then it's a matter of figuring out which packages are required, or it may be that the libjpeg8 library we had to use in alpine with a soft link isn't handling base64 images (I'm assuming). It's always tricky with native libraries. Please post back if you figure it out, I don't think I can be of much more help here.

ivicaned commented 1 year ago

@stefanodesimone Did you figured out issue and what packages you need to include? If you did, it would be helpful if you can share Docker file sections related to wkhtmltopdf installation/setup, because I run into some issues too.

@HakanL It might be useful to provide instructions for installation on Alpine Linux, as it is the most widely used image for .net/C# projects and running them in production.

HakanL commented 1 year ago

It's quite involved, you have to re-compile wkthtmltopdf from source to make it compatible with Alpine so images work. It may be possible, but I'm not sure it's worth it just to support Alpine. Feel free to look into a PR if you want. Here's a good starting point: https://github.com/madnight/docker-alpine-wkhtmltopdf

ivicaned commented 1 year ago

It's quite involved, you have to re-compile wkthtmltopdf from source to make it compatible with Alpine so images work. It may be possible, but I'm not sure it's worth it just to support Alpine. Feel free to look into a PR if you want. Here's a good starting point: https://github.com/madnight/docker-alpine-wkhtmltopdf

Thank you for fast response, we decided to temporary switch to Debian base image. I agree that there is much work! Good work anyway!

Rvanmech commented 10 months ago

Hi there, thanks very much for raising this issue and HakanL for maintaining this! I wanted to share my experience and logs in case it helps. I tried switching to Alpine following the example here (https://github.com/HakanL/WkHtmlToPdf-DotNet/blob/master/src/TestConsoleAppDockerAlpine/Dockerfile#L32C20-L32C20), and while it generates a PDF, we also were seeing images not loading and it seems fonts not displaying properly compared to the Debian distribution.

The logs generated by wkhtmltopdf say "Wrong JPEG library version: library is 80, caller expects 62". From my limited research, the sym link that we are doing in the Dockerfile helps for the jpeg library, but doesn't overcome library incompatibilities.

HakanL commented 10 months ago

The logs generated by wkhtmltopdf say "Wrong JPEG library version: library is 80, caller expects 62". From my limited research, the sym link that we are doing in the Dockerfile helps for the jpeg library, but doesn't overcome library incompatibilities.

Yeah, the symlink isn't a complete solution.