Load Fonts on Demand in PDFs Using JavaScript

PSPDFKit for Web introduces a powerful dynamic font loading mechanism that allows you to dynamically load fonts when users enter text containing characters not supported by available fonts. This feature ensures that text is correctly displayed, even when the user’s language and character set aren’t known in advance. This feature can improve the rendered result in a number of use cases:

  • Office-to-PDF conversions — Office documents frequently use fonts that aren’t available to the SDK. With dynamic font loading, PSPDFKit for Web can fetch the necessary fonts to render the document text with closer fidelity.

  • Text annotations — Text annotations can use non-standard fonts to render the text, a common use case which can now be automatically handled by making use of dynamically loaded fonts.

  • PDF forms — Similarly to text annotations, form fields may also use fonts that the SDK doesn’t include by default. When dynamic font loading is used, we can ensure those forms will be correctly rendered.

To leverage dynamic font loading, provide a list of fonts in a JSON file, which will be loaded on demand.

Default Fonts Bundle

To use this feature, we provide a default, ready-to-use dynamic fonts bundle.

To use it, extract the compressed file into a public folder accessible to your application, preferably in the same origin as the application so as to avoid CORS issues. Then, set the dynamicFonts property in the configuration object passed to PSPDFKit.load() to a URL pointing to the JSON file with the dynamic fonts data:

  dynamicFonts: "https://example.com/path/to/fonts.json"

The fonts.json file contains the information necessary for PSPDFKit for Web to download and make use of the fonts included in the bundle when needed.

Do you want to create your own font bundle? Contact us on Support and we’ll help you!