Html to PDF Bold appears inconsistent (Telerik 2024.3.802)

2 Answers 87 Views
PdfProcessing
Jason
Top achievements
Rank 1
Iron
Jason asked on 22 Aug 2024, 02:25 AM | edited on 22 Aug 2024, 02:42 AM

 

I have the following code that shows the bold formatting from HTML to PDF appears inconsistent.

The last table has all the cells wrapped in <b> tags, but they are not rendered bold in the PDF. There seems to be some correlation to the length of the text (short text sometimes will be bold). I have also tried <strong> tags and CSS that applies bold. It does not matter which font is chosen.

Inconclusive observation: It appears that if the bold text line wraps inside a cell, it doesn’t apply bold. Text that does not wrap in the cell appears to apply bold.

Any help appreciated.

 


using RegServices.Portal.Web.Services.Pdf;
using Telerik.Documents.ImageUtils;
using Telerik.Windows.Documents.Flow.FormatProviders.Html;
using Telerik.Windows.Documents.Flow.Model;
using Xunit.Abstractions;

namespace Portal.Web.Tests.Pdf;

public class PdfBoldRenderTests : OutputTestBase
{
    private readonly IHtmlToWordConverter _htmlToWordConverter = new HtmlToWordConverter();
    string path = "c:\\\\temp\\\\pdf\\\\";
    public PdfBoldRenderTests(ITestOutputHelper output) : base(output)
    {
    }

    [Fact]
    public void CanRenderBold()
    {
        var html = $$"""
                   <!DOCTYPE html>
                   <html lang="en">
                      <head>
                         <title>Test</title>
                         <style>
                            body { 
                            font-family: Arial;
                            }
                            .bold {
                            font-weight: bold;
                            }
                            .font-bold {
                            font-weight: 800;
                            }
                            .font-black {
                            font-weight: 800;
                            }
                         </style>
                      </head>
                      <body>
                         <table>
                            <tr>
                               <td>
                                  The quick brow fox. Regular text <br>
                                  <b>The quick brow fox.  Bold text</b><br>
                                  <span class="bold">The quick brow fox.  Bold text via css</span><br>
                               </td>
                               <td>
                                  <table>
                                     <tr>
                                        <td>
                                           The quick brow fox. Regular text <br>
                                           <b>The quick brow fox.  Bold text</b><br>
                                           <span class="bold">The quick brow fox.  Bold text via css</span><br>
                                        </td>
                                     </tr>
                                  </table>
                               </td>
                            </tr>
                            <tr>
                               <td colspan="2">
                                  <table>
                                     <tbody>
                                        <tr>
                                           <td class="w-2-24">
                                              <b>Financial year</b>
                                           </td>
                                           <td>
                                              <b>General Services Charge (range)</b><em>(weekly)</em>
                                              <span class="font-black">General Services Charge (range)</span>
                                           </td>
                                           <td class="w-2-24">
                                              <b>Overall % change from previous year </b>
                                           </td>
                                           <td>
                                              <b>
                                              Maintenance Reserve Fund contribution (range)
                                              </b>
                                              <em>(weekly)</em>
                                           </td>
                                           <td class="w-2-24">
                                              <b>Overall % change from previous year</b>
                                              <em>(+ or -)</em>
                                           </td>
                                        </tr>
                                  </table>
                               </td>
                            </tr>
                         </table>
                         <b>The quick brow fox.  Bold text</b><br>
                         <table>
                            <tbody>
                               <tr>
                                  <td class="w-2-24">
                                     <b>Financial year</b>
                                  </td>
                                  <td>
                                     <b>Bold Text</b><em>(weekly)</em>
                                     <span class="font-black">General Services Charge (range)</span>
                                  </td>
                                  <td class="w-2-24">
                                     <b>Bold Text</b>
                                  </td>
                                  <td>
                                     <b>
                                     Bold Text
                                     </b>
                                     <em>(weekly)</em>
                                  </td>
                                  <td class="w-2-24">
                                     <b>Bold Text</b>
                                     <em>(+ or -)</em>
                                  </td>
                               </tr>
                            </tbody>
                         </table>
                         
                         <b>The quick brow fox.  Bold text</b><br>
                         <table>
                            <tr>
                               <td>
                                  <b>I am a bold text</b>
                               </td>
                               <td>
                                  <b>I am a bold text
                                  </b><em>(weekly)</em>
                               </td>
                               <td>
                                  <b>I am a bold text</b>
                               </td>
                               <td>
                                  <b>
                                  I am a bold text test test
                                  </b>
                                  <em>(weekly)</em>
                               </td>
                               <td>
                                  <b>I am a bold text test</b>
                               </td>
                            </tr>
                         </table>
                      </body>
                   </html>
                   """;

        var htmlDocument = _htmlToWordConverter.ConvertHtmlToWord(html);
        var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();

        Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider =
            new FontsProvider();

        using var memoryStream = new MemoryStream();
        docxProvider.Export(htmlDocument, memoryStream);
        var documentBytes = memoryStream.ToArray();

        File.WriteAllBytes($"{path}Test.docx", documentBytes);

        var (pdfBytes, pdfDoc) = PdfConverter.ConvertDocxToPdf(documentBytes);
        File.WriteAllBytes($"{path}Test.pdf", pdfBytes);

        var provider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();
        using Stream pdfStream = File.OpenWrite($"{path}Test2.pdf");
        provider.Export(pdfDoc, pdfStream);
    }
    
    public interface IHtmlToWordConverter
    {
        RadFlowDocument ConvertHtmlToWord(string html);
    }

    public class HtmlToWordConverter : IHtmlToWordConverter
    {
        public RadFlowDocument ConvertHtmlToWord(string html)
        {
            var htmlFormatProvider = new HtmlFormatProvider();
            return htmlFormatProvider.Import(html);
        }
    }

    public static class PdfConverter
    {
        static PdfConverter()
        {
            var defaultImagePropertiesResolver = new ImagePropertiesResolver();
            Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.ImagePropertiesResolver =
                defaultImagePropertiesResolver;
        }

        public static (byte[] bytes, RadFlowDocument doc) ConvertDocxToPdf(byte[] docxBytes)
        {
            var docxProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Docx.DocxFormatProvider();
            var pdfProvider = new Telerik.Windows.Documents.Flow.FormatProviders.Pdf.PdfFormatProvider();

            var document = docxProvider.Import(docxBytes);

            using var memoryStream = new MemoryStream();
            pdfProvider.Export(document, memoryStream);
            pdfProvider.ExportToFixedDocument(document);
            return (memoryStream.ToArray(), document);
        }
    }
}


Jason
Top achievements
Rank 1
Iron
commented on 22 Aug 2024, 03:16 AM

Further Testing.

If I wrap my output in a method that splits on whitespace, thus avoiding line wrapping, then all the words are bold.  So I do suspect there is a bug in the Telerik PDF rendering to do with being in the Table Cell and line wrapping.

 

  protected MarkupString BoldTextForTelerikBug(string text)
    {
        return new MarkupString(string.Join("<b>", text.Split(' ').Select(x => $"<b>{x}</b> ")));
    }

2 Answers, 1 is accepted

Sort by
1
Setven
Top achievements
Rank 1
Iron
Iron
answered on 31 Aug 2024, 12:52 PM
Hey Jason, thanks for reporting the bold text inconsistency. I've seen similar issues before. It might be related to how line breaks are handled within cells. Have you tried setting a fixed cell height or using a different approach for bold text within tables (e.g., dedicated bold table cells)?
Yoan
Telerik team
commented on 04 Sep 2024, 08:27 AM

Hi Jason,

Your input on the matter is much appreciated. Please correct me if I am wrong but from what I understand you are also encountering bolding issues with wrapped text and line breaks.

As I mentioned in my latest reply I am unable to reproduce such results on my end, however, I would like to investigate this behavior further in order to avoid such scenarios in the future. With that in mind, would you be able to provide us with a sample project or steps that I can take to achieve that? This way I can try to identify the cause and try to handle it.

Also, can you confirm whether the above-provided workaround works for you and handles the unexpected results you are experiencing?

Thank you in advance for your cooperation.

Regards,

Yoan

Jason
Top achievements
Rank 1
Iron
commented on 03 Mar 2025, 11:59 AM

Thank you Setven,  after coming back to this issue since updating to 2025.1  we are still seeing it.  Taking your advice about line breaks we wrote a method to hard break the text ourselves int he cell, and this has made the bold formatting consistent.  It certainly feels like a bug in the Telerik lib, but this workaround should suffice.
Yoan
Telerik team
commented on 05 Mar 2025, 01:43 PM

Hello Jason,

Thank you for writing back with feedback.

I am glad to hear you confirm that the suggested by Steven workaround is sufficient. As it fits your scenario you can indeed use it as a temporary solution.

When it comes to the WordsProcessing: HtmlFormatProvider: Add full support for font-weight CSS property task, unfortunately, it is still "Unplanned", due to the fact that we prioritize the logged tasks based on customer demand, complexity, development capacity, etc. In cases like this one, tasks are rated with a lower score which is why they can sometimes be left behind.

As for the wrapping issue, we are still unable to reproduce it on our end however, your feedback will surely help us narrow down potential causes.

We appreciate your efforts.

Regards,

Yoan

0
Yoan
Telerik team
answered on 26 Aug 2024, 01:52 PM

Hello Jason,

Currently, the WordsProcessing library only partially supports the font-weight CSS property. As of now, the supported values are bold and normal. We already have a feature request about the full support of this functionality logged in our Feedback Portal - WordsProcessing: HtmlFormatProvider: Add full support for font-weight CSS property. Please accept my apologies for the setbacks this might be causing you. If you are interested make sure you cast your vote for the task in order to increase its priority in our backlog and to subscribe so you can track potential status changes in case of any updates.

With that in mind, specifying the font-weight value in numbers might lead to unexpected results. On my end, setting all styles to "bold"

<style>
    body { 
    font-family: Arial;
    }
    .bold {
    font-weight: bold;
    }
    .font-bold {
    font-weight: bold;
    }
    .font-black {
    font-weight: bold;
    }
</style>

...and replacing any <span> elements with <p> in the HTML manages to resolve all bolding issues. I am also unable to reproduce the wrapping issue as all bolding appears to be present after export:

I am attaching for your disposal the project I am using on my end and the modified HTML. Feel free to examine it, use it for your own purposes, and do not hesitate to correct me if I am doing something differently that prevents me from reproducing the same wrapping issue.

I hope this helps.

Regards,
Yoan
Progress Telerik

Love the Telerik and Kendo UI products and believe more people should try them? Invite a fellow developer to become a Progress customer and each of you can get a $50 Amazon gift voucher.

Tags
PdfProcessing
Asked by
Jason
Top achievements
Rank 1
Iron
Answers by
Setven
Top achievements
Rank 1
Iron
Iron
Yoan
Telerik team
Share this question
or