I have noticed that itext7.pdfhtml is generating incorrect PDF when app is published with PublishSingleFile set to true. Am I doing something wrong or is this known behavior?
This
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
generates correct PDF (preview https://i.sstatic.net/eUSJc.png).
and this
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
generates incorrect PDF where all <br/>
tags and paddings are ignored (preview https://i.sstatic.net/bvINf.png).
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/>
</div>
<div>
Přijal:<br/>
</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>
Dotnet info:
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
In some circumstances iText currently has an issue with parsing the default CSS file that is embedded into pdfHTML distribution. It's an analogue of default CSS that many browsers have.
What you can do as a workaround is:
Download the default.css
from the pdfHTML repository (https://github.com/itext/i7n-pdfhtml/blob/master/itext/itext.html2pdf/resources/default.css).
Put the downloaded file into your working directory
Add the link to the CSS file from your HTML: <link rel=""stylesheet"" href=""default.css""/>
You can also put your default.css
to another directory and either refer to the file at the absolute path or tweak converterProperties.SetBaseUri()
so that the base directory (the one that the default.css
file will be resolved against) differs from your work directory.
You can also integrate this CSS in a different way, for instance by copy-pasting the content and embedding it into your HTML. The main point is to get this CSS picked up.