Customize or Edit PDF Fonts on iOS

PDF files are supposed to be rendered exactly the same, no matter which 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.

However, sometimes — due to the size or other considerations — fonts aren’t embedded, which makes the PDF viewer look for fonts on the current system. Depending on the fonts that are available, this can cause rendering problems.

While the best option is to always embed 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 that shows how to do this using PSPDFKit. 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
let options: [SDK.Setting: AnyObject] = [.additionalFontDirectories: [additionalFontDirectory]]
PSPDFKit.SDK.setLicenseKey("YOUR_LICENSE_KEY_GOES_HERE", options: options)
NSString* additionalFontDirectory = [[NSBundle.mainBundle bundleURL] URLByAppendingPathComponent:@"AdditionalFonts" isDirectory:YES].path;
NSDictionary<PSPDFSettingKey, id>* options = @{PSPDFAdditionalFontDirectories: @[additionalFontDirectory]};
[PSPDFKitGlobal 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 the adding the license key guide.

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

Microsoft Core Fonts

Microsoft core fonts are widely used on the web and in PDF files. Including them as custom fonts in your project improves the conversion and rendering fidelity for documents that use them. These fonts aren’t included in PSPDFKit because Microsoft no longer offers these files directly, and the license prohibits redistribution. To use these fonts in your project, download them from SourceForge and add them to PSPDFKit as a custom font.