Customize PDF fonts on iOS
PDF files should render consistently across different PDF viewers. This consistency is possible because a PDF file can embed the fonts required for rendering.
However, in some cases — due to file size or other considerations — PDFs don’t embed fonts. When this happens, the PDF viewer relies on system fonts, which may cause rendering issues if the required fonts are unavailable.
Embedding fonts in PDFs is the best way to ensure accurate rendering, but this isn’t always possible, especially when working with third-party PDFs. Custom font path support addresses this issue.
iOS comes with a relatively complete set of fonts, so it usually isn’t necessary to add a custom font path, except in special circumstances. We provide an example that shows how to do this using Nutrient. You can download the CustomFontPath
example project here.
The important part is supplying the extra font path when setting the license using the .additionalFontDirectories
option:
let additionalFontDirectory = Bundle.main.bundleURL.appendingPathComponent("AdditionalFonts", isDirectory: true).path PSPDFKit.SDK.setLicenseKey("YOUR_LICENSE_KEY_GOES_HERE", options: [.additionalFontDirectories: [additionalFontDirectory]])
NSString *additionalFontDirectory = [[NSBundle.mainBundle bundleURL] URLByAppendingPathComponent:@"AdditionalFonts" isDirectory:YES].path; [PSPDFKitGlobal setLicenseKey:@"YOUR_LICENSE_KEY_GOES_HERE" options:@{PSPDFSettingKeyAdditionalFontDirectories: @[additionalFontDirectory]}];
Do this as early as possible in the application’s launch process, like in application(_:willFinishLaunchingWithOptions:)
. You can read more about when to set the license key in the adding the license key guide.
The font directory can be any directory accessible to your app, and all .ttf
, .ttc
, and .otf
files will be added to the Nutrient font list.
If you expect rendered pages of documents that have previously been opened, and therefore have been cached, before specifying additional fonts to be different, you might need to clear the cache once to get the updated renderings:
PSPDFKit.SDK.shared.cache.clear()
Microsoft core fonts
Microsoft core fonts are widely used on the web and in PDFs. Adding them as custom fonts improves document conversion and rendering accuracy. Nutrient doesn’t include these fonts because Microsoft no longer provides them directly, and redistribution is prohibited by license. To use these fonts, download them from SourceForge and add them as custom fonts.