You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows容器中Evo Pdf库生成PDF内容变形问题求助

解决Windows Docker容器中Evo PDF生成PDF内容变形的问题

可能的原因及修复方案

1. 补充容器字体支持

Windows容器默认字体库不全,Evo PDF渲染时无法匹配所需字体,直接导致排版变形。

  • 手动添加常用字体到容器:
    1. 在项目根目录新建fonts文件夹,复制本地Windows系统(如C:\Windows\Fonts)中的宋体、黑体等常用字体文件进去
    2. 在Dockerfile中添加以下步骤:
    COPY ./fonts/* "C:/Windows/Fonts/"
    # 注册字体到系统注册表
    RUN reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "SimSun (TrueType)" /t REG_SZ /d "simsun.ttc" /f
    RUN reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "Microsoft YaHei UI (TrueType)" /t REG_SZ /d "msyh.ttc" /f
    

2. 补齐Evo PDF依赖的系统组件

Evo PDF的渲染引擎依赖GDI+等桌面组件,Windows Server Core镜像默认缺失这些组件。

  • 方案一:切换到带完整桌面体验的容器镜像,比如将mcr.microsoft.com/windows/servercore:ltsc2019替换为mcr.microsoft.com/windows:ltsc2019(注意镜像体积会增大)
  • 方案二:在Server Core镜像中安装桌面体验组件:
    RUN dism /online /enable-feature /all /featurename:ServerCore-DesktopExperience /NoRestart
    

3. 强制配置Evo PDF渲染参数

通过代码指定字体和渲染规则,避免容器环境下的自动适配异常:

HtmlToPdfConverter converter = new HtmlToPdfConverter();
// 指定默认渲染字体
converter.PdfDocumentOptions.DefaultFontName = "SimSun";
// 启用字体嵌入,防止字体缺失
converter.PdfDocumentOptions.FontEmbeddingEnabled = true;
// 固定页面尺寸与边距
converter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
converter.PdfDocumentOptions.TopMargin = 10;
converter.PdfDocumentOptions.BottomMargin = 10;
converter.PdfDocumentOptions.LeftMargin = 10;
converter.PdfDocumentOptions.RightMargin = 10;

byte[] htmlToPdfBuffer = converter.ConvertHtml("<p><b>Yes, Hello World</b></p><p>From EVO PDF !</p>", null);
System.IO.File.WriteAllBytes("HtmlToPdf.pdf", htmlToPdfBuffer);

4. 检查容器运行权限

确保容器进程拥有访问字体文件、系统资源的足够权限,避免因权限不足导致渲染异常。


内容的提问来源于stack exchange,提问作者Farhad Abaei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:24:57