BBC's guide to development
  • General

    • About
    • Tools
    • Git(hub)
    • Showpad
    • Hosting
    • Maintenance
    • Security
    • Go live checklist
  • Front-end development

    • Bundlers
    • CSS/SCSS
    • Javascript
    • Vue
    • PHP
    • Mails
    • Dev Faq
  • Functions
  • Mixins
  • General

    • OOP Structure
  • Component Classes

    • Accordion
    • App
    • Component
    • HighwayApp
    • Popup
    • PNG Sequencer
    • Tab
  • Manager Classes

    • BountListenerMgr
    • Cache
    • Configuration
    • InViewStateMgr
    • Instance Manager
    • Event dispatcher
  • Factories

    • SwiperFactory
  • PDF

    • AssetLoader
    • BasePdfDoc
    • TemplatePdfDoc
    • CustomPdfDoc
  • Utility functions

    • canvas
    • Connection Status
    • css
    • dev
    • placeholder
    • dom
    • fetch
    • json
    • object
    • scroll
    • scrollbar
    • spreadsheets
    • string
    • url
  • General

    • ComponentMgr
    • ThreeJsViewer
  • Components

    • ComponentMgr
    • GltfModel
    • Snappable
    • Socket
    • ThreeJsViewer
    • ThreeJsViewerCamera
  • Loaders

    • ConfigurationSerializer
    • GltfBlockParser
  • Utils

    • CanvasInputAdapter
    • CollisionManager
    • SocketGridExpander
    • blender
    • headless
  • General

    • Troubleshooting
    • Legacy
  • Components

    • AssetBar
    • ConfigGenerator
    • ShowpadApp
  • Managers

    • Assets
    • AppsDb
    • Config
  • Utils

    • Connection Status
    • general
    • showpad-interactive
    • showpad-upload
  • Components

    • Accordion
    • BackButton
    • Breadcrumb
    • ByltButton
    • Hamburger
    • Icon
    • Logo
    • Loader
    • Modal
    • Popup
    • Prompt
    • ProgressBar
    • TextLoader
  • Composables

    • useDebugMode
    • useConnectionStatus
  • Utils

    • dom
    • props
  • General

    • General
    • Tracking
  • Components

    • Accordion
    • ActionButton
    • AssetItem
    • AssetList
    • BackButton
    • ConfigGenButton
    • Logo
    • Media
    • Modal
    • Popup
    • Prompt
    • SPButton
    • SPRouterView
    • SPTrackedRouterLink
    • TextLoader
    • View
  • Composables

    • useConnectionStatus
  • Stores

    • useAppsDbStore
    • useBreadcrumbStore
    • useShowpadAPIStore
    • useShowpadSDKStore
    • useSpConfigStore
    • useSpStore
    • useSpTrackingStore
  • The New Kit

    • General
    • Installation & Usage
    • ACF Blocks
    • PHPCS
    • Functions
    • Vite
    • WP Config
    • Staging Deployment
  • Best Practices

    • Page Structure
    • Fonts/Typography
  • Todo
GitHub
  • General

    • About
    • Tools
    • Git(hub)
    • Showpad
    • Hosting
    • Maintenance
    • Security
    • Go live checklist
  • Front-end development

    • Bundlers
    • CSS/SCSS
    • Javascript
    • Vue
    • PHP
    • Mails
    • Dev Faq
  • Functions
  • Mixins
  • General

    • OOP Structure
  • Component Classes

    • Accordion
    • App
    • Component
    • HighwayApp
    • Popup
    • PNG Sequencer
    • Tab
  • Manager Classes

    • BountListenerMgr
    • Cache
    • Configuration
    • InViewStateMgr
    • Instance Manager
    • Event dispatcher
  • Factories

    • SwiperFactory
  • PDF

    • AssetLoader
    • BasePdfDoc
    • TemplatePdfDoc
    • CustomPdfDoc
  • Utility functions

    • canvas
    • Connection Status
    • css
    • dev
    • placeholder
    • dom
    • fetch
    • json
    • object
    • scroll
    • scrollbar
    • spreadsheets
    • string
    • url
  • General

    • ComponentMgr
    • ThreeJsViewer
  • Components

    • ComponentMgr
    • GltfModel
    • Snappable
    • Socket
    • ThreeJsViewer
    • ThreeJsViewerCamera
  • Loaders

    • ConfigurationSerializer
    • GltfBlockParser
  • Utils

    • CanvasInputAdapter
    • CollisionManager
    • SocketGridExpander
    • blender
    • headless
  • General

    • Troubleshooting
    • Legacy
  • Components

    • AssetBar
    • ConfigGenerator
    • ShowpadApp
  • Managers

    • Assets
    • AppsDb
    • Config
  • Utils

    • Connection Status
    • general
    • showpad-interactive
    • showpad-upload
  • Components

    • Accordion
    • BackButton
    • Breadcrumb
    • ByltButton
    • Hamburger
    • Icon
    • Logo
    • Loader
    • Modal
    • Popup
    • Prompt
    • ProgressBar
    • TextLoader
  • Composables

    • useDebugMode
    • useConnectionStatus
  • Utils

    • dom
    • props
  • General

    • General
    • Tracking
  • Components

    • Accordion
    • ActionButton
    • AssetItem
    • AssetList
    • BackButton
    • ConfigGenButton
    • Logo
    • Media
    • Modal
    • Popup
    • Prompt
    • SPButton
    • SPRouterView
    • SPTrackedRouterLink
    • TextLoader
    • View
  • Composables

    • useConnectionStatus
  • Stores

    • useAppsDbStore
    • useBreadcrumbStore
    • useShowpadAPIStore
    • useShowpadSDKStore
    • useSpConfigStore
    • useSpStore
    • useSpTrackingStore
  • The New Kit

    • General
    • Installation & Usage
    • ACF Blocks
    • PHPCS
    • Functions
    • Vite
    • WP Config
    • Staging Deployment
  • Best Practices

    • Page Structure
    • Fonts/Typography
  • Todo
GitHub
  • AssetLoader

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

PropertyTypeDescription
_fontsobject[]Font definition objects for this instance
_imagesobject[]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.

Edit this page
Last Updated: 4/27/26, 12:56 PM
Contributors: Nicolas Jaenen