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);
}
}
}
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> "))); }