Custom Font Path

PDF files are supposed to be rendered exactly the same no matter what PDF viewer you’re using. One reason why this is the case is because a PDF file can embed the fonts required to render it.

Sometimes, due to size reasons or other considerations, the fonts are not embedded, which makes the PDF viewer look for the font on the current system. Depending on what fonts are available, this can cause rendering problems.

While the best option is to always embed the fonts in the PDF, this isn’t always possible, especially if you’re working with third-party PDF files. This is where custom font path support comes in.

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 of how to properly do this in our SDK DMG file found in Examples/CustomFontPath.

The important part is supplying the extra font path when setting the license using the option PSPDFAdditionalFontDirectories:

Copy
1
2
3
let additionalFontDirectory = Bundle.main.bundleURL.appendingPathComponent("AdditionalFonts", isDirectory: true).path
let options = [PSPDFSettingKey.additionalFontDirectories: [additionalFontDirectory]]
PSPDFKit.setLicenseKey("YOUR_LICENSE_KEY_GOES_HERE", options: options)
Copy
1
2
3
NSString* additionalFontDirectory = [[NSBundle.mainBundle bundleURL] URLByAppendingPathComponent:@"AdditionalFonts" isDirectory:YES].path;
NSDictionary<PSPDFSettingKey, id>* options = @{PSPDFAdditionalFontDirectories: @[additionalFontDirectory]};
[PSPDFKit setLicenseKey:@"YOUR_LICENSE_KEY_GOES_HERE" options:options];

This should be done 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 Adding the License Key.

The font directory can be any directory that is accessible to the app, and all .ttf, .ttc, and .otf files will be added to the font list of PSPDFKit.