当前位置: 首页 > news >正文

wkhtmltopdf 与 .Net Core

wkhtmltopdf 是使用webkit引擎转化为pdf的开源小插件.
其有.NET CORE版本的组件,DinkToPdf,但该控件对跨平台支持有限
是由于各系统平台会产生不同的编译结果,故windows上使用.dll,而Linux上的动态链接库是.so
所以你需要在Linux系统上安装相关wkhtmltox软件。
我这里准备了 mcr.microsoft.com/dotnet/sdk:6.0 的docker镜像。

准备工作

虚拟机: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
Debian 11 镜像

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"]

生成镜像类
shell

docker build -t wkhtml:2023 .

docker-compose.yml

version: "3.0" #  模板的版本
networks:core-net:#  网络driver: bridge
services:test:networks:core-net:aliases:- testimage: wkhtml:2023volumes:- ./test:/appcommand: dotnet ConsoleApp.dll

生成测试类

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程序。则得到如此结果。
在这里插入图片描述

关于中文字体等

你可以使用Windows中的字体库 C:\Windows\Fonts 将其拷贝到 容器的 /usr/share/fonts
当然我是使用的容器挂载. 然后将其放到宿主机相关目录就可以了。
因为这里是用的相对路径,所以放置在docker-compose.yml所在目录的fonts就可以了。

docker-compose.yml

version: "3.0" #  模板的版本
networks:core-net:#  网络driver: bridge
services:test:networks:core-net:aliases:- testimage: wkhtml:2023volumes:- ./fonts:/usr/share/fontscommand: dotnet ConsoleApp.dll
http://www.lryc.cn/news/131638.html

相关文章:

  • Linux Mint 21.3 计划于 2023 年圣诞节发布
  • 腾讯云3年轻量应用服务器2核4G5M和2核2G4M详细介绍
  • rabbitmq中的消息确认
  • jenkins一键部署github项目
  • 岩土工程安全监测隧道中使用振弦采集仪注意要点?
  • 第四章nginx组件精讲
  • LlamaGPT -基于Llama 2的自托管类chatgpt聊天机器人
  • 常见的跨域解决方案
  • 分布式websocket解决方案
  • 奥威BI财务数据分析方案:借BI之利,成就智能财务分析
  • Android12之com.android.media.swcodec无法生成apex问题(一百六十三)
  • Xcode build和version
  • 前端面试:【原型链】代码世界的家族传承
  • 2D应用开发是选择WebGL 还是选择Canvas?
  • Android Framework 常见解决方案(20)UDP广播无效问题
  • VINS-Mono中的边缘化与滑窗 (4)——VINS边缘化为何是局部变量边缘化?
  • 真·VB.NET彻底释放Interop.Excel对象
  • 记录hutool http通过代理模式proxy访问外面的链接
  • Selenium 自动化 | 案例实战篇
  • 前端技术栈es6+promise
  • windows vscode使用opencv
  • json文件读取数据报错 AttributeError: ‘str‘ object has no attribute ‘items‘
  • 1、Spring_IOC
  • Socks5、IP代理在爬虫开发与HTTP通信中的应用
  • 重新认识小米
  • react之react-redux的介绍、基本使用、获取状态、分发动作、数据流、reducer的分离与合并等
  • 滑块验证码-接口返回base64数据
  • 智能文件改名,一键与上上级目录名称同步,让文件整理更加便捷
  • RK3399平台开发系列讲解(内核调试篇)Valgrind使用案例
  • 07_缓存预热缓存雪崩缓存击穿缓存穿透