-
Notifications
You must be signed in to change notification settings - Fork 40
Added new kb article radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf #540
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dessyordanova
merged 4 commits into
master
from
new-kb-radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf-f6df166b1341460ab5e882aa5b41cec7
May 14, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a187e54
Added new kb article radwordsprocessing-correctly-render-non-breaking…
02cb463
new KB - Correctly Rendering Non-Breaking Spaces in PDF from HTML wit…
dessyordanova a695a22
addressed review
dessyordanova c6069ae
Update radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf.md
dessyordanova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
134 changes: 134 additions & 0 deletions
134
knowledge-base/radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
--- | ||
title: Correctly Rendering Non-Breaking Spaces in PDF from HTML with RadWordsProcessing | ||
description: This article demonstrates how to properly handle non-breaking spaces in HTML when converting to PDF using RadWordsProcessing libraries. | ||
type: how-to | ||
page_title: How to Ensure Non-Breaking Spaces Are Rendered Correctly in PDFs Generated from HTML | ||
slug: radwordsprocessing-correctly-render-non-breaking-spaces-in-pdf | ||
tags: radwordsprocessing, document, processing, html,formatprovider, pdf, fontsprovider, nonbreaking,spaces | ||
res_type: kb | ||
ticketid: 1683368 | ||
--- | ||
|
||
## Environment | ||
|
||
| Version | Product | Author | | ||
| ---- | ---- | ---- | | ||
| 2025.1.205| RadWordsProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| | ||
|
||
## Description | ||
|
||
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. | ||
|
||
This knowledge base article shows how to ensure that non-breaking spaces in HTML are correctly rendered in the exported PDF documents using RadWordsProcessing. | ||
|
||
 | ||
|
||
## Solution | ||
|
||
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. | ||
|
||
1. **Implement a Custom FontsProvider** | ||
|
||
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. | ||
|
||
```csharp | ||
public class FontsProvider : Telerik.Windows.Documents.Extensibility.FontsProviderBase | ||
{ | ||
public override byte[] GetFontData(Telerik.Windows.Documents.Core.Fonts.FontProperties fontProperties) | ||
{ | ||
string fontFileName = fontProperties.FontFamilyName + ".ttf"; | ||
string fontFolder = Environment.GetFolderPath(Environment.SpecialFolder.Fonts); | ||
|
||
//The fonts can differ depending on the file | ||
if (fontProperties.FontFamilyName == "Segoe UI") | ||
{ | ||
if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold) | ||
{ | ||
fontFileName = $"segoeuiz.ttf"; | ||
} | ||
else if (fontProperties.FontStyle == FontStyles.Italic) | ||
{ | ||
fontFileName = $"segoeuii.ttf"; | ||
} | ||
else if (fontProperties.FontWeight == FontWeights.Normal) | ||
{ | ||
fontFileName = "segoeui.ttf"; | ||
} | ||
else if (fontProperties.FontWeight == FontWeights.Bold) | ||
{ | ||
fontFileName = $"segoeuib.ttf"; | ||
} | ||
} | ||
else if (fontProperties.FontFamilyName == "Times New Roman") | ||
{ | ||
if (fontProperties.FontStyle == FontStyles.Italic && fontProperties.FontWeight == FontWeights.Bold) | ||
{ | ||
fontFileName = $"timesbi.ttf"; | ||
} | ||
else if (fontProperties.FontStyle == FontStyles.Italic) | ||
{ | ||
fontFileName = $"timesi.ttf"; | ||
} | ||
else if (fontProperties.FontWeight == FontWeights.Normal) | ||
{ | ||
fontFileName = "times.ttf"; | ||
} | ||
else if (fontProperties.FontWeight == FontWeights.Bold) | ||
{ | ||
fontFileName = $"timesbd.ttf"; | ||
} | ||
} | ||
|
||
//...add more fonts if needed... | ||
|
||
DirectoryInfo directory = new DirectoryInfo(fontFolder); | ||
FileInfo[] fontFiles = directory.GetFiles(); | ||
|
||
var fontFile = fontFiles.FirstOrDefault(f => f.Name.Equals(fontFileName, StringComparison.InvariantCultureIgnoreCase)); | ||
if (fontFile != null) | ||
{ | ||
var targetPath = fontFile.FullName; | ||
using (FileStream fileStream = File.OpenRead(targetPath)) | ||
{ | ||
using (MemoryStream memoryStream = new MemoryStream()) | ||
{ | ||
fileStream.CopyTo(memoryStream); | ||
Debug.WriteLine("Found "+ fontFileName); | ||
return memoryStream.ToArray(); | ||
} | ||
} | ||
} | ||
Debug.WriteLine("NOT Found " + fontFileName); | ||
return null; | ||
} | ||
} | ||
``` | ||
|
||
2. **Set the Custom FontsProvider to the FixedExtensibilityManager** | ||
|
||
Before generating the PDF document, assign an instance of the custom `FontsProvider` to the `FontsProvider` property of `FixedExtensibilityManager`. | ||
|
||
```csharp | ||
Telerik.Windows.Documents.Extensibility.FontsProviderBase fontsProvider = new FontsProvider(); | ||
Telerik.Windows.Documents.Extensibility.FixedExtensibilityManager.FontsProvider = fontsProvider; | ||
``` | ||
|
||
3. **Generate PDF Document from HTML Content** | ||
|
||
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. | ||
|
||
```csharp | ||
// Example method implementation for converting HTML to PDF | ||
public static byte[] CreateDocumentFromHtml(string html, bool pdf = false) | ||
{ | ||
// Conversion logic... | ||
} | ||
``` | ||
|
||
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. | ||
|
||
>note There is also an alternative option of [manually registering the fonts]({%slug load-fonts-with-net-standard%}). | ||
|
||
## See Also | ||
|
||
- [How to Implement a FontsProvider for RadPdfProcessing]({%slug pdfprocessing-implement-fontsprovider%}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reference kb in another article |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.