On this page

Skip to content

Pitfalls and Solutions for 'TheArtOfDev.HtmlRenderer.PdfSharp'

TLDR

  • If you encounter errors when executing PdfGenerator.GeneratePdf, downgrade the PDFsharp version to 1.32.3057.
  • If the project fails to compile, check for and remove legacy NuGet restore settings (NuGet.targets).
  • If Chinese characters display incorrectly, explicitly specify font-family as '標楷體' (BiauKai) or 'DFKai-SB' in your CSS (depending on the OS environment).
  • If nested table borders are clipped, fix them by adding a <tr> with a very small height inside the table and fine-tuning the height attribute.

Package Initialization and Version Conflicts

When using TheArtOfDev.HtmlRenderer.PdfSharp in a .NET Framework project, you may encounter runtime errors if you run the sample code directly.

When this issue occurs: When installing the package for the first time and executing the PdfGenerator.GeneratePdf method.

Solution:

  1. Downgrade dependency: Downgrade the PDFsharp version to 1.32.3057.
  2. Clean up NuGet settings: If the project file (.csproj) contains references to legacy NuGet.targets, remove them; otherwise, it will cause compilation failures.
csharp
// Successful execution example
PdfDocument pdf = PdfGenerator.GeneratePdf("<p><h1>Hello World</h1>This is html rendered text</p>", PageSize.A4);
pdf.Save("document.pdf");

Chinese Font Display Issues

By default, this package has limited support for Chinese characters. If no font is specified, Chinese will not render correctly.

When this issue occurs: When the HTML content contains Chinese characters and no CSS font properties are set.

Solution: Force the font-family in the <style> block of your HTML.

csharp
string html = @"
    <style>
        * {
            font-family: 標楷體;
        }
    </style>
    <p>中文</p>
";

TIP

  • In a Windows Chinese environment, please use '標楷體'; in an English environment, use 'DFKai-SB'.
  • Currently, only '標楷體' and 'Malgun Gothic' have been tested to display Chinese reliably.

Table Borders Being Clipped

When using nested tables where internal <td> elements have fixed height attributes, the bottom border is prone to being clipped.

When this issue occurs: When the height calculation of the nested table's <td> element is imprecise, causing content overflow or incorrect border rendering ranges.

Solution: There is no direct fix parameter. It is recommended to use a "patch" strategy: manually add a <tr> with a very small height at the end of the table and fine-tune the height attribute (in increments of 0.5px) to calibrate the border position.

html
<!-- Example: Fixing borders with an extra tr -->
<table>
    <tr>
        <td>
            <table class="list">
                <tr><td>Content</td></tr>
                <!-- Extra tr added for height calibration -->
                <tr>
                    <td colspan="3" style="height: 9px;"></td>
                </tr>
            </table>
        </td>
    </tr>
</table>

Insights

TheArtOfDev.HtmlRenderer.PdfSharp has more complete CSS support than iTextSharp. Many styles that do not work in iTextSharp will function normally here, which may require you to readjust your original layout. However, when dealing with non-standard HTML structures (such as placing an <hr /> inside a <td>), the rendering results may differ from modern browsers (like Chrome). It is recommended to perform multiple tests for complex layouts during development.

Changelog

  • 2023-12-05 Initial version created.