Skip to content

Commit ec7c769

Browse files
Merge pull request #540 from telerik/new-kb-radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf-f6df166b1341460ab5e882aa5b41cec7
Added new kb article radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf
2 parents 982b056 + c6069ae commit ec7c769

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
---
2+
title: Correctly Rendering Non-Breaking Spaces in PDF from HTML with RadWordsProcessing
3+
description: This article demonstrates how to properly handle non-breaking spaces in HTML when converting to PDF using RadWordsProcessing libraries.
4+
type: how-to
5+
page_title: How to Ensure Non-Breaking Spaces Are Rendered Correctly in PDFs Generated from HTML
6+
slug: radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf
7+
tags: radwordsprocessing, document, processing, html,formatprovider, pdf, fontsprovider, nonbreaking,spaces
8+
res_type: kb
9+
ticketid: 1683368
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| ---- | ---- | ---- |
16+
| 2025.1.205| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
When converting HTML content to PDF format using [RadWordsProcessing]({%slug radwordsprocessing-overview%}), non-breaking spaces (` `) within and surrounding HTML tags are not rendered correctly in the generated PDF document, although they appear as expected in the DOCX output. This issue occurs only when exporting to PDF format due to the .NET Standard version of RadPdfProcessing lacking a default mechanism for reading font data, which is required for accurate space rendering in PDFs.
21+
22+
This knowledge base article shows how to ensure that non-breaking spaces in HTML are correctly rendered in the exported PDF documents using RadWordsProcessing.
23+
24+
![HTML to PDF with Non-Breaking Spaces](images/non-breaking-spaces-in-exported-pdf.png)
25+
26+
## Solution
27+
28+
To resolve the issue of non-breaking spaces not being rendered correctly in PDF documents generated from HTML content, it is necessary to implement a custom [FontsProvider]({%slug pdfprocessing-implement-fontsprovider%}). This ensures [RadPdfProcessing]({%slug radpdfprocessing-overview%}) has access to font data for accurately rendering spaces and other font-related features in the PDF output.
29+
30+
1. **Implement a Custom FontsProvider**
31+
32+
Create a class that extends `FontsProviderBase` and override the `GetFontData` method to provide the necessary font data. This method should return the font data as a byte array for the specified font properties.
33+
34+
```csharp
35+
public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase
36+
{
37+
public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties)
38+
{
39+
string fontFileName = fontProperties.FontFamilyName + ".ttf";
40+
string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
41+
42+
//The fonts can differ depending on the file
43+
if (fontProperties.FontFamilyName == "Segoe UI")
44+
{
45+
if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold)
46+
{
47+
fontFileName = $"segoeuiz.ttf";
48+
}
49+
else if (fontProperties.FontStyle == FontStyles.Italic)
50+
{
51+
fontFileName = $"segoeuii.ttf";
52+
}
53+
else if (fontProperties.FontWeight == FontWeights.Normal)
54+
{
55+
fontFileName = "segoeui.ttf";
56+
}
57+
else if (fontProperties.FontWeight == FontWeights.Bold)
58+
{
59+
fontFileName = $"segoeuib.ttf";
60+
}
61+
}
62+
else if (fontProperties.FontFamilyName == "Times New Roman")
63+
{
64+
if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold)
65+
{
66+
fontFileName = $"timesbi.ttf";
67+
}
68+
else if (fontProperties.FontStyle == FontStyles.Italic)
69+
{
70+
fontFileName = $"timesi.ttf";
71+
}
72+
else if (fontProperties.FontWeight == FontWeights.Normal)
73+
{
74+
fontFileName = "times.ttf";
75+
}
76+
else if (fontProperties.FontWeight == FontWeights.Bold)
77+
{
78+
fontFileName = $"timesbd.ttf";
79+
}
80+
}
81+
82+
//...add more fonts if needed...
83+
84+
DirectoryInfo directory = new DirectoryInfo(fontFolder);
85+
FileInfo[] fontFiles = directory.GetFiles();
86+
87+
var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase));
88+
if (fontFile != null)
89+
{
90+
var targetPath = fontFile.FullName;
91+
using (FileStream fileStream = File.OpenRead(targetPath))
92+
{
93+
using (MemoryStream memoryStream = new MemoryStream())
94+
{
95+
fileStream.CopyTo(memoryStream);
96+
Debug.WriteLine("Found "+ fontFileName);
97+
return memoryStream.ToArray();
98+
}
99+
}
100+
}
101+
Debug.WriteLine("NOT Found " + fontFileName);
102+
return null;
103+
}
104+
}
105+
```
106+
107+
2. **Set the Custom FontsProvider to the FixedExtensibilityManager**
108+
109+
Before generating the PDF document, assign an instance of the custom `FontsProvider` to the `FontsProvider` property of `FixedExtensibilityManager`.
110+
111+
```csharp
112+
Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider();
113+
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider;
114+
```
115+
116+
3. **Generate PDF Document from HTML Content**
117+
118+
Utilize the [HtmlFormatProvider]({%slug radwordsprocessing-formats-and-conversion-html-htmlformatprovider%}) to import HTML content and convert it to a [RadFlowDocument]({%slug radwordsprocessing-model-radflowdocument%}). Then, use the [PdfFormatProvider]({%slug radwordsprocessing-formats-and-conversion-pdf-pdfformatprovider%}) to export the document to PDF, ensuring non-breaking spaces and other font-related elements are rendered correctly.
119+
120+
```csharp
121+
// Example method implementation for converting HTML to PDF
122+
public static byte[] CreateDocumentFromHtml(string html, bool pdf = false)
123+
{
124+
// Conversion logic...
125+
}
126+
```
127+
128+
For a detailed guide on implementing a `FontsProvider`, refer to the [How to implement a FontsProvider]({%slug pdfprocessing-implement-fontsprovider%}) article. This implementation ensures that non-breaking spaces and other font-related elements are accurately rendered in PDF documents generated from HTML content using RadWordsProcessing.
129+
130+
>note There is also an alternative option of [manually registering the fonts]({%slug load-fonts-with-net-standard%}).
131+
132+
## See Also
133+
134+
- [How to Implement a FontsProvider for RadPdfProcessing]({%slug pdfprocessing-implement-fontsprovider%})

0 commit comments

Comments
 (0)