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

itext7.pdfhtml单文件发布时忽略内边距与换行问题咨询

问题:单文件发布时itext7.pdfhtml生成的PDF样式异常

当使用.NET的单文件发布(PublishSingleFile=true)时,itext7.pdfhtml生成的PDF出现样式错误:所有<br/>标签的换行效果和内边距(padding)均被忽略。而不启用单文件发布时,生成的PDF样式完全正常。

复现步骤

正常发布(样式正确)

执行命令:

C:\Projects\itext7test\itext7test>dotnet publish --self-contained -r win-x64 -c Release
Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  itext7test -> C:\Projects\itext7test\itext7test\bin\Release\net6.0\win-x64\itext7test.dll
  itext7test -> C:\Projects\itext7test\itext7test\bin\Release\net6.0\win-x64\publish\
C:\Projects\itext7test\itext7test>.\bin\Release\net6.0\win-x64\publish\itext7test.exe

生成的PDF样式正常,<br/>换行有效,内边距样式生效。

单文件发布(样式异常)

执行命令:

C:\Projects\itext7test\itext7test>dotnet publish --self-contained -r win-x64 -c Release -p:PublishSingleFile=true
Microsoft (R) Build Engine version 17.2.0+41abc5629 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.

  Determining projects to restore...
  All projects are up-to-date for restore.
  itext7test -> C:\Projects\itext7test\itext7test\bin\Release\net6.0\win-x64\itext7test.dll
  itext7test -> C:\Projects\itext7test\itext7test\bin\Release\net6.0\win-x64\publish\
C:\Projects\itext7test\itext7test>.\bin\Release\net6.0\win-x64\publish\itext7test.exe

生成的PDF中,<br/>标签无换行效果,所有元素的padding样式被忽略。

相关代码与配置

Program.cs代码

using iText.Html2pdf;
using iText.Html2pdf.Resolver.Font;

var pdfStream = System.IO.File.OpenWrite("out.pdf");

var converterProperties = new ConverterProperties();
var fontProvider = new DefaultFontProvider(false, false, true);
converterProperties.SetFontProvider(fontProvider);

var generatedHtml = $@"<html lang=""cs"">
    <head>
<meta charset=""UTF-8""/>
<title></title>
<style>
    body, div, p {{padding: 10px;line-height: 1.75em;}}
    body {{font-family: Arial, sans-serif;}}
    div {{border: 1px solid black;}}
</style>
</head>
<body>
    <div style=""page-break-after: always;"">
        <div>
            <p style=""width: 40%;display: inline-block"">
                firma<br/>
                adresa<br/>
                zipcode city<br/>
                IČO: 123456789<br/>
            </p>
            <p style=""width: 40%;display: inline-block;text-align: right;float: right"">
                Příjmový pokladní doklad<br/>
                Číslo: <b>0</b><br/>
                Ze dne: <b>{DateTime.Now:d.M.yyyy}</b><br/>
            </p>
        </div>
        <div>
            Celková částka: <b> Kč</b><br/>
            Slovy: <b>~~ korun českých~~</b><br/>
            Účel platby: <br/>
        </div>
        <div>
            Přijato od: <br/>&nbsp;
        </div>
        <div>
            Přijal:<br/>&nbsp;
        </div>
    </div>
</body>
</html>";

HtmlConverter.ConvertToPdf(generatedHtml, pdfStream, converterProperties);

itext7test.csproj配置

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net6.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="itext7.pdfhtml" Version="4.0.3" />
    </ItemGroup>

</Project>

.NET环境信息

c:\>dotnet --info
.NET SDK (reflecting any global.json):
 Version:   6.0.302
 Commit:    c857713418

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19044
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.302\

global.json file:
  Not found

Host:
  Version:      6.0.7
  Architecture: x64
  Commit:       0ec02c8c96

.NET SDKs installed:
  5.0.101 [C:\Program Files\dotnet\sdk]
  5.0.203 [C:\Program Files\dotnet\sdk]
  5.0.409 [C:\Program Files\dotnet\sdk]
  6.0.302 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.26 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.26 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.26 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

原因与解决方法

这是itext7.pdfhtml在.NET单文件发布场景下的已知问题。单文件发布时,.NET会将所有程序集和资源打包到单个可执行文件中,运行时才提取资源,但itext7.pdfhtml默认的资源加载逻辑无法正确访问这些提取后的资源,导致负责处理<br/>换行、padding等基础样式的内部默认CSS样式表无法加载,最终出现样式异常。

解决方法

  1. 修改发布参数:发布时添加-p:IncludeNativeLibrariesForSelfExtract=true,确保itext所需资源能被正确提取和加载:
    dotnet publish --self-contained -r win-x64 -c Release -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true
    
  2. 升级版本:将itext7.pdfhtml升级到4.0.4及以上版本,官方已在后续版本中修复了单文件发布的资源加载问题。
  3. 手动配置资源路径:通过ConverterProperties指定自定义样式资源,将所需的基础CSS样式嵌入代码中,替代itext的默认样式表。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 02:54:37