AssetLoader
import AssetLoader from '@bbc/front-end-kit/js/pdf/core/AssetLoader';
Provides an easy API to load all the necessary assets (images and fonts) for PDF creation with PDFKit. The class caches the loaded assets in a static way, sharing the cache across all AssetLoader instances.
Getting started
import AssetLoader from '@bbc/front-end-kit/js/pdf/core/AssetLoader';
const loader = new AssetLoader();
// load fonts and images in parallel
await Promise.all([
loader.loadFonts([
{ name: 'font-regular', url: 'path/to/font-regular.ttf' },
{ name: 'font-bold', url: 'path/to/font-bold.ttf' },
]),
loader.loadImages([
{ name: 'logo', url: 'path/to/logo.png' },
]),
]);
// retrieve loaded assets (ArrayBuffer)
const fontBuffer = loader.getFontFile('font-regular');
const imageBuffer = loader.getImageFile('logo');
Tips
loadFonts and loadImages both return a Promise.all internally. Wrap them in Promise.all to start both in parallel and await both at once.
API
constructor()
Initialises the AssetLoader instance with empty _fonts and _images arrays.
Methods
getFont(name)
Returns the font definition object for the given name.
Parameters
name(string): The registered font name.
Returns
object — the font definition { name, url, file, isLoaded, error }.
getFontFile(name)
Returns the loaded file data for the given font.
Parameters
name(string): The registered font name.
Returns
ArrayBuffer — the raw font file data, ready to pass to PDFKit's .font() method.
loadFonts(fontsData)
Fetches all listed fonts and stores them in the instance cache. Already-loading or already-loaded entries are reused from the static FONT_CACHE.
Parameters
fontsData(object[]): Array of font definition objects:name(string): Key under which the asset is stored.url(string): URL to the font file.
Returns
Promise — resolves when all fonts are loaded.
getImage(name)
Returns the image definition object for the given name.
Parameters
name(string): The registered image name.
Returns
object — the image definition { name, url, file, isLoaded, error }.
getImageFile(name)
Returns the loaded file data for the given image.
Parameters
name(string): The registered image name.
Returns
ArrayBuffer — the raw image file data.
loadImages(imagesData)
Fetches all listed images and stores them in the instance cache. Already-loading or already-loaded entries are reused from the static IMAGE_CACHE.
Parameters
imagesData(object[]): Array of image definition objects:name(string): Key under which the asset is stored.url(string): URL to the image file.
Returns
Promise — resolves when all images are loaded.
Private properties
| Property | Type | Description |
|---|---|---|
_fonts | object[] | Font definition objects for this instance |
_images | object[] | Image definition objects for this instance |
Exported utility functions
These are also exported as named exports for advanced use:
getDefinitionFromCache(cache, url)
Looks up a definition by URL in a static cache array. Returns a resolving Promise if already loaded, the in-flight Promise if still loading, or undefined if not found.
createDefinition(data, localCache, cache)
Creates a new definition object { file, isLoaded, error, ...data } and pushes it to both the local instance array and the static cache.
loadData(definition)
Fetches definition.url via fetch, stores the resolved ArrayBuffer in definition.file, sets definition.isLoaded = true, and returns the Promise.