wkhtmltopdf 是使用webkit引擎轉化為pdf的開源小插件.
其有.NET CORE版本的組件,DinkToPdf,但該控件對跨平臺支持有限。
故打算在Linux上安裝相關插件直接調用.
準備工作
虛擬機:Linux version 3.10.0-1160.el7.x86_64
wkhtmltox開發包:wkhtmltox_0.12.6.1-2.bullseye_amd64.deb
運行環境:mcr.microsoft.com/dotnet/aspnet:6.0
Dockerfile
# 微軟Debian 11 鏡像
FROM mcr.microsoft.com/dotnet/sdk:6.0
WORKDIR /app
# 設置清華源
RUN sed -i 's/deb.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
RUN sed -i 's/security.debian.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list
# 安裝依賴項
RUN apt-get update && apt-get install -y \fontconfig \libfreetype6 \libjpeg62-turbo \libpng16-16 \libx11-6 \libxcb1 \libxext6 \libxrender1 \xfonts-75dpi \xfonts-base# 安裝wkhtmltox
COPY wkhtmltox_0.12.6.1-2.bullseye_amd64.deb .
RUN dpkg -i wkhtmltox_0.12.6.1-2.bullseye_amd64.deb# 設置環境變量
ENV LD_LIBRARY_PATH=/usr/local/lib:/usr/lib:/usr/local/lib64:/usr/lib64CMD ["/bin/bash"]
生成測試類
using System.Runtime.InteropServices;public class Program
{// 聲明CAPI函數[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern int wkhtmltopdf_init(int use_graphics);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern IntPtr wkhtmltopdf_create_global_settings();[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern IntPtr wkhtmltopdf_create_object_settings();[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern void wkhtmltopdf_set_global_setting(IntPtr settings, string name, string value);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern void wkhtmltopdf_set_object_setting(IntPtr settings, string name, string value);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern IntPtr wkhtmltopdf_create_converter(IntPtr settings);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern void wkhtmltopdf_add_object(IntPtr converter, IntPtr objectSettings, byte[] data);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern int wkhtmltopdf_convert(IntPtr converter);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern void wkhtmltopdf_destroy_converter(IntPtr converter);[DllImport("libwkhtmltox.so", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]public static extern void wkhtmltopdf_deinit();public static void Main(string[] args){Console.WriteLine("123");try{// 初始化wkhtmltopdf庫wkhtmltopdf_init(0);// 創建全局設置和對象設置IntPtr gs = wkhtmltopdf_create_global_settings();IntPtr os = wkhtmltopdf_create_object_settings();// 設置全局設置和對象設置wkhtmltopdf_set_global_setting(gs, "out", "output.pdf");wkhtmltopdf_set_object_setting(os, "page", "1.html");// 創建轉換器IntPtr converter = wkhtmltopdf_create_converter(gs);// 添加要轉換的對象wkhtmltopdf_add_object(converter, os, null);// 執行轉換wkhtmltopdf_convert(converter);// 清理資源wkhtmltopdf_destroy_converter(converter);wkhtmltopdf_deinit();}catch(Exception ex){Console.WriteLine(ex.ToString());}}
}
通過 Volumn 掛載將dll直接放置在容器中,并啟動dll程序。則得到如此結果。